nfs4proc.c 288 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728
  1. /*
  2. * fs/nfs/nfs4proc.c
  3. *
  4. * Client-side procedure declarations for NFSv4.
  5. *
  6. * Copyright (c) 2002 The Regents of the University of Michigan.
  7. * All rights reserved.
  8. *
  9. * Kendrick Smith <kmsmith@umich.edu>
  10. * Andy Adamson <andros@umich.edu>
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. *
  16. * 1. Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. * 2. Redistributions in binary form must reproduce the above copyright
  19. * notice, this list of conditions and the following disclaimer in the
  20. * documentation and/or other materials provided with the distribution.
  21. * 3. Neither the name of the University nor the names of its
  22. * contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  26. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  27. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  28. * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  29. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  30. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  31. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  32. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  33. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  34. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. #include <linux/mm.h>
  38. #include <linux/delay.h>
  39. #include <linux/errno.h>
  40. #include <linux/string.h>
  41. #include <linux/ratelimit.h>
  42. #include <linux/printk.h>
  43. #include <linux/slab.h>
  44. #include <linux/sunrpc/clnt.h>
  45. #include <linux/nfs.h>
  46. #include <linux/nfs4.h>
  47. #include <linux/nfs_fs.h>
  48. #include <linux/nfs_page.h>
  49. #include <linux/nfs_mount.h>
  50. #include <linux/namei.h>
  51. #include <linux/mount.h>
  52. #include <linux/module.h>
  53. #include <linux/xattr.h>
  54. #include <linux/utsname.h>
  55. #include <linux/freezer.h>
  56. #include <linux/iversion.h>
  57. #include "nfs4_fs.h"
  58. #include "delegation.h"
  59. #include "internal.h"
  60. #include "iostat.h"
  61. #include "callback.h"
  62. #include "pnfs.h"
  63. #include "netns.h"
  64. #include "sysfs.h"
  65. #include "nfs4idmap.h"
  66. #include "nfs4session.h"
  67. #include "fscache.h"
  68. #include "nfs40.h"
  69. #include "nfs42.h"
  70. #include "nfs4trace.h"
  71. #define NFSDBG_FACILITY NFSDBG_PROC
  72. #define NFS4_BITMASK_SZ 3
  73. #define NFS4_POLL_RETRY_MIN (HZ/10)
  74. #define NFS4_POLL_RETRY_MAX (15*HZ)
  75. /* file attributes which can be mapped to nfs attributes */
  76. #define NFS4_VALID_ATTRS (ATTR_MODE \
  77. | ATTR_UID \
  78. | ATTR_GID \
  79. | ATTR_SIZE \
  80. | ATTR_ATIME \
  81. | ATTR_MTIME \
  82. | ATTR_CTIME \
  83. | ATTR_ATIME_SET \
  84. | ATTR_MTIME_SET)
  85. struct nfs4_opendata;
  86. static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
  87. static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
  88. static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
  89. static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
  90. struct nfs_fattr *fattr, struct inode *inode);
  91. static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
  92. struct nfs_fattr *fattr, struct iattr *sattr,
  93. struct nfs_open_context *ctx, struct nfs4_label *ilabel);
  94. static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
  95. const struct cred *cred,
  96. struct nfs4_slot *slot,
  97. bool is_privileged);
  98. static int nfs41_test_stateid(struct nfs_server *, const nfs4_stateid *,
  99. const struct cred *);
  100. static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *,
  101. const struct cred *, bool);
  102. #ifdef CONFIG_NFS_V4_SECURITY_LABEL
  103. static inline struct nfs4_label *
  104. nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
  105. struct iattr *sattr, struct nfs4_label *label)
  106. {
  107. struct lsm_context shim;
  108. int err;
  109. if (label == NULL)
  110. return NULL;
  111. if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
  112. return NULL;
  113. label->lfs = 0;
  114. label->pi = 0;
  115. label->len = 0;
  116. label->label = NULL;
  117. err = security_dentry_init_security(dentry, sattr->ia_mode,
  118. &dentry->d_name, NULL, &shim);
  119. if (err)
  120. return NULL;
  121. label->lsmid = shim.id;
  122. label->label = shim.context;
  123. label->len = shim.len;
  124. return label;
  125. }
  126. static inline void
  127. nfs4_label_release_security(struct nfs4_label *label)
  128. {
  129. struct lsm_context shim;
  130. if (label) {
  131. shim.context = label->label;
  132. shim.len = label->len;
  133. shim.id = label->lsmid;
  134. security_release_secctx(&shim);
  135. }
  136. }
  137. static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
  138. {
  139. if (label)
  140. return server->attr_bitmask;
  141. return server->attr_bitmask_nl;
  142. }
  143. #else
  144. static inline struct nfs4_label *
  145. nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
  146. struct iattr *sattr, struct nfs4_label *l)
  147. { return NULL; }
  148. static inline void
  149. nfs4_label_release_security(struct nfs4_label *label)
  150. { return; }
  151. static inline u32 *
  152. nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
  153. { return server->attr_bitmask; }
  154. #endif
  155. /* Prevent leaks of NFSv4 errors into userland */
  156. static int nfs4_map_errors(int err)
  157. {
  158. if (err >= -1000)
  159. return err;
  160. switch (err) {
  161. case -NFS4ERR_RESOURCE:
  162. case -NFS4ERR_LAYOUTTRYLATER:
  163. case -NFS4ERR_RECALLCONFLICT:
  164. case -NFS4ERR_RETURNCONFLICT:
  165. return -EREMOTEIO;
  166. case -NFS4ERR_WRONGSEC:
  167. case -NFS4ERR_WRONG_CRED:
  168. return -EPERM;
  169. case -NFS4ERR_BADOWNER:
  170. case -NFS4ERR_BADNAME:
  171. return -EINVAL;
  172. case -NFS4ERR_SHARE_DENIED:
  173. return -EACCES;
  174. case -NFS4ERR_MINOR_VERS_MISMATCH:
  175. return -EPROTONOSUPPORT;
  176. case -NFS4ERR_FILE_OPEN:
  177. return -EBUSY;
  178. case -NFS4ERR_NOT_SAME:
  179. return -ENOTSYNC;
  180. case -ENETDOWN:
  181. case -ENETUNREACH:
  182. break;
  183. default:
  184. dprintk("%s could not handle NFSv4 error %d\n",
  185. __func__, -err);
  186. break;
  187. }
  188. return -EIO;
  189. }
  190. /*
  191. * This is our standard bitmap for GETATTR requests.
  192. */
  193. const u32 nfs4_fattr_bitmap[3] = {
  194. FATTR4_WORD0_TYPE
  195. | FATTR4_WORD0_CHANGE
  196. | FATTR4_WORD0_SIZE
  197. | FATTR4_WORD0_FSID
  198. | FATTR4_WORD0_FILEID,
  199. FATTR4_WORD1_MODE
  200. | FATTR4_WORD1_NUMLINKS
  201. | FATTR4_WORD1_OWNER
  202. | FATTR4_WORD1_OWNER_GROUP
  203. | FATTR4_WORD1_RAWDEV
  204. | FATTR4_WORD1_SPACE_USED
  205. | FATTR4_WORD1_TIME_ACCESS
  206. | FATTR4_WORD1_TIME_CREATE
  207. | FATTR4_WORD1_TIME_METADATA
  208. | FATTR4_WORD1_TIME_MODIFY
  209. | FATTR4_WORD1_MOUNTED_ON_FILEID,
  210. #ifdef CONFIG_NFS_V4_SECURITY_LABEL
  211. FATTR4_WORD2_SECURITY_LABEL
  212. #endif
  213. };
  214. static const u32 nfs4_pnfs_open_bitmap[3] = {
  215. FATTR4_WORD0_TYPE
  216. | FATTR4_WORD0_CHANGE
  217. | FATTR4_WORD0_SIZE
  218. | FATTR4_WORD0_FSID
  219. | FATTR4_WORD0_FILEID,
  220. FATTR4_WORD1_MODE
  221. | FATTR4_WORD1_NUMLINKS
  222. | FATTR4_WORD1_OWNER
  223. | FATTR4_WORD1_OWNER_GROUP
  224. | FATTR4_WORD1_RAWDEV
  225. | FATTR4_WORD1_SPACE_USED
  226. | FATTR4_WORD1_TIME_ACCESS
  227. | FATTR4_WORD1_TIME_CREATE
  228. | FATTR4_WORD1_TIME_METADATA
  229. | FATTR4_WORD1_TIME_MODIFY,
  230. FATTR4_WORD2_MDSTHRESHOLD
  231. #ifdef CONFIG_NFS_V4_SECURITY_LABEL
  232. | FATTR4_WORD2_SECURITY_LABEL
  233. #endif
  234. };
  235. static const u32 nfs4_open_noattr_bitmap[3] = {
  236. FATTR4_WORD0_TYPE
  237. | FATTR4_WORD0_FILEID,
  238. };
  239. const u32 nfs4_statfs_bitmap[3] = {
  240. FATTR4_WORD0_FILES_AVAIL
  241. | FATTR4_WORD0_FILES_FREE
  242. | FATTR4_WORD0_FILES_TOTAL,
  243. FATTR4_WORD1_SPACE_AVAIL
  244. | FATTR4_WORD1_SPACE_FREE
  245. | FATTR4_WORD1_SPACE_TOTAL
  246. };
  247. const u32 nfs4_pathconf_bitmap[3] = {
  248. FATTR4_WORD0_MAXLINK
  249. | FATTR4_WORD0_MAXNAME,
  250. 0
  251. };
  252. const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
  253. | FATTR4_WORD0_MAXREAD
  254. | FATTR4_WORD0_MAXWRITE
  255. | FATTR4_WORD0_LEASE_TIME,
  256. FATTR4_WORD1_TIME_DELTA
  257. | FATTR4_WORD1_FS_LAYOUT_TYPES,
  258. FATTR4_WORD2_LAYOUT_BLKSIZE
  259. | FATTR4_WORD2_CLONE_BLKSIZE
  260. | FATTR4_WORD2_CHANGE_ATTR_TYPE
  261. | FATTR4_WORD2_XATTR_SUPPORT
  262. };
  263. const u32 nfs4_fs_locations_bitmap[3] = {
  264. FATTR4_WORD0_CHANGE
  265. | FATTR4_WORD0_SIZE
  266. | FATTR4_WORD0_FSID
  267. | FATTR4_WORD0_FILEID
  268. | FATTR4_WORD0_FS_LOCATIONS,
  269. FATTR4_WORD1_OWNER
  270. | FATTR4_WORD1_OWNER_GROUP
  271. | FATTR4_WORD1_RAWDEV
  272. | FATTR4_WORD1_SPACE_USED
  273. | FATTR4_WORD1_TIME_ACCESS
  274. | FATTR4_WORD1_TIME_METADATA
  275. | FATTR4_WORD1_TIME_MODIFY
  276. | FATTR4_WORD1_MOUNTED_ON_FILEID,
  277. };
  278. static void nfs4_bitmap_copy_adjust(__u32 *dst, const __u32 *src,
  279. struct inode *inode, unsigned long flags)
  280. {
  281. unsigned long cache_validity;
  282. memcpy(dst, src, NFS4_BITMASK_SZ*sizeof(*dst));
  283. if (!inode || !nfs_have_read_or_write_delegation(inode))
  284. return;
  285. cache_validity = READ_ONCE(NFS_I(inode)->cache_validity) | flags;
  286. /* Remove the attributes over which we have full control */
  287. dst[1] &= ~FATTR4_WORD1_RAWDEV;
  288. if (!(cache_validity & NFS_INO_INVALID_SIZE))
  289. dst[0] &= ~FATTR4_WORD0_SIZE;
  290. if (!(cache_validity & NFS_INO_INVALID_CHANGE))
  291. dst[0] &= ~FATTR4_WORD0_CHANGE;
  292. if (!(cache_validity & NFS_INO_INVALID_MODE))
  293. dst[1] &= ~FATTR4_WORD1_MODE;
  294. if (!(cache_validity & NFS_INO_INVALID_OTHER))
  295. dst[1] &= ~(FATTR4_WORD1_OWNER | FATTR4_WORD1_OWNER_GROUP);
  296. if (!(cache_validity & NFS_INO_INVALID_BTIME))
  297. dst[1] &= ~FATTR4_WORD1_TIME_CREATE;
  298. if (nfs_have_delegated_mtime(inode)) {
  299. if (!(cache_validity & NFS_INO_INVALID_ATIME))
  300. dst[1] &= ~(FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET);
  301. if (!(cache_validity & NFS_INO_INVALID_MTIME))
  302. dst[1] &= ~(FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET);
  303. if (!(cache_validity & NFS_INO_INVALID_CTIME))
  304. dst[1] &= ~(FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY_SET);
  305. } else if (nfs_have_delegated_atime(inode)) {
  306. if (!(cache_validity & NFS_INO_INVALID_ATIME))
  307. dst[1] &= ~(FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET);
  308. }
  309. }
  310. static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
  311. struct nfs4_readdir_arg *readdir)
  312. {
  313. unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
  314. __be32 *start, *p;
  315. if (cookie > 2) {
  316. readdir->cookie = cookie;
  317. memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
  318. return;
  319. }
  320. readdir->cookie = 0;
  321. memset(&readdir->verifier, 0, sizeof(readdir->verifier));
  322. if (cookie == 2)
  323. return;
  324. /*
  325. * NFSv4 servers do not return entries for '.' and '..'
  326. * Therefore, we fake these entries here. We let '.'
  327. * have cookie 0 and '..' have cookie 1. Note that
  328. * when talking to the server, we always send cookie 0
  329. * instead of 1 or 2.
  330. */
  331. start = p = kmap_atomic(*readdir->pages);
  332. if (cookie == 0) {
  333. *p++ = xdr_one; /* next */
  334. *p++ = xdr_zero; /* cookie, first word */
  335. *p++ = xdr_one; /* cookie, second word */
  336. *p++ = xdr_one; /* entry len */
  337. memcpy(p, ".\0\0\0", 4); /* entry */
  338. p++;
  339. *p++ = xdr_one; /* bitmap length */
  340. *p++ = htonl(attrs); /* bitmap */
  341. *p++ = htonl(12); /* attribute buffer length */
  342. *p++ = htonl(NF4DIR);
  343. p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
  344. }
  345. *p++ = xdr_one; /* next */
  346. *p++ = xdr_zero; /* cookie, first word */
  347. *p++ = xdr_two; /* cookie, second word */
  348. *p++ = xdr_two; /* entry len */
  349. memcpy(p, "..\0\0", 4); /* entry */
  350. p++;
  351. *p++ = xdr_one; /* bitmap length */
  352. *p++ = htonl(attrs); /* bitmap */
  353. *p++ = htonl(12); /* attribute buffer length */
  354. *p++ = htonl(NF4DIR);
  355. spin_lock(&dentry->d_lock);
  356. p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
  357. spin_unlock(&dentry->d_lock);
  358. readdir->pgbase = (char *)p - (char *)start;
  359. readdir->count -= readdir->pgbase;
  360. kunmap_atomic(start);
  361. }
  362. static void nfs4_fattr_set_prechange(struct nfs_fattr *fattr, u64 version)
  363. {
  364. if (!(fattr->valid & NFS_ATTR_FATTR_PRECHANGE)) {
  365. fattr->pre_change_attr = version;
  366. fattr->valid |= NFS_ATTR_FATTR_PRECHANGE;
  367. }
  368. }
  369. static void nfs4_test_and_free_stateid(struct nfs_server *server,
  370. nfs4_stateid *stateid,
  371. const struct cred *cred)
  372. {
  373. const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
  374. ops->test_and_free_expired(server, stateid, cred);
  375. }
  376. static void __nfs4_free_revoked_stateid(struct nfs_server *server,
  377. nfs4_stateid *stateid,
  378. const struct cred *cred)
  379. {
  380. stateid->type = NFS4_REVOKED_STATEID_TYPE;
  381. nfs4_test_and_free_stateid(server, stateid, cred);
  382. }
  383. static void nfs4_free_revoked_stateid(struct nfs_server *server,
  384. const nfs4_stateid *stateid,
  385. const struct cred *cred)
  386. {
  387. nfs4_stateid tmp;
  388. nfs4_stateid_copy(&tmp, stateid);
  389. __nfs4_free_revoked_stateid(server, &tmp, cred);
  390. }
  391. static long nfs4_update_delay(long *timeout)
  392. {
  393. long ret;
  394. if (!timeout)
  395. return NFS4_POLL_RETRY_MAX;
  396. if (*timeout <= 0)
  397. *timeout = NFS4_POLL_RETRY_MIN;
  398. if (*timeout > NFS4_POLL_RETRY_MAX)
  399. *timeout = NFS4_POLL_RETRY_MAX;
  400. ret = *timeout;
  401. *timeout <<= 1;
  402. return ret;
  403. }
  404. static int nfs4_delay_killable(long *timeout)
  405. {
  406. might_sleep();
  407. if (unlikely(nfs_current_task_exiting()))
  408. return -EINTR;
  409. __set_current_state(TASK_KILLABLE|TASK_FREEZABLE_UNSAFE);
  410. schedule_timeout(nfs4_update_delay(timeout));
  411. if (!__fatal_signal_pending(current))
  412. return 0;
  413. return -EINTR;
  414. }
  415. static int nfs4_delay_interruptible(long *timeout)
  416. {
  417. might_sleep();
  418. if (unlikely(nfs_current_task_exiting()))
  419. return -EINTR;
  420. __set_current_state(TASK_INTERRUPTIBLE|TASK_FREEZABLE_UNSAFE);
  421. schedule_timeout(nfs4_update_delay(timeout));
  422. if (!signal_pending(current))
  423. return 0;
  424. return __fatal_signal_pending(current) ? -EINTR :-ERESTARTSYS;
  425. }
  426. static int nfs4_delay(long *timeout, bool interruptible)
  427. {
  428. if (interruptible)
  429. return nfs4_delay_interruptible(timeout);
  430. return nfs4_delay_killable(timeout);
  431. }
  432. static const nfs4_stateid *
  433. nfs4_recoverable_stateid(const nfs4_stateid *stateid)
  434. {
  435. if (!stateid)
  436. return NULL;
  437. switch (stateid->type) {
  438. case NFS4_OPEN_STATEID_TYPE:
  439. case NFS4_LOCK_STATEID_TYPE:
  440. case NFS4_DELEGATION_STATEID_TYPE:
  441. return stateid;
  442. default:
  443. break;
  444. }
  445. return NULL;
  446. }
  447. /* This is the error handling routine for processes that are allowed
  448. * to sleep.
  449. */
  450. static int nfs4_do_handle_exception(struct nfs_server *server,
  451. int errorcode, struct nfs4_exception *exception)
  452. {
  453. struct nfs_client *clp = server->nfs_client;
  454. struct nfs4_state *state = exception->state;
  455. const nfs4_stateid *stateid;
  456. struct inode *inode = exception->inode;
  457. int ret = errorcode;
  458. exception->delay = 0;
  459. exception->recovering = 0;
  460. exception->retry = 0;
  461. stateid = nfs4_recoverable_stateid(exception->stateid);
  462. if (stateid == NULL && state != NULL)
  463. stateid = nfs4_recoverable_stateid(&state->stateid);
  464. switch(errorcode) {
  465. case 0:
  466. return 0;
  467. case -NFS4ERR_BADHANDLE:
  468. case -ESTALE:
  469. if (inode != NULL && S_ISREG(inode->i_mode))
  470. pnfs_destroy_layout(NFS_I(inode));
  471. break;
  472. case -NFS4ERR_DELEG_REVOKED:
  473. case -NFS4ERR_ADMIN_REVOKED:
  474. case -NFS4ERR_EXPIRED:
  475. case -NFS4ERR_BAD_STATEID:
  476. case -NFS4ERR_PARTNER_NO_AUTH:
  477. if (inode != NULL && stateid != NULL) {
  478. nfs_inode_find_state_and_recover(inode,
  479. stateid);
  480. goto wait_on_recovery;
  481. }
  482. fallthrough;
  483. case -NFS4ERR_OPENMODE:
  484. if (inode) {
  485. int err;
  486. err = nfs_async_inode_return_delegation(inode,
  487. stateid);
  488. if (err == 0)
  489. goto wait_on_recovery;
  490. if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
  491. exception->retry = 1;
  492. break;
  493. }
  494. }
  495. if (state == NULL)
  496. break;
  497. ret = nfs4_schedule_stateid_recovery(server, state);
  498. if (ret < 0)
  499. break;
  500. goto wait_on_recovery;
  501. case -NFS4ERR_STALE_STATEID:
  502. case -NFS4ERR_STALE_CLIENTID:
  503. nfs4_schedule_lease_recovery(clp);
  504. goto wait_on_recovery;
  505. case -NFS4ERR_MOVED:
  506. ret = nfs4_schedule_migration_recovery(server);
  507. if (ret < 0)
  508. break;
  509. goto wait_on_recovery;
  510. case -NFS4ERR_LEASE_MOVED:
  511. nfs4_schedule_lease_moved_recovery(clp);
  512. goto wait_on_recovery;
  513. case -NFS4ERR_BADSESSION:
  514. case -NFS4ERR_BADSLOT:
  515. case -NFS4ERR_BAD_HIGH_SLOT:
  516. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  517. case -NFS4ERR_DEADSESSION:
  518. case -NFS4ERR_SEQ_FALSE_RETRY:
  519. case -NFS4ERR_SEQ_MISORDERED:
  520. /* Handled in nfs41_sequence_process() */
  521. goto wait_on_recovery;
  522. case -NFS4ERR_FILE_OPEN:
  523. if (exception->timeout > HZ) {
  524. /* We have retried a decent amount, time to
  525. * fail
  526. */
  527. ret = -EBUSY;
  528. break;
  529. }
  530. fallthrough;
  531. case -NFS4ERR_DELAY:
  532. nfs_inc_server_stats(server, NFSIOS_DELAY);
  533. fallthrough;
  534. case -NFS4ERR_GRACE:
  535. case -NFS4ERR_LAYOUTTRYLATER:
  536. case -NFS4ERR_RECALLCONFLICT:
  537. case -NFS4ERR_RETURNCONFLICT:
  538. exception->delay = 1;
  539. return 0;
  540. case -NFS4ERR_RETRY_UNCACHED_REP:
  541. case -NFS4ERR_OLD_STATEID:
  542. exception->retry = 1;
  543. break;
  544. case -NFS4ERR_BADOWNER:
  545. /* The following works around a Linux server bug! */
  546. case -NFS4ERR_BADNAME:
  547. if (server->caps & NFS_CAP_UIDGID_NOMAP) {
  548. server->caps &= ~NFS_CAP_UIDGID_NOMAP;
  549. exception->retry = 1;
  550. printk(KERN_WARNING "NFS: v4 server %s "
  551. "does not accept raw "
  552. "uid/gids. "
  553. "Reenabling the idmapper.\n",
  554. server->nfs_client->cl_hostname);
  555. }
  556. }
  557. /* We failed to handle the error */
  558. return nfs4_map_errors(ret);
  559. wait_on_recovery:
  560. exception->recovering = 1;
  561. return 0;
  562. }
  563. /*
  564. * Track the number of NFS4ERR_DELAY related retransmissions and return
  565. * EAGAIN if the 'softerr' mount option is set, and we've exceeded the limit
  566. * set by 'nfs_delay_retrans'.
  567. */
  568. static int nfs4_exception_should_retrans(const struct nfs_server *server,
  569. struct nfs4_exception *exception)
  570. {
  571. if (server->flags & NFS_MOUNT_SOFTERR && nfs_delay_retrans >= 0) {
  572. if (exception->retrans++ >= (unsigned short)nfs_delay_retrans)
  573. return -EAGAIN;
  574. }
  575. return 0;
  576. }
  577. /* This is the error handling routine for processes that are allowed
  578. * to sleep.
  579. */
  580. int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
  581. {
  582. struct nfs_client *clp = server->nfs_client;
  583. int ret;
  584. ret = nfs4_do_handle_exception(server, errorcode, exception);
  585. if (exception->delay) {
  586. int ret2 = nfs4_exception_should_retrans(server, exception);
  587. if (ret2 < 0) {
  588. exception->retry = 0;
  589. return ret2;
  590. }
  591. ret = nfs4_delay(&exception->timeout,
  592. exception->interruptible);
  593. goto out_retry;
  594. }
  595. if (exception->recovering) {
  596. if (exception->task_is_privileged)
  597. return -EDEADLOCK;
  598. ret = nfs4_wait_clnt_recover(clp);
  599. if (test_bit(NFS_MIG_FAILED, &server->mig_status))
  600. return -EIO;
  601. goto out_retry;
  602. }
  603. return ret;
  604. out_retry:
  605. if (ret == 0)
  606. exception->retry = 1;
  607. return ret;
  608. }
  609. static int
  610. nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
  611. int errorcode, struct nfs4_exception *exception)
  612. {
  613. struct nfs_client *clp = server->nfs_client;
  614. int ret;
  615. if ((task->tk_rpc_status == -ENETDOWN ||
  616. task->tk_rpc_status == -ENETUNREACH) &&
  617. task->tk_flags & RPC_TASK_NETUNREACH_FATAL) {
  618. exception->delay = 0;
  619. exception->recovering = 0;
  620. exception->retry = 0;
  621. return -EIO;
  622. }
  623. ret = nfs4_do_handle_exception(server, errorcode, exception);
  624. if (exception->delay) {
  625. int ret2 = nfs4_exception_should_retrans(server, exception);
  626. if (ret2 < 0) {
  627. exception->retry = 0;
  628. return ret2;
  629. }
  630. rpc_delay(task, nfs4_update_delay(&exception->timeout));
  631. goto out_retry;
  632. }
  633. if (exception->recovering) {
  634. if (exception->task_is_privileged)
  635. return -EDEADLOCK;
  636. rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
  637. if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
  638. rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
  639. goto out_retry;
  640. }
  641. if (test_bit(NFS_MIG_FAILED, &server->mig_status))
  642. ret = -EIO;
  643. return ret;
  644. out_retry:
  645. if (ret == 0) {
  646. exception->retry = 1;
  647. /*
  648. * For NFS4ERR_MOVED, the client transport will need to
  649. * be recomputed after migration recovery has completed.
  650. */
  651. if (errorcode == -NFS4ERR_MOVED)
  652. rpc_task_release_transport(task);
  653. }
  654. return ret;
  655. }
  656. int
  657. nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
  658. struct nfs4_state *state, long *timeout)
  659. {
  660. struct nfs4_exception exception = {
  661. .state = state,
  662. };
  663. if (task->tk_status >= 0)
  664. return 0;
  665. if (timeout)
  666. exception.timeout = *timeout;
  667. task->tk_status = nfs4_async_handle_exception(task, server,
  668. task->tk_status,
  669. &exception);
  670. if (exception.delay && timeout)
  671. *timeout = exception.timeout;
  672. if (exception.retry)
  673. return -EAGAIN;
  674. return 0;
  675. }
  676. /*
  677. * Return 'true' if 'clp' is using an rpc_client that is integrity protected
  678. * or 'false' otherwise.
  679. */
  680. static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
  681. {
  682. rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
  683. return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
  684. }
  685. void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
  686. {
  687. spin_lock(&clp->cl_lock);
  688. if (time_before(clp->cl_last_renewal,timestamp))
  689. clp->cl_last_renewal = timestamp;
  690. spin_unlock(&clp->cl_lock);
  691. }
  692. void renew_lease(const struct nfs_server *server, unsigned long timestamp)
  693. {
  694. struct nfs_client *clp = server->nfs_client;
  695. if (!nfs4_has_session(clp))
  696. do_renew_lease(clp, timestamp);
  697. }
  698. void nfs4_init_sequence(struct nfs_client *clp,
  699. struct nfs4_sequence_args *args,
  700. struct nfs4_sequence_res *res, int cache_reply,
  701. int privileged)
  702. {
  703. args->sa_slot = NULL;
  704. args->sa_cache_this = cache_reply;
  705. args->sa_privileged = privileged;
  706. res->sr_slot = NULL;
  707. res->sr_slot_ops = clp->cl_mvops->sequence_slot_ops;
  708. }
  709. static void nfs41_release_slot(struct nfs4_slot *slot)
  710. {
  711. struct nfs4_session *session;
  712. struct nfs4_slot_table *tbl;
  713. bool send_new_highest_used_slotid = false;
  714. if (!slot)
  715. return;
  716. tbl = slot->table;
  717. session = tbl->session;
  718. /* Bump the slot sequence number */
  719. if (slot->seq_done)
  720. slot->seq_nr++;
  721. slot->seq_done = 0;
  722. spin_lock(&tbl->slot_tbl_lock);
  723. /* Be nice to the server: try to ensure that the last transmitted
  724. * value for highest_user_slotid <= target_highest_slotid
  725. */
  726. if (tbl->highest_used_slotid > tbl->target_highest_slotid)
  727. send_new_highest_used_slotid = true;
  728. if (nfs41_wake_and_assign_slot(tbl, slot)) {
  729. send_new_highest_used_slotid = false;
  730. goto out_unlock;
  731. }
  732. nfs4_free_slot(tbl, slot);
  733. if (tbl->highest_used_slotid != NFS4_NO_SLOT)
  734. send_new_highest_used_slotid = false;
  735. out_unlock:
  736. spin_unlock(&tbl->slot_tbl_lock);
  737. if (send_new_highest_used_slotid)
  738. nfs41_notify_server(session->clp);
  739. if (waitqueue_active(&tbl->slot_waitq))
  740. wake_up_all(&tbl->slot_waitq);
  741. }
  742. static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
  743. {
  744. nfs41_release_slot(res->sr_slot);
  745. res->sr_slot = NULL;
  746. }
  747. static void nfs4_slot_sequence_record_sent(struct nfs4_slot *slot,
  748. u32 seqnr)
  749. {
  750. if ((s32)(seqnr - slot->seq_nr_highest_sent) > 0)
  751. slot->seq_nr_highest_sent = seqnr;
  752. }
  753. static void nfs4_slot_sequence_acked(struct nfs4_slot *slot, u32 seqnr)
  754. {
  755. nfs4_slot_sequence_record_sent(slot, seqnr);
  756. slot->seq_nr_last_acked = seqnr;
  757. }
  758. static void nfs4_probe_sequence(struct nfs_client *client, const struct cred *cred,
  759. struct nfs4_slot *slot)
  760. {
  761. struct rpc_task *task = _nfs41_proc_sequence(client, cred, slot, true);
  762. if (!IS_ERR(task))
  763. rpc_put_task_async(task);
  764. }
  765. static int nfs41_sequence_process(struct rpc_task *task,
  766. struct nfs4_sequence_res *res)
  767. {
  768. struct nfs4_session *session;
  769. struct nfs4_slot *slot = res->sr_slot;
  770. struct nfs_client *clp;
  771. int status;
  772. int ret = 1;
  773. if (slot == NULL)
  774. goto out_noaction;
  775. /* don't increment the sequence number if the task wasn't sent */
  776. if (!RPC_WAS_SENT(task) || slot->seq_done)
  777. goto out;
  778. session = slot->table->session;
  779. clp = session->clp;
  780. trace_nfs4_sequence_done(session, res);
  781. status = res->sr_status;
  782. if (task->tk_status == -NFS4ERR_DEADSESSION)
  783. status = -NFS4ERR_DEADSESSION;
  784. /* Check the SEQUENCE operation status */
  785. switch (status) {
  786. case 0:
  787. /* Mark this sequence number as having been acked */
  788. nfs4_slot_sequence_acked(slot, slot->seq_nr);
  789. /* Update the slot's sequence and clientid lease timer */
  790. slot->seq_done = 1;
  791. do_renew_lease(clp, res->sr_timestamp);
  792. /* Check sequence flags */
  793. nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
  794. !!slot->privileged);
  795. nfs41_update_target_slotid(slot->table, slot, res);
  796. break;
  797. case 1:
  798. /*
  799. * sr_status remains 1 if an RPC level error occurred.
  800. * The server may or may not have processed the sequence
  801. * operation..
  802. */
  803. nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
  804. slot->seq_done = 1;
  805. goto out;
  806. case -NFS4ERR_DELAY:
  807. /* The server detected a resend of the RPC call and
  808. * returned NFS4ERR_DELAY as per Section 2.10.6.2
  809. * of RFC5661.
  810. */
  811. dprintk("%s: slot=%u seq=%u: Operation in progress\n",
  812. __func__,
  813. slot->slot_nr,
  814. slot->seq_nr);
  815. goto out_retry;
  816. case -NFS4ERR_RETRY_UNCACHED_REP:
  817. case -NFS4ERR_SEQ_FALSE_RETRY:
  818. /*
  819. * The server thinks we tried to replay a request.
  820. * Retry the call after bumping the sequence ID.
  821. */
  822. nfs4_slot_sequence_acked(slot, slot->seq_nr);
  823. goto retry_new_seq;
  824. case -NFS4ERR_BADSLOT:
  825. /*
  826. * The slot id we used was probably retired. Try again
  827. * using a different slot id.
  828. */
  829. if (slot->slot_nr < slot->table->target_highest_slotid)
  830. goto session_recover;
  831. goto retry_nowait;
  832. case -NFS4ERR_SEQ_MISORDERED:
  833. nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
  834. /*
  835. * Were one or more calls using this slot interrupted?
  836. * If the server never received the request, then our
  837. * transmitted slot sequence number may be too high. However,
  838. * if the server did receive the request then it might
  839. * accidentally give us a reply with a mismatched operation.
  840. * We can sort this out by sending a lone sequence operation
  841. * to the server on the same slot.
  842. */
  843. if ((s32)(slot->seq_nr - slot->seq_nr_last_acked) > 1) {
  844. slot->seq_nr--;
  845. if (task->tk_msg.rpc_proc != &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE]) {
  846. nfs4_probe_sequence(clp, task->tk_msg.rpc_cred, slot);
  847. res->sr_slot = NULL;
  848. }
  849. goto retry_nowait;
  850. }
  851. /*
  852. * RFC5661:
  853. * A retry might be sent while the original request is
  854. * still in progress on the replier. The replier SHOULD
  855. * deal with the issue by returning NFS4ERR_DELAY as the
  856. * reply to SEQUENCE or CB_SEQUENCE operation, but
  857. * implementations MAY return NFS4ERR_SEQ_MISORDERED.
  858. *
  859. * Restart the search after a delay.
  860. */
  861. slot->seq_nr = slot->seq_nr_highest_sent;
  862. goto out_retry;
  863. case -NFS4ERR_BADSESSION:
  864. case -NFS4ERR_DEADSESSION:
  865. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  866. goto session_recover;
  867. default:
  868. /* Just update the slot sequence no. */
  869. slot->seq_done = 1;
  870. }
  871. out:
  872. /* The session may be reset by one of the error handlers. */
  873. dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
  874. out_noaction:
  875. return ret;
  876. session_recover:
  877. set_bit(NFS4_SLOT_TBL_DRAINING, &session->fc_slot_table.slot_tbl_state);
  878. nfs4_schedule_session_recovery(session, status);
  879. dprintk("%s ERROR: %d Reset session\n", __func__, status);
  880. nfs41_sequence_free_slot(res);
  881. goto out;
  882. retry_new_seq:
  883. ++slot->seq_nr;
  884. retry_nowait:
  885. if (rpc_restart_call_prepare(task)) {
  886. nfs41_sequence_free_slot(res);
  887. task->tk_status = 0;
  888. ret = 0;
  889. }
  890. goto out;
  891. out_retry:
  892. if (!rpc_restart_call(task))
  893. goto out;
  894. rpc_delay(task, NFS4_POLL_RETRY_MAX);
  895. return 0;
  896. }
  897. int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
  898. {
  899. if (!nfs41_sequence_process(task, res))
  900. return 0;
  901. if (res->sr_slot != NULL)
  902. nfs41_sequence_free_slot(res);
  903. return 1;
  904. }
  905. EXPORT_SYMBOL_GPL(nfs41_sequence_done);
  906. static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
  907. {
  908. struct nfs4_call_sync_data *data = calldata;
  909. dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
  910. nfs4_setup_sequence(data->seq_server->nfs_client,
  911. data->seq_args, data->seq_res, task);
  912. }
  913. static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
  914. {
  915. struct nfs4_call_sync_data *data = calldata;
  916. nfs41_sequence_done(task, data->seq_res);
  917. }
  918. static const struct rpc_call_ops nfs41_call_sync_ops = {
  919. .rpc_call_prepare = nfs41_call_sync_prepare,
  920. .rpc_call_done = nfs41_call_sync_done,
  921. };
  922. static void nfs41_sequence_res_init(struct nfs4_sequence_res *res)
  923. {
  924. res->sr_timestamp = jiffies;
  925. res->sr_status_flags = 0;
  926. res->sr_status = 1;
  927. }
  928. static
  929. void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
  930. struct nfs4_sequence_res *res,
  931. struct nfs4_slot *slot)
  932. {
  933. if (!slot)
  934. return;
  935. slot->privileged = args->sa_privileged ? 1 : 0;
  936. args->sa_slot = slot;
  937. res->sr_slot = slot;
  938. }
  939. static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
  940. {
  941. if (res->sr_slot != NULL)
  942. res->sr_slot_ops->free_slot(res);
  943. }
  944. static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
  945. {
  946. if (res->sr_slot == NULL)
  947. return 1;
  948. return res->sr_slot_ops->process(task, res);
  949. }
  950. int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
  951. {
  952. if (res->sr_slot == NULL)
  953. return 1;
  954. return res->sr_slot_ops->done(task, res);
  955. }
  956. EXPORT_SYMBOL_GPL(nfs4_sequence_done);
  957. int nfs4_setup_sequence(struct nfs_client *client,
  958. struct nfs4_sequence_args *args,
  959. struct nfs4_sequence_res *res,
  960. struct rpc_task *task)
  961. {
  962. struct nfs4_session *session = nfs4_get_session(client);
  963. struct nfs4_slot_table *tbl = client->cl_slot_tbl;
  964. struct nfs4_slot *slot;
  965. /* slot already allocated? */
  966. if (res->sr_slot != NULL)
  967. goto out_start;
  968. if (session)
  969. tbl = &session->fc_slot_table;
  970. spin_lock(&tbl->slot_tbl_lock);
  971. /* The state manager will wait until the slot table is empty */
  972. if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
  973. goto out_sleep;
  974. slot = nfs4_alloc_slot(tbl);
  975. if (IS_ERR(slot)) {
  976. if (slot == ERR_PTR(-ENOMEM))
  977. goto out_sleep_timeout;
  978. goto out_sleep;
  979. }
  980. spin_unlock(&tbl->slot_tbl_lock);
  981. nfs4_sequence_attach_slot(args, res, slot);
  982. trace_nfs4_setup_sequence(session, args);
  983. out_start:
  984. nfs41_sequence_res_init(res);
  985. rpc_call_start(task);
  986. return 0;
  987. out_sleep_timeout:
  988. /* Try again in 1/4 second */
  989. if (args->sa_privileged)
  990. rpc_sleep_on_priority_timeout(&tbl->slot_tbl_waitq, task,
  991. jiffies + (HZ >> 2), RPC_PRIORITY_PRIVILEGED);
  992. else
  993. rpc_sleep_on_timeout(&tbl->slot_tbl_waitq, task,
  994. NULL, jiffies + (HZ >> 2));
  995. spin_unlock(&tbl->slot_tbl_lock);
  996. return -EAGAIN;
  997. out_sleep:
  998. if (args->sa_privileged)
  999. rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
  1000. RPC_PRIORITY_PRIVILEGED);
  1001. else
  1002. rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
  1003. spin_unlock(&tbl->slot_tbl_lock);
  1004. return -EAGAIN;
  1005. }
  1006. EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
  1007. static int nfs4_call_sync_custom(struct rpc_task_setup *task_setup)
  1008. {
  1009. int ret;
  1010. struct rpc_task *task;
  1011. task = rpc_run_task(task_setup);
  1012. if (IS_ERR(task))
  1013. return PTR_ERR(task);
  1014. ret = task->tk_status;
  1015. rpc_put_task(task);
  1016. return ret;
  1017. }
  1018. static int nfs4_do_call_sync(struct rpc_clnt *clnt,
  1019. struct nfs_server *server,
  1020. struct rpc_message *msg,
  1021. struct nfs4_sequence_args *args,
  1022. struct nfs4_sequence_res *res,
  1023. unsigned short task_flags)
  1024. {
  1025. struct nfs_client *clp = server->nfs_client;
  1026. struct nfs4_call_sync_data data = {
  1027. .seq_server = server,
  1028. .seq_args = args,
  1029. .seq_res = res,
  1030. };
  1031. struct rpc_task_setup task_setup = {
  1032. .rpc_client = clnt,
  1033. .rpc_message = msg,
  1034. .callback_ops = clp->cl_mvops->call_sync_ops,
  1035. .callback_data = &data,
  1036. .flags = task_flags,
  1037. };
  1038. return nfs4_call_sync_custom(&task_setup);
  1039. }
  1040. int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
  1041. struct nfs_server *server,
  1042. struct rpc_message *msg,
  1043. struct nfs4_sequence_args *args,
  1044. struct nfs4_sequence_res *res)
  1045. {
  1046. unsigned short task_flags = 0;
  1047. if (server->caps & NFS_CAP_MOVEABLE)
  1048. task_flags = RPC_TASK_MOVEABLE;
  1049. return nfs4_do_call_sync(clnt, server, msg, args, res, task_flags);
  1050. }
  1051. int nfs4_call_sync(struct rpc_clnt *clnt,
  1052. struct nfs_server *server,
  1053. struct rpc_message *msg,
  1054. struct nfs4_sequence_args *args,
  1055. struct nfs4_sequence_res *res,
  1056. int cache_reply)
  1057. {
  1058. nfs4_init_sequence(server->nfs_client, args, res, cache_reply, 0);
  1059. return nfs4_call_sync_sequence(clnt, server, msg, args, res);
  1060. }
  1061. static void
  1062. nfs4_inc_nlink_locked(struct inode *inode)
  1063. {
  1064. nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
  1065. NFS_INO_INVALID_CTIME |
  1066. NFS_INO_INVALID_NLINK);
  1067. inc_nlink(inode);
  1068. }
  1069. static void
  1070. nfs4_inc_nlink(struct inode *inode)
  1071. {
  1072. spin_lock(&inode->i_lock);
  1073. nfs4_inc_nlink_locked(inode);
  1074. spin_unlock(&inode->i_lock);
  1075. }
  1076. static void
  1077. nfs4_dec_nlink_locked(struct inode *inode)
  1078. {
  1079. nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
  1080. NFS_INO_INVALID_CTIME |
  1081. NFS_INO_INVALID_NLINK);
  1082. drop_nlink(inode);
  1083. }
  1084. static void
  1085. nfs4_update_changeattr_locked(struct inode *inode,
  1086. struct nfs4_change_info *cinfo,
  1087. unsigned long timestamp, unsigned long cache_validity)
  1088. {
  1089. struct nfs_inode *nfsi = NFS_I(inode);
  1090. u64 change_attr = inode_peek_iversion_raw(inode);
  1091. if (!nfs_have_delegated_mtime(inode))
  1092. cache_validity |= NFS_INO_INVALID_CTIME | NFS_INO_INVALID_MTIME;
  1093. if (S_ISDIR(inode->i_mode))
  1094. cache_validity |= NFS_INO_INVALID_DATA;
  1095. switch (NFS_SERVER(inode)->change_attr_type) {
  1096. case NFS4_CHANGE_TYPE_IS_UNDEFINED:
  1097. if (cinfo->after == change_attr)
  1098. goto out;
  1099. break;
  1100. default:
  1101. if ((s64)(change_attr - cinfo->after) >= 0)
  1102. goto out;
  1103. }
  1104. inode_set_iversion_raw(inode, cinfo->after);
  1105. if (!cinfo->atomic || cinfo->before != change_attr) {
  1106. if (S_ISDIR(inode->i_mode))
  1107. nfs_force_lookup_revalidate(inode);
  1108. if (!nfs_have_delegated_attributes(inode))
  1109. cache_validity |=
  1110. NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL |
  1111. NFS_INO_INVALID_SIZE | NFS_INO_INVALID_OTHER |
  1112. NFS_INO_INVALID_BLOCKS | NFS_INO_INVALID_NLINK |
  1113. NFS_INO_INVALID_MODE | NFS_INO_INVALID_BTIME |
  1114. NFS_INO_INVALID_XATTR;
  1115. nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
  1116. }
  1117. nfsi->attrtimeo_timestamp = jiffies;
  1118. nfsi->read_cache_jiffies = timestamp;
  1119. nfsi->attr_gencount = nfs_inc_attr_generation_counter();
  1120. nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
  1121. out:
  1122. nfs_set_cache_invalid(inode, cache_validity);
  1123. }
  1124. void
  1125. nfs4_update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
  1126. unsigned long timestamp, unsigned long cache_validity)
  1127. {
  1128. spin_lock(&dir->i_lock);
  1129. nfs4_update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
  1130. spin_unlock(&dir->i_lock);
  1131. }
  1132. struct nfs4_open_createattrs {
  1133. struct nfs4_label *label;
  1134. struct iattr *sattr;
  1135. const __u32 verf[2];
  1136. };
  1137. static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
  1138. int err, struct nfs4_exception *exception)
  1139. {
  1140. if (err != -EINVAL)
  1141. return false;
  1142. if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
  1143. return false;
  1144. server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
  1145. exception->retry = 1;
  1146. return true;
  1147. }
  1148. static fmode_t _nfs4_ctx_to_accessmode(const struct nfs_open_context *ctx)
  1149. {
  1150. return ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
  1151. }
  1152. static fmode_t _nfs4_ctx_to_openmode(const struct nfs_open_context *ctx)
  1153. {
  1154. fmode_t ret = ctx->mode & (FMODE_READ|FMODE_WRITE);
  1155. return (ctx->mode & FMODE_EXEC) ? FMODE_READ | ret : ret;
  1156. }
  1157. static u32
  1158. nfs4_fmode_to_share_access(fmode_t fmode)
  1159. {
  1160. u32 res = 0;
  1161. switch (fmode & (FMODE_READ | FMODE_WRITE)) {
  1162. case FMODE_READ:
  1163. res = NFS4_SHARE_ACCESS_READ;
  1164. break;
  1165. case FMODE_WRITE:
  1166. res = NFS4_SHARE_ACCESS_WRITE;
  1167. break;
  1168. case FMODE_READ|FMODE_WRITE:
  1169. res = NFS4_SHARE_ACCESS_BOTH;
  1170. }
  1171. return res;
  1172. }
  1173. static u32
  1174. nfs4_map_atomic_open_share(struct nfs_server *server,
  1175. fmode_t fmode, int openflags)
  1176. {
  1177. u32 res = nfs4_fmode_to_share_access(fmode);
  1178. if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
  1179. goto out;
  1180. /* Want no delegation if we're using O_DIRECT */
  1181. if (openflags & O_DIRECT) {
  1182. res |= NFS4_SHARE_WANT_NO_DELEG;
  1183. goto out;
  1184. }
  1185. /* res |= NFS4_SHARE_WANT_NO_PREFERENCE; */
  1186. if (server->caps & NFS_CAP_DELEGTIME)
  1187. res |= NFS4_SHARE_WANT_DELEG_TIMESTAMPS;
  1188. if (server->caps & NFS_CAP_OPEN_XOR)
  1189. res |= NFS4_SHARE_WANT_OPEN_XOR_DELEGATION;
  1190. out:
  1191. return res;
  1192. }
  1193. static enum open_claim_type4
  1194. nfs4_map_atomic_open_claim(struct nfs_server *server,
  1195. enum open_claim_type4 claim)
  1196. {
  1197. if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
  1198. return claim;
  1199. switch (claim) {
  1200. default:
  1201. return claim;
  1202. case NFS4_OPEN_CLAIM_FH:
  1203. return NFS4_OPEN_CLAIM_NULL;
  1204. case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
  1205. return NFS4_OPEN_CLAIM_DELEGATE_CUR;
  1206. case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
  1207. return NFS4_OPEN_CLAIM_DELEGATE_PREV;
  1208. }
  1209. }
  1210. static void nfs4_init_opendata_res(struct nfs4_opendata *p)
  1211. {
  1212. p->o_res.f_attr = &p->f_attr;
  1213. p->o_res.seqid = p->o_arg.seqid;
  1214. p->c_res.seqid = p->c_arg.seqid;
  1215. p->o_res.server = p->o_arg.server;
  1216. p->o_res.access_request = p->o_arg.access;
  1217. nfs_fattr_init(&p->f_attr);
  1218. nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
  1219. }
  1220. static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
  1221. struct nfs4_state_owner *sp, fmode_t fmode, int flags,
  1222. const struct nfs4_open_createattrs *c,
  1223. enum open_claim_type4 claim,
  1224. gfp_t gfp_mask)
  1225. {
  1226. struct dentry *parent = dget_parent(dentry);
  1227. struct inode *dir = d_inode(parent);
  1228. struct nfs_server *server = NFS_SERVER(dir);
  1229. struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
  1230. struct nfs4_label *label = (c != NULL) ? c->label : NULL;
  1231. struct nfs4_opendata *p;
  1232. p = kzalloc_obj(*p, gfp_mask);
  1233. if (p == NULL)
  1234. goto err;
  1235. p->f_attr.label = nfs4_label_alloc(server, gfp_mask);
  1236. if (IS_ERR(p->f_attr.label))
  1237. goto err_free_p;
  1238. p->a_label = nfs4_label_alloc(server, gfp_mask);
  1239. if (IS_ERR(p->a_label))
  1240. goto err_free_f;
  1241. alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
  1242. p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
  1243. if (IS_ERR(p->o_arg.seqid))
  1244. goto err_free_label;
  1245. nfs_sb_active(dentry->d_sb);
  1246. p->dentry = dget(dentry);
  1247. p->dir = parent;
  1248. p->owner = sp;
  1249. atomic_inc(&sp->so_count);
  1250. p->o_arg.open_flags = flags;
  1251. p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
  1252. p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
  1253. p->o_arg.share_access = nfs4_map_atomic_open_share(server,
  1254. fmode, flags);
  1255. if (flags & O_CREAT) {
  1256. p->o_arg.umask = current_umask();
  1257. p->o_arg.label = nfs4_label_copy(p->a_label, label);
  1258. if (c->sattr != NULL && c->sattr->ia_valid != 0) {
  1259. p->o_arg.u.attrs = &p->attrs;
  1260. memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
  1261. memcpy(p->o_arg.u.verifier.data, c->verf,
  1262. sizeof(p->o_arg.u.verifier.data));
  1263. }
  1264. }
  1265. /* ask server to check for all possible rights as results
  1266. * are cached */
  1267. switch (p->o_arg.claim) {
  1268. default:
  1269. break;
  1270. case NFS4_OPEN_CLAIM_NULL:
  1271. case NFS4_OPEN_CLAIM_FH:
  1272. p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
  1273. NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE |
  1274. NFS4_ACCESS_EXECUTE |
  1275. nfs_access_xattr_mask(server);
  1276. }
  1277. p->o_arg.clientid = server->nfs_client->cl_clientid;
  1278. p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
  1279. p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
  1280. p->o_arg.name = &dentry->d_name;
  1281. p->o_arg.server = server;
  1282. p->o_arg.bitmask = nfs4_bitmask(server, label);
  1283. p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
  1284. switch (p->o_arg.claim) {
  1285. case NFS4_OPEN_CLAIM_NULL:
  1286. case NFS4_OPEN_CLAIM_DELEGATE_CUR:
  1287. case NFS4_OPEN_CLAIM_DELEGATE_PREV:
  1288. p->o_arg.fh = NFS_FH(dir);
  1289. break;
  1290. case NFS4_OPEN_CLAIM_PREVIOUS:
  1291. case NFS4_OPEN_CLAIM_FH:
  1292. case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
  1293. case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
  1294. p->o_arg.fh = NFS_FH(d_inode(dentry));
  1295. }
  1296. p->c_arg.fh = &p->o_res.fh;
  1297. p->c_arg.stateid = &p->o_res.stateid;
  1298. p->c_arg.seqid = p->o_arg.seqid;
  1299. nfs4_init_opendata_res(p);
  1300. kref_init(&p->kref);
  1301. return p;
  1302. err_free_label:
  1303. nfs4_label_free(p->a_label);
  1304. err_free_f:
  1305. nfs4_label_free(p->f_attr.label);
  1306. err_free_p:
  1307. kfree(p);
  1308. err:
  1309. dput(parent);
  1310. return NULL;
  1311. }
  1312. static void nfs4_opendata_free(struct kref *kref)
  1313. {
  1314. struct nfs4_opendata *p = container_of(kref,
  1315. struct nfs4_opendata, kref);
  1316. struct super_block *sb = p->dentry->d_sb;
  1317. nfs4_lgopen_release(p->lgp);
  1318. nfs_free_seqid(p->o_arg.seqid);
  1319. nfs4_sequence_free_slot(&p->o_res.seq_res);
  1320. if (p->state != NULL)
  1321. nfs4_put_open_state(p->state);
  1322. nfs4_put_state_owner(p->owner);
  1323. nfs4_label_free(p->a_label);
  1324. nfs4_label_free(p->f_attr.label);
  1325. dput(p->dir);
  1326. dput(p->dentry);
  1327. nfs_sb_deactive(sb);
  1328. nfs_fattr_free_names(&p->f_attr);
  1329. kfree(p->f_attr.mdsthreshold);
  1330. kfree(p);
  1331. }
  1332. static void nfs4_opendata_put(struct nfs4_opendata *p)
  1333. {
  1334. if (p != NULL)
  1335. kref_put(&p->kref, nfs4_opendata_free);
  1336. }
  1337. static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
  1338. fmode_t fmode)
  1339. {
  1340. switch(fmode & (FMODE_READ|FMODE_WRITE)) {
  1341. case FMODE_READ|FMODE_WRITE:
  1342. return state->n_rdwr != 0;
  1343. case FMODE_WRITE:
  1344. return state->n_wronly != 0;
  1345. case FMODE_READ:
  1346. return state->n_rdonly != 0;
  1347. }
  1348. WARN_ON_ONCE(1);
  1349. return false;
  1350. }
  1351. static int can_open_cached(struct nfs4_state *state, fmode_t mode,
  1352. int open_mode, enum open_claim_type4 claim)
  1353. {
  1354. int ret = 0;
  1355. if (open_mode & (O_EXCL|O_TRUNC))
  1356. goto out;
  1357. switch (claim) {
  1358. case NFS4_OPEN_CLAIM_NULL:
  1359. case NFS4_OPEN_CLAIM_FH:
  1360. goto out;
  1361. default:
  1362. break;
  1363. }
  1364. switch (mode & (FMODE_READ|FMODE_WRITE)) {
  1365. case FMODE_READ:
  1366. ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
  1367. && state->n_rdonly != 0;
  1368. break;
  1369. case FMODE_WRITE:
  1370. ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
  1371. && state->n_wronly != 0;
  1372. break;
  1373. case FMODE_READ|FMODE_WRITE:
  1374. ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
  1375. && state->n_rdwr != 0;
  1376. }
  1377. out:
  1378. return ret;
  1379. }
  1380. static bool can_open_delegated(const struct inode *inode, fmode_t fmode,
  1381. enum open_claim_type4 claim, nfs4_stateid *stateid)
  1382. {
  1383. struct nfs_delegation *delegation;
  1384. bool ret = false;
  1385. delegation = nfs4_get_valid_delegation(inode);
  1386. if (!delegation)
  1387. return false;
  1388. if ((delegation->type & fmode) != fmode)
  1389. goto out_put_delegation;
  1390. switch (claim) {
  1391. case NFS4_OPEN_CLAIM_PREVIOUS:
  1392. if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
  1393. break;
  1394. fallthrough;
  1395. case NFS4_OPEN_CLAIM_NULL:
  1396. case NFS4_OPEN_CLAIM_FH:
  1397. nfs_mark_delegation_referenced(delegation);
  1398. /* Save the delegation stateid */
  1399. if (stateid)
  1400. nfs4_stateid_copy(stateid, &delegation->stateid);
  1401. ret = true;
  1402. break;
  1403. default:
  1404. break;
  1405. }
  1406. out_put_delegation:
  1407. nfs_put_delegation(delegation);
  1408. return ret;
  1409. }
  1410. static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
  1411. {
  1412. switch (fmode) {
  1413. case FMODE_WRITE:
  1414. state->n_wronly++;
  1415. break;
  1416. case FMODE_READ:
  1417. state->n_rdonly++;
  1418. break;
  1419. case FMODE_READ|FMODE_WRITE:
  1420. state->n_rdwr++;
  1421. }
  1422. nfs4_state_set_mode_locked(state, state->state | fmode);
  1423. }
  1424. static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
  1425. {
  1426. if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
  1427. return true;
  1428. if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
  1429. return true;
  1430. if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
  1431. return true;
  1432. return false;
  1433. }
  1434. static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
  1435. {
  1436. if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
  1437. wake_up_all(&state->waitq);
  1438. }
  1439. static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
  1440. {
  1441. struct nfs_client *clp = state->owner->so_server->nfs_client;
  1442. bool need_recover = false;
  1443. if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
  1444. need_recover = true;
  1445. if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
  1446. need_recover = true;
  1447. if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
  1448. need_recover = true;
  1449. if (need_recover)
  1450. nfs4_state_mark_reclaim_nograce(clp, state);
  1451. }
  1452. /*
  1453. * Check for whether or not the caller may update the open stateid
  1454. * to the value passed in by stateid.
  1455. *
  1456. * Note: This function relies heavily on the server implementing
  1457. * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
  1458. * correctly.
  1459. * i.e. The stateid seqids have to be initialised to 1, and
  1460. * are then incremented on every state transition.
  1461. */
  1462. static bool nfs_stateid_is_sequential(struct nfs4_state *state,
  1463. const nfs4_stateid *stateid)
  1464. {
  1465. if (test_bit(NFS_OPEN_STATE, &state->flags)) {
  1466. /* The common case - we're updating to a new sequence number */
  1467. if (nfs4_stateid_match_other(stateid, &state->open_stateid)) {
  1468. if (nfs4_stateid_is_next(&state->open_stateid, stateid))
  1469. return true;
  1470. return false;
  1471. }
  1472. /* The server returned a new stateid */
  1473. }
  1474. /* This is the first OPEN in this generation */
  1475. if (stateid->seqid == cpu_to_be32(1))
  1476. return true;
  1477. return false;
  1478. }
  1479. static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
  1480. {
  1481. if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
  1482. return;
  1483. if (state->n_wronly)
  1484. set_bit(NFS_O_WRONLY_STATE, &state->flags);
  1485. if (state->n_rdonly)
  1486. set_bit(NFS_O_RDONLY_STATE, &state->flags);
  1487. if (state->n_rdwr)
  1488. set_bit(NFS_O_RDWR_STATE, &state->flags);
  1489. set_bit(NFS_OPEN_STATE, &state->flags);
  1490. }
  1491. static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
  1492. nfs4_stateid *stateid, fmode_t fmode)
  1493. {
  1494. clear_bit(NFS_O_RDWR_STATE, &state->flags);
  1495. switch (fmode & (FMODE_READ|FMODE_WRITE)) {
  1496. case FMODE_WRITE:
  1497. clear_bit(NFS_O_RDONLY_STATE, &state->flags);
  1498. break;
  1499. case FMODE_READ:
  1500. clear_bit(NFS_O_WRONLY_STATE, &state->flags);
  1501. break;
  1502. case 0:
  1503. clear_bit(NFS_O_RDONLY_STATE, &state->flags);
  1504. clear_bit(NFS_O_WRONLY_STATE, &state->flags);
  1505. clear_bit(NFS_OPEN_STATE, &state->flags);
  1506. }
  1507. if (stateid == NULL)
  1508. return;
  1509. /* Handle OPEN+OPEN_DOWNGRADE races */
  1510. if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
  1511. !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
  1512. nfs_resync_open_stateid_locked(state);
  1513. goto out;
  1514. }
  1515. if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
  1516. nfs4_stateid_copy(&state->stateid, stateid);
  1517. nfs4_stateid_copy(&state->open_stateid, stateid);
  1518. trace_nfs4_open_stateid_update(state->inode, stateid, 0);
  1519. out:
  1520. nfs_state_log_update_open_stateid(state);
  1521. }
  1522. static void nfs_clear_open_stateid(struct nfs4_state *state,
  1523. nfs4_stateid *arg_stateid,
  1524. nfs4_stateid *stateid, fmode_t fmode)
  1525. {
  1526. write_seqlock(&state->seqlock);
  1527. /* Ignore, if the CLOSE argment doesn't match the current stateid */
  1528. if (nfs4_state_match_open_stateid_other(state, arg_stateid))
  1529. nfs_clear_open_stateid_locked(state, stateid, fmode);
  1530. write_sequnlock(&state->seqlock);
  1531. if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
  1532. nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
  1533. }
  1534. static void nfs_set_open_stateid_locked(struct nfs4_state *state,
  1535. const nfs4_stateid *stateid, nfs4_stateid *freeme)
  1536. __must_hold(&state->owner->so_lock)
  1537. __must_hold(&state->seqlock)
  1538. __must_hold(RCU)
  1539. {
  1540. DEFINE_WAIT(wait);
  1541. int status = 0;
  1542. for (;;) {
  1543. if (nfs_stateid_is_sequential(state, stateid))
  1544. break;
  1545. if (status) {
  1546. if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
  1547. !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
  1548. trace_nfs4_open_stateid_update_skip(state->inode,
  1549. stateid, status);
  1550. return;
  1551. } else {
  1552. break;
  1553. }
  1554. }
  1555. /* Rely on seqids for serialisation with NFSv4.0 */
  1556. if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
  1557. break;
  1558. set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
  1559. prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
  1560. /*
  1561. * Ensure we process the state changes in the same order
  1562. * in which the server processed them by delaying the
  1563. * update of the stateid until we are in sequence.
  1564. */
  1565. write_sequnlock(&state->seqlock);
  1566. spin_unlock(&state->owner->so_lock);
  1567. rcu_read_unlock();
  1568. trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
  1569. if (!fatal_signal_pending(current) &&
  1570. !nfs_current_task_exiting()) {
  1571. if (schedule_timeout(5*HZ) == 0)
  1572. status = -EAGAIN;
  1573. else
  1574. status = 0;
  1575. } else
  1576. status = -EINTR;
  1577. finish_wait(&state->waitq, &wait);
  1578. rcu_read_lock();
  1579. spin_lock(&state->owner->so_lock);
  1580. write_seqlock(&state->seqlock);
  1581. }
  1582. if (test_bit(NFS_OPEN_STATE, &state->flags) &&
  1583. !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
  1584. nfs4_stateid_copy(freeme, &state->open_stateid);
  1585. nfs_test_and_clear_all_open_stateid(state);
  1586. }
  1587. if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
  1588. nfs4_stateid_copy(&state->stateid, stateid);
  1589. nfs4_stateid_copy(&state->open_stateid, stateid);
  1590. trace_nfs4_open_stateid_update(state->inode, stateid, status);
  1591. nfs_state_log_update_open_stateid(state);
  1592. }
  1593. static void nfs_state_set_open_stateid(struct nfs4_state *state,
  1594. const nfs4_stateid *open_stateid,
  1595. fmode_t fmode,
  1596. nfs4_stateid *freeme)
  1597. {
  1598. /*
  1599. * Protect the call to nfs4_state_set_mode_locked and
  1600. * serialise the stateid update
  1601. */
  1602. write_seqlock(&state->seqlock);
  1603. nfs_set_open_stateid_locked(state, open_stateid, freeme);
  1604. switch (fmode) {
  1605. case FMODE_READ:
  1606. set_bit(NFS_O_RDONLY_STATE, &state->flags);
  1607. break;
  1608. case FMODE_WRITE:
  1609. set_bit(NFS_O_WRONLY_STATE, &state->flags);
  1610. break;
  1611. case FMODE_READ|FMODE_WRITE:
  1612. set_bit(NFS_O_RDWR_STATE, &state->flags);
  1613. }
  1614. set_bit(NFS_OPEN_STATE, &state->flags);
  1615. write_sequnlock(&state->seqlock);
  1616. }
  1617. void nfs_state_clear_open_state_flags(struct nfs4_state *state)
  1618. {
  1619. clear_bit(NFS_O_RDWR_STATE, &state->flags);
  1620. clear_bit(NFS_O_WRONLY_STATE, &state->flags);
  1621. clear_bit(NFS_O_RDONLY_STATE, &state->flags);
  1622. clear_bit(NFS_OPEN_STATE, &state->flags);
  1623. }
  1624. static void nfs_state_set_delegation(struct nfs4_state *state,
  1625. const nfs4_stateid *deleg_stateid,
  1626. fmode_t fmode)
  1627. {
  1628. /*
  1629. * Protect the call to nfs4_state_set_mode_locked and
  1630. * serialise the stateid update
  1631. */
  1632. write_seqlock(&state->seqlock);
  1633. nfs4_stateid_copy(&state->stateid, deleg_stateid);
  1634. set_bit(NFS_DELEGATED_STATE, &state->flags);
  1635. write_sequnlock(&state->seqlock);
  1636. }
  1637. void nfs_state_clear_delegation(struct nfs4_state *state)
  1638. {
  1639. write_seqlock(&state->seqlock);
  1640. nfs4_stateid_copy(&state->stateid, &state->open_stateid);
  1641. clear_bit(NFS_DELEGATED_STATE, &state->flags);
  1642. write_sequnlock(&state->seqlock);
  1643. }
  1644. int update_open_stateid(struct nfs4_state *state,
  1645. const nfs4_stateid *open_stateid,
  1646. const nfs4_stateid *delegation,
  1647. fmode_t fmode)
  1648. {
  1649. struct nfs_server *server = NFS_SERVER(state->inode);
  1650. struct nfs_client *clp = server->nfs_client;
  1651. struct nfs_delegation *deleg_cur;
  1652. nfs4_stateid freeme = { };
  1653. int ret = 0;
  1654. fmode &= (FMODE_READ|FMODE_WRITE);
  1655. spin_lock(&state->owner->so_lock);
  1656. if (open_stateid != NULL) {
  1657. rcu_read_lock();
  1658. nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
  1659. rcu_read_unlock();
  1660. ret = 1;
  1661. }
  1662. deleg_cur = nfs4_get_valid_delegation(state->inode);
  1663. if (deleg_cur == NULL)
  1664. goto no_delegation;
  1665. spin_lock(&deleg_cur->lock);
  1666. if (!deleg_cur->inode ||
  1667. test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
  1668. (deleg_cur->type & fmode) != fmode)
  1669. goto no_delegation_unlock;
  1670. if (delegation == NULL)
  1671. delegation = &deleg_cur->stateid;
  1672. else if (!nfs4_stateid_match_other(&deleg_cur->stateid, delegation))
  1673. goto no_delegation_unlock;
  1674. nfs_mark_delegation_referenced(deleg_cur);
  1675. nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
  1676. ret = 1;
  1677. no_delegation_unlock:
  1678. spin_unlock(&deleg_cur->lock);
  1679. nfs_put_delegation(deleg_cur);
  1680. no_delegation:
  1681. if (ret)
  1682. update_open_stateflags(state, fmode);
  1683. spin_unlock(&state->owner->so_lock);
  1684. if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
  1685. nfs4_schedule_state_manager(clp);
  1686. if (freeme.type != 0)
  1687. nfs4_test_and_free_stateid(server, &freeme,
  1688. state->owner->so_cred);
  1689. return ret;
  1690. }
  1691. static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
  1692. const nfs4_stateid *stateid)
  1693. {
  1694. struct nfs4_state *state = lsp->ls_state;
  1695. bool ret = false;
  1696. spin_lock(&state->state_lock);
  1697. if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
  1698. goto out_noupdate;
  1699. if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
  1700. goto out_noupdate;
  1701. nfs4_stateid_copy(&lsp->ls_stateid, stateid);
  1702. ret = true;
  1703. out_noupdate:
  1704. spin_unlock(&state->state_lock);
  1705. return ret;
  1706. }
  1707. static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
  1708. {
  1709. struct nfs_delegation *delegation;
  1710. fmode &= FMODE_READ|FMODE_WRITE;
  1711. delegation = nfs4_get_valid_delegation(inode);
  1712. if (!delegation)
  1713. return;
  1714. if ((delegation->type & fmode) != fmode)
  1715. nfs4_inode_return_delegation(inode);
  1716. nfs_put_delegation(delegation);
  1717. }
  1718. static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
  1719. {
  1720. struct nfs4_state *state = opendata->state;
  1721. int open_mode = opendata->o_arg.open_flags;
  1722. fmode_t fmode = opendata->o_arg.fmode;
  1723. enum open_claim_type4 claim = opendata->o_arg.claim;
  1724. nfs4_stateid stateid;
  1725. int ret = -EAGAIN;
  1726. for (;;) {
  1727. spin_lock(&state->owner->so_lock);
  1728. if (can_open_cached(state, fmode, open_mode, claim)) {
  1729. update_open_stateflags(state, fmode);
  1730. spin_unlock(&state->owner->so_lock);
  1731. goto out_return_state;
  1732. }
  1733. spin_unlock(&state->owner->so_lock);
  1734. if (!can_open_delegated(state->inode, fmode, claim, &stateid))
  1735. break;
  1736. nfs_release_seqid(opendata->o_arg.seqid);
  1737. if (!opendata->is_recover) {
  1738. ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
  1739. if (ret != 0)
  1740. goto out;
  1741. }
  1742. ret = -EAGAIN;
  1743. /* Try to update the stateid using the delegation */
  1744. if (update_open_stateid(state, NULL, &stateid, fmode))
  1745. goto out_return_state;
  1746. }
  1747. out:
  1748. return ERR_PTR(ret);
  1749. out_return_state:
  1750. refcount_inc(&state->count);
  1751. return state;
  1752. }
  1753. static void
  1754. nfs4_process_delegation(struct inode *inode, const struct cred *cred,
  1755. enum open_claim_type4 claim,
  1756. const struct nfs4_open_delegation *delegation)
  1757. {
  1758. switch (delegation->open_delegation_type) {
  1759. case NFS4_OPEN_DELEGATE_READ:
  1760. case NFS4_OPEN_DELEGATE_WRITE:
  1761. case NFS4_OPEN_DELEGATE_READ_ATTRS_DELEG:
  1762. case NFS4_OPEN_DELEGATE_WRITE_ATTRS_DELEG:
  1763. break;
  1764. default:
  1765. return;
  1766. }
  1767. switch (claim) {
  1768. case NFS4_OPEN_CLAIM_DELEGATE_CUR:
  1769. case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
  1770. pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
  1771. "returning a delegation for "
  1772. "OPEN(CLAIM_DELEGATE_CUR)\n",
  1773. NFS_SERVER(inode)->nfs_client->cl_hostname);
  1774. break;
  1775. case NFS4_OPEN_CLAIM_PREVIOUS:
  1776. nfs_inode_reclaim_delegation(inode, cred, delegation->type,
  1777. &delegation->stateid,
  1778. delegation->pagemod_limit,
  1779. delegation->open_delegation_type);
  1780. break;
  1781. default:
  1782. nfs_inode_set_delegation(inode, cred, delegation->type,
  1783. &delegation->stateid,
  1784. delegation->pagemod_limit,
  1785. delegation->open_delegation_type);
  1786. }
  1787. if (delegation->do_recall)
  1788. nfs_async_inode_return_delegation(inode, &delegation->stateid);
  1789. }
  1790. /*
  1791. * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
  1792. * and update the nfs4_state.
  1793. */
  1794. static struct nfs4_state *
  1795. _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
  1796. {
  1797. struct inode *inode = data->state->inode;
  1798. struct nfs4_state *state = data->state;
  1799. int ret;
  1800. if (!data->rpc_done) {
  1801. if (data->rpc_status)
  1802. return ERR_PTR(data->rpc_status);
  1803. return nfs4_try_open_cached(data);
  1804. }
  1805. ret = nfs_refresh_inode(inode, &data->f_attr);
  1806. if (ret)
  1807. return ERR_PTR(ret);
  1808. nfs4_process_delegation(state->inode,
  1809. data->owner->so_cred,
  1810. data->o_arg.claim,
  1811. &data->o_res.delegation);
  1812. if (!(data->o_res.rflags & NFS4_OPEN_RESULT_NO_OPEN_STATEID)) {
  1813. if (!update_open_stateid(state, &data->o_res.stateid,
  1814. NULL, data->o_arg.fmode))
  1815. return ERR_PTR(-EAGAIN);
  1816. } else if (!update_open_stateid(state, NULL, NULL, data->o_arg.fmode))
  1817. return ERR_PTR(-EAGAIN);
  1818. refcount_inc(&state->count);
  1819. return state;
  1820. }
  1821. static struct inode *
  1822. nfs4_opendata_get_inode(struct nfs4_opendata *data)
  1823. {
  1824. struct inode *inode;
  1825. switch (data->o_arg.claim) {
  1826. case NFS4_OPEN_CLAIM_NULL:
  1827. case NFS4_OPEN_CLAIM_DELEGATE_CUR:
  1828. case NFS4_OPEN_CLAIM_DELEGATE_PREV:
  1829. if (!(data->f_attr.valid & NFS_ATTR_FATTR))
  1830. return ERR_PTR(-EAGAIN);
  1831. inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
  1832. &data->f_attr);
  1833. break;
  1834. default:
  1835. inode = d_inode(data->dentry);
  1836. ihold(inode);
  1837. nfs_refresh_inode(inode, &data->f_attr);
  1838. }
  1839. return inode;
  1840. }
  1841. static struct nfs4_state *
  1842. nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
  1843. {
  1844. struct nfs4_state *state;
  1845. struct inode *inode;
  1846. inode = nfs4_opendata_get_inode(data);
  1847. if (IS_ERR(inode))
  1848. return ERR_CAST(inode);
  1849. if (data->state != NULL && data->state->inode == inode) {
  1850. state = data->state;
  1851. refcount_inc(&state->count);
  1852. } else
  1853. state = nfs4_get_open_state(inode, data->owner);
  1854. iput(inode);
  1855. if (state == NULL)
  1856. state = ERR_PTR(-ENOMEM);
  1857. return state;
  1858. }
  1859. static struct nfs4_state *
  1860. _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
  1861. {
  1862. struct nfs4_state *state;
  1863. if (!data->rpc_done) {
  1864. state = nfs4_try_open_cached(data);
  1865. trace_nfs4_cached_open(data->state);
  1866. goto out;
  1867. }
  1868. state = nfs4_opendata_find_nfs4_state(data);
  1869. if (IS_ERR(state))
  1870. goto out;
  1871. nfs4_process_delegation(state->inode,
  1872. data->owner->so_cred,
  1873. data->o_arg.claim,
  1874. &data->o_res.delegation);
  1875. if (!(data->o_res.rflags & NFS4_OPEN_RESULT_NO_OPEN_STATEID)) {
  1876. if (!update_open_stateid(state, &data->o_res.stateid,
  1877. NULL, data->o_arg.fmode)) {
  1878. nfs4_put_open_state(state);
  1879. state = ERR_PTR(-EAGAIN);
  1880. }
  1881. } else if (!update_open_stateid(state, NULL, NULL, data->o_arg.fmode)) {
  1882. nfs4_put_open_state(state);
  1883. state = ERR_PTR(-EAGAIN);
  1884. }
  1885. out:
  1886. nfs_release_seqid(data->o_arg.seqid);
  1887. return state;
  1888. }
  1889. static struct nfs4_state *
  1890. nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
  1891. {
  1892. struct nfs4_state *ret;
  1893. if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
  1894. ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
  1895. else
  1896. ret = _nfs4_opendata_to_nfs4_state(data);
  1897. nfs4_sequence_free_slot(&data->o_res.seq_res);
  1898. return ret;
  1899. }
  1900. static struct nfs_open_context *
  1901. nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
  1902. {
  1903. struct nfs_inode *nfsi = NFS_I(state->inode);
  1904. struct nfs_open_context *ctx;
  1905. rcu_read_lock();
  1906. list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
  1907. if (ctx->state != state)
  1908. continue;
  1909. if ((ctx->mode & mode) != mode)
  1910. continue;
  1911. if (!get_nfs_open_context(ctx))
  1912. continue;
  1913. rcu_read_unlock();
  1914. return ctx;
  1915. }
  1916. rcu_read_unlock();
  1917. return ERR_PTR(-ENOENT);
  1918. }
  1919. static struct nfs_open_context *
  1920. nfs4_state_find_open_context(struct nfs4_state *state)
  1921. {
  1922. struct nfs_open_context *ctx;
  1923. ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
  1924. if (!IS_ERR(ctx))
  1925. return ctx;
  1926. ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
  1927. if (!IS_ERR(ctx))
  1928. return ctx;
  1929. return nfs4_state_find_open_context_mode(state, FMODE_READ);
  1930. }
  1931. static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
  1932. struct nfs4_state *state, enum open_claim_type4 claim)
  1933. {
  1934. struct nfs4_opendata *opendata;
  1935. opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
  1936. NULL, claim, GFP_NOFS);
  1937. if (opendata == NULL)
  1938. return ERR_PTR(-ENOMEM);
  1939. opendata->state = state;
  1940. refcount_inc(&state->count);
  1941. return opendata;
  1942. }
  1943. static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
  1944. fmode_t fmode)
  1945. {
  1946. struct nfs4_state *newstate;
  1947. struct nfs_server *server = NFS_SB(opendata->dentry->d_sb);
  1948. int openflags = opendata->o_arg.open_flags;
  1949. int ret;
  1950. if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
  1951. return 0;
  1952. opendata->o_arg.fmode = fmode;
  1953. opendata->o_arg.share_access =
  1954. nfs4_map_atomic_open_share(server, fmode, openflags);
  1955. memset(&opendata->o_res, 0, sizeof(opendata->o_res));
  1956. memset(&opendata->c_res, 0, sizeof(opendata->c_res));
  1957. nfs4_init_opendata_res(opendata);
  1958. ret = _nfs4_recover_proc_open(opendata);
  1959. if (ret != 0)
  1960. return ret;
  1961. newstate = nfs4_opendata_to_nfs4_state(opendata);
  1962. if (IS_ERR(newstate))
  1963. return PTR_ERR(newstate);
  1964. if (newstate != opendata->state)
  1965. ret = -ESTALE;
  1966. nfs4_close_state(newstate, fmode);
  1967. return ret;
  1968. }
  1969. static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
  1970. {
  1971. int ret;
  1972. /* memory barrier prior to reading state->n_* */
  1973. smp_rmb();
  1974. ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
  1975. if (ret != 0)
  1976. return ret;
  1977. ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
  1978. if (ret != 0)
  1979. return ret;
  1980. ret = nfs4_open_recover_helper(opendata, FMODE_READ);
  1981. if (ret != 0)
  1982. return ret;
  1983. /*
  1984. * We may have performed cached opens for all three recoveries.
  1985. * Check if we need to update the current stateid.
  1986. */
  1987. if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
  1988. !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
  1989. write_seqlock(&state->seqlock);
  1990. if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
  1991. nfs4_stateid_copy(&state->stateid, &state->open_stateid);
  1992. write_sequnlock(&state->seqlock);
  1993. }
  1994. return 0;
  1995. }
  1996. /*
  1997. * OPEN_RECLAIM:
  1998. * reclaim state on the server after a reboot.
  1999. */
  2000. static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
  2001. {
  2002. struct nfs_delegation *delegation;
  2003. struct nfs4_opendata *opendata;
  2004. u32 delegation_type = NFS4_OPEN_DELEGATE_NONE;
  2005. int status;
  2006. opendata = nfs4_open_recoverdata_alloc(ctx, state,
  2007. NFS4_OPEN_CLAIM_PREVIOUS);
  2008. if (IS_ERR(opendata))
  2009. return PTR_ERR(opendata);
  2010. rcu_read_lock();
  2011. delegation = rcu_dereference(NFS_I(state->inode)->delegation);
  2012. if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0) {
  2013. switch(delegation->type) {
  2014. case FMODE_READ:
  2015. delegation_type = NFS4_OPEN_DELEGATE_READ;
  2016. if (test_bit(NFS_DELEGATION_DELEGTIME, &delegation->flags))
  2017. delegation_type = NFS4_OPEN_DELEGATE_READ_ATTRS_DELEG;
  2018. break;
  2019. case FMODE_WRITE:
  2020. case FMODE_READ|FMODE_WRITE:
  2021. delegation_type = NFS4_OPEN_DELEGATE_WRITE;
  2022. if (test_bit(NFS_DELEGATION_DELEGTIME, &delegation->flags))
  2023. delegation_type = NFS4_OPEN_DELEGATE_WRITE_ATTRS_DELEG;
  2024. }
  2025. }
  2026. rcu_read_unlock();
  2027. opendata->o_arg.u.delegation_type = delegation_type;
  2028. status = nfs4_open_recover(opendata, state);
  2029. nfs4_opendata_put(opendata);
  2030. return status;
  2031. }
  2032. static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
  2033. {
  2034. struct nfs_server *server = NFS_SERVER(state->inode);
  2035. struct nfs4_exception exception = { };
  2036. int err;
  2037. do {
  2038. err = _nfs4_do_open_reclaim(ctx, state);
  2039. trace_nfs4_open_reclaim(ctx, 0, err);
  2040. if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
  2041. continue;
  2042. if (err != -NFS4ERR_DELAY)
  2043. break;
  2044. nfs4_handle_exception(server, err, &exception);
  2045. } while (exception.retry);
  2046. return err;
  2047. }
  2048. int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
  2049. {
  2050. struct nfs_open_context *ctx;
  2051. int ret;
  2052. ctx = nfs4_state_find_open_context(state);
  2053. if (IS_ERR(ctx))
  2054. return -EAGAIN;
  2055. clear_bit(NFS_DELEGATED_STATE, &state->flags);
  2056. nfs_state_clear_open_state_flags(state);
  2057. ret = nfs4_do_open_reclaim(ctx, state);
  2058. put_nfs_open_context(ctx);
  2059. return ret;
  2060. }
  2061. static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, struct file_lock *fl, int err)
  2062. {
  2063. switch (err) {
  2064. default:
  2065. printk(KERN_ERR "NFS: %s: unhandled error "
  2066. "%d.\n", __func__, err);
  2067. fallthrough;
  2068. case 0:
  2069. case -ENOENT:
  2070. case -EAGAIN:
  2071. case -ESTALE:
  2072. case -ETIMEDOUT:
  2073. break;
  2074. case -NFS4ERR_BADSESSION:
  2075. case -NFS4ERR_BADSLOT:
  2076. case -NFS4ERR_BAD_HIGH_SLOT:
  2077. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  2078. case -NFS4ERR_DEADSESSION:
  2079. return -EAGAIN;
  2080. case -NFS4ERR_STALE_CLIENTID:
  2081. case -NFS4ERR_STALE_STATEID:
  2082. /* Don't recall a delegation if it was lost */
  2083. nfs4_schedule_lease_recovery(server->nfs_client);
  2084. return -EAGAIN;
  2085. case -NFS4ERR_MOVED:
  2086. nfs4_schedule_migration_recovery(server);
  2087. return -EAGAIN;
  2088. case -NFS4ERR_LEASE_MOVED:
  2089. nfs4_schedule_lease_moved_recovery(server->nfs_client);
  2090. return -EAGAIN;
  2091. case -NFS4ERR_DELEG_REVOKED:
  2092. case -NFS4ERR_ADMIN_REVOKED:
  2093. case -NFS4ERR_EXPIRED:
  2094. case -NFS4ERR_BAD_STATEID:
  2095. case -NFS4ERR_OPENMODE:
  2096. nfs_inode_find_state_and_recover(state->inode,
  2097. stateid);
  2098. nfs4_schedule_stateid_recovery(server, state);
  2099. return -EAGAIN;
  2100. case -NFS4ERR_DELAY:
  2101. case -NFS4ERR_GRACE:
  2102. ssleep(1);
  2103. return -EAGAIN;
  2104. case -ENOMEM:
  2105. case -NFS4ERR_DENIED:
  2106. if (fl) {
  2107. struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
  2108. if (lsp)
  2109. set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
  2110. }
  2111. return 0;
  2112. }
  2113. return err;
  2114. }
  2115. int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
  2116. struct nfs4_state *state, const nfs4_stateid *stateid)
  2117. {
  2118. struct nfs_server *server = NFS_SERVER(state->inode);
  2119. struct nfs4_opendata *opendata;
  2120. int err = 0;
  2121. opendata = nfs4_open_recoverdata_alloc(ctx, state,
  2122. NFS4_OPEN_CLAIM_DELEG_CUR_FH);
  2123. if (IS_ERR(opendata))
  2124. return PTR_ERR(opendata);
  2125. nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
  2126. if (!test_bit(NFS_O_RDWR_STATE, &state->flags)) {
  2127. err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
  2128. if (err)
  2129. goto out;
  2130. }
  2131. if (!test_bit(NFS_O_WRONLY_STATE, &state->flags)) {
  2132. err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
  2133. if (err)
  2134. goto out;
  2135. }
  2136. if (!test_bit(NFS_O_RDONLY_STATE, &state->flags)) {
  2137. err = nfs4_open_recover_helper(opendata, FMODE_READ);
  2138. if (err)
  2139. goto out;
  2140. }
  2141. nfs_state_clear_delegation(state);
  2142. out:
  2143. nfs4_opendata_put(opendata);
  2144. return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
  2145. }
  2146. static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
  2147. {
  2148. struct nfs4_opendata *data = calldata;
  2149. nfs4_setup_sequence(data->o_arg.server->nfs_client,
  2150. &data->c_arg.seq_args, &data->c_res.seq_res, task);
  2151. }
  2152. static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
  2153. {
  2154. struct nfs4_opendata *data = calldata;
  2155. data->c_res.seq_res.sr_slot_ops->done(task, &data->c_res.seq_res);
  2156. data->rpc_status = task->tk_status;
  2157. if (data->rpc_status == 0) {
  2158. nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
  2159. nfs_confirm_seqid(&data->owner->so_seqid, 0);
  2160. renew_lease(data->o_res.server, data->timestamp);
  2161. data->rpc_done = true;
  2162. }
  2163. }
  2164. static void nfs4_open_confirm_release(void *calldata)
  2165. {
  2166. struct nfs4_opendata *data = calldata;
  2167. struct nfs4_state *state = NULL;
  2168. /* If this request hasn't been cancelled, do nothing */
  2169. if (!data->cancelled)
  2170. goto out_free;
  2171. /* In case of error, no cleanup! */
  2172. if (!data->rpc_done)
  2173. goto out_free;
  2174. state = nfs4_opendata_to_nfs4_state(data);
  2175. if (!IS_ERR(state))
  2176. nfs4_close_state(state, data->o_arg.fmode);
  2177. out_free:
  2178. nfs4_opendata_put(data);
  2179. }
  2180. static const struct rpc_call_ops nfs4_open_confirm_ops = {
  2181. .rpc_call_prepare = nfs4_open_confirm_prepare,
  2182. .rpc_call_done = nfs4_open_confirm_done,
  2183. .rpc_release = nfs4_open_confirm_release,
  2184. };
  2185. /*
  2186. * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
  2187. */
  2188. static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
  2189. {
  2190. struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
  2191. struct rpc_task *task;
  2192. struct rpc_message msg = {
  2193. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
  2194. .rpc_argp = &data->c_arg,
  2195. .rpc_resp = &data->c_res,
  2196. .rpc_cred = data->owner->so_cred,
  2197. };
  2198. struct rpc_task_setup task_setup_data = {
  2199. .rpc_client = server->client,
  2200. .rpc_message = &msg,
  2201. .callback_ops = &nfs4_open_confirm_ops,
  2202. .callback_data = data,
  2203. .workqueue = nfsiod_workqueue,
  2204. .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
  2205. };
  2206. int status;
  2207. nfs4_init_sequence(server->nfs_client, &data->c_arg.seq_args,
  2208. &data->c_res.seq_res, 1, data->is_recover);
  2209. kref_get(&data->kref);
  2210. data->rpc_done = false;
  2211. data->rpc_status = 0;
  2212. data->timestamp = jiffies;
  2213. task = rpc_run_task(&task_setup_data);
  2214. if (IS_ERR(task))
  2215. return PTR_ERR(task);
  2216. status = rpc_wait_for_completion_task(task);
  2217. if (status != 0) {
  2218. data->cancelled = true;
  2219. smp_wmb();
  2220. } else
  2221. status = data->rpc_status;
  2222. rpc_put_task(task);
  2223. return status;
  2224. }
  2225. static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
  2226. {
  2227. struct nfs4_opendata *data = calldata;
  2228. struct nfs4_state_owner *sp = data->owner;
  2229. struct nfs_client *clp = sp->so_server->nfs_client;
  2230. enum open_claim_type4 claim = data->o_arg.claim;
  2231. if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
  2232. goto out_wait;
  2233. /*
  2234. * Check if we still need to send an OPEN call, or if we can use
  2235. * a delegation instead.
  2236. */
  2237. if (data->state != NULL) {
  2238. if (can_open_cached(data->state, data->o_arg.fmode,
  2239. data->o_arg.open_flags, claim))
  2240. goto out_no_action;
  2241. if (can_open_delegated(data->state->inode, data->o_arg.fmode,
  2242. claim, NULL)) {
  2243. trace_nfs4_cached_open(data->state);
  2244. goto out_no_action;
  2245. }
  2246. }
  2247. /* Update client id. */
  2248. data->o_arg.clientid = clp->cl_clientid;
  2249. switch (claim) {
  2250. default:
  2251. break;
  2252. case NFS4_OPEN_CLAIM_PREVIOUS:
  2253. case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
  2254. case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
  2255. data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
  2256. fallthrough;
  2257. case NFS4_OPEN_CLAIM_FH:
  2258. task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
  2259. }
  2260. data->timestamp = jiffies;
  2261. if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
  2262. &data->o_arg.seq_args,
  2263. &data->o_res.seq_res,
  2264. task) != 0)
  2265. nfs_release_seqid(data->o_arg.seqid);
  2266. /* Set the create mode (note dependency on the session type) */
  2267. data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
  2268. if (data->o_arg.open_flags & O_EXCL) {
  2269. data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
  2270. if (clp->cl_mvops->minor_version == 0) {
  2271. data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
  2272. /* don't put an ACCESS op in OPEN compound if O_EXCL,
  2273. * because ACCESS will return permission denied for
  2274. * all bits until close */
  2275. data->o_res.access_request = data->o_arg.access = 0;
  2276. } else if (nfs4_has_persistent_session(clp))
  2277. data->o_arg.createmode = NFS4_CREATE_GUARDED;
  2278. }
  2279. return;
  2280. out_no_action:
  2281. task->tk_action = NULL;
  2282. out_wait:
  2283. nfs4_sequence_done(task, &data->o_res.seq_res);
  2284. }
  2285. static void nfs4_open_done(struct rpc_task *task, void *calldata)
  2286. {
  2287. struct nfs4_opendata *data = calldata;
  2288. data->rpc_status = task->tk_status;
  2289. if (!nfs4_sequence_process(task, &data->o_res.seq_res))
  2290. return;
  2291. if (task->tk_status == 0) {
  2292. if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
  2293. switch (data->o_res.f_attr->mode & S_IFMT) {
  2294. case S_IFREG:
  2295. break;
  2296. case S_IFLNK:
  2297. data->rpc_status = -ELOOP;
  2298. break;
  2299. case S_IFDIR:
  2300. data->rpc_status = -EISDIR;
  2301. break;
  2302. default:
  2303. data->rpc_status = -ENOTDIR;
  2304. }
  2305. }
  2306. renew_lease(data->o_res.server, data->timestamp);
  2307. if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
  2308. nfs_confirm_seqid(&data->owner->so_seqid, 0);
  2309. }
  2310. data->rpc_done = true;
  2311. }
  2312. static void nfs4_open_release(void *calldata)
  2313. {
  2314. struct nfs4_opendata *data = calldata;
  2315. struct nfs4_state *state = NULL;
  2316. /* In case of error, no cleanup! */
  2317. if (data->rpc_status != 0 || !data->rpc_done) {
  2318. nfs_release_seqid(data->o_arg.seqid);
  2319. goto out_free;
  2320. }
  2321. /* If this request hasn't been cancelled, do nothing */
  2322. if (!data->cancelled)
  2323. goto out_free;
  2324. /* In case we need an open_confirm, no cleanup! */
  2325. if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
  2326. goto out_free;
  2327. state = nfs4_opendata_to_nfs4_state(data);
  2328. if (!IS_ERR(state))
  2329. nfs4_close_state(state, data->o_arg.fmode);
  2330. out_free:
  2331. nfs4_opendata_put(data);
  2332. }
  2333. static const struct rpc_call_ops nfs4_open_ops = {
  2334. .rpc_call_prepare = nfs4_open_prepare,
  2335. .rpc_call_done = nfs4_open_done,
  2336. .rpc_release = nfs4_open_release,
  2337. };
  2338. static int nfs4_run_open_task(struct nfs4_opendata *data,
  2339. struct nfs_open_context *ctx)
  2340. {
  2341. struct inode *dir = d_inode(data->dir);
  2342. struct nfs_server *server = NFS_SERVER(dir);
  2343. struct nfs_client *clp = server->nfs_client;
  2344. struct nfs_openargs *o_arg = &data->o_arg;
  2345. struct nfs_openres *o_res = &data->o_res;
  2346. struct rpc_task *task;
  2347. struct rpc_message msg = {
  2348. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
  2349. .rpc_argp = o_arg,
  2350. .rpc_resp = o_res,
  2351. .rpc_cred = data->owner->so_cred,
  2352. };
  2353. struct rpc_task_setup task_setup_data = {
  2354. .rpc_client = server->client,
  2355. .rpc_message = &msg,
  2356. .callback_ops = &nfs4_open_ops,
  2357. .callback_data = data,
  2358. .workqueue = nfsiod_workqueue,
  2359. .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
  2360. };
  2361. int status;
  2362. if (nfs_server_capable(dir, NFS_CAP_MOVEABLE))
  2363. task_setup_data.flags |= RPC_TASK_MOVEABLE;
  2364. kref_get(&data->kref);
  2365. data->rpc_done = false;
  2366. data->rpc_status = 0;
  2367. data->cancelled = false;
  2368. data->is_recover = false;
  2369. if (!ctx) {
  2370. nfs4_init_sequence(clp, &o_arg->seq_args, &o_res->seq_res, 1, 1);
  2371. data->is_recover = true;
  2372. task_setup_data.flags |= RPC_TASK_TIMEOUT;
  2373. } else {
  2374. nfs4_init_sequence(clp, &o_arg->seq_args, &o_res->seq_res, 1, 0);
  2375. pnfs_lgopen_prepare(data, ctx);
  2376. }
  2377. task = rpc_run_task(&task_setup_data);
  2378. if (IS_ERR(task))
  2379. return PTR_ERR(task);
  2380. status = rpc_wait_for_completion_task(task);
  2381. if (status != 0) {
  2382. data->cancelled = true;
  2383. smp_wmb();
  2384. } else
  2385. status = data->rpc_status;
  2386. rpc_put_task(task);
  2387. return status;
  2388. }
  2389. static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
  2390. {
  2391. struct inode *dir = d_inode(data->dir);
  2392. struct nfs_openres *o_res = &data->o_res;
  2393. int status;
  2394. status = nfs4_run_open_task(data, NULL);
  2395. if (status != 0 || !data->rpc_done)
  2396. return status;
  2397. nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
  2398. if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
  2399. status = _nfs4_proc_open_confirm(data);
  2400. return status;
  2401. }
  2402. /*
  2403. * Additional permission checks in order to distinguish between an
  2404. * open for read, and an open for execute. This works around the
  2405. * fact that NFSv4 OPEN treats read and execute permissions as being
  2406. * the same.
  2407. * Note that in the non-execute case, we want to turn off permission
  2408. * checking if we just created a new file (POSIX open() semantics).
  2409. */
  2410. static int nfs4_opendata_access(const struct cred *cred,
  2411. struct nfs4_opendata *opendata,
  2412. struct nfs4_state *state, fmode_t fmode)
  2413. {
  2414. struct nfs_access_entry cache;
  2415. u32 mask, flags;
  2416. /* access call failed or for some reason the server doesn't
  2417. * support any access modes -- defer access call until later */
  2418. if (opendata->o_res.access_supported == 0)
  2419. return 0;
  2420. mask = 0;
  2421. if (fmode & FMODE_EXEC) {
  2422. /* ONLY check for exec rights */
  2423. if (S_ISDIR(state->inode->i_mode))
  2424. mask = NFS4_ACCESS_LOOKUP;
  2425. else
  2426. mask = NFS4_ACCESS_EXECUTE;
  2427. } else if ((fmode & FMODE_READ) && !opendata->file_created)
  2428. mask = NFS4_ACCESS_READ;
  2429. nfs_access_set_mask(&cache, opendata->o_res.access_result);
  2430. nfs_access_add_cache(state->inode, &cache, cred);
  2431. flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
  2432. if ((mask & ~cache.mask & flags) == 0)
  2433. return 0;
  2434. return -EACCES;
  2435. }
  2436. /*
  2437. * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
  2438. */
  2439. static int _nfs4_proc_open(struct nfs4_opendata *data,
  2440. struct nfs_open_context *ctx)
  2441. {
  2442. struct inode *dir = d_inode(data->dir);
  2443. struct nfs_server *server = NFS_SERVER(dir);
  2444. struct nfs_openargs *o_arg = &data->o_arg;
  2445. struct nfs_openres *o_res = &data->o_res;
  2446. int status;
  2447. status = nfs4_run_open_task(data, ctx);
  2448. if (!data->rpc_done)
  2449. return status;
  2450. if (status != 0) {
  2451. if (status == -NFS4ERR_BADNAME &&
  2452. !(o_arg->open_flags & O_CREAT))
  2453. return -ENOENT;
  2454. return status;
  2455. }
  2456. nfs_fattr_map_and_free_names(server, &data->f_attr);
  2457. if (o_arg->open_flags & O_CREAT) {
  2458. if (o_arg->open_flags & O_EXCL)
  2459. data->file_created = true;
  2460. else if (o_res->cinfo.before != o_res->cinfo.after)
  2461. data->file_created = true;
  2462. if (data->file_created ||
  2463. inode_peek_iversion_raw(dir) != o_res->cinfo.after)
  2464. nfs4_update_changeattr(dir, &o_res->cinfo,
  2465. o_res->f_attr->time_start,
  2466. NFS_INO_INVALID_DATA);
  2467. }
  2468. if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
  2469. server->caps &= ~NFS_CAP_POSIX_LOCK;
  2470. if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
  2471. status = _nfs4_proc_open_confirm(data);
  2472. if (status != 0)
  2473. return status;
  2474. }
  2475. if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
  2476. struct nfs_fh *fh = &o_res->fh;
  2477. nfs4_sequence_free_slot(&o_res->seq_res);
  2478. if (o_arg->claim == NFS4_OPEN_CLAIM_FH)
  2479. fh = NFS_FH(d_inode(data->dentry));
  2480. nfs4_proc_getattr(server, fh, o_res->f_attr, NULL);
  2481. }
  2482. return 0;
  2483. }
  2484. /*
  2485. * OPEN_EXPIRED:
  2486. * reclaim state on the server after a network partition.
  2487. * Assumes caller holds the appropriate lock
  2488. */
  2489. static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
  2490. {
  2491. struct nfs4_opendata *opendata;
  2492. int ret;
  2493. opendata = nfs4_open_recoverdata_alloc(ctx, state, NFS4_OPEN_CLAIM_FH);
  2494. if (IS_ERR(opendata))
  2495. return PTR_ERR(opendata);
  2496. /*
  2497. * We're not recovering a delegation, so ask for no delegation.
  2498. * Otherwise the recovery thread could deadlock with an outstanding
  2499. * delegation return.
  2500. */
  2501. opendata->o_arg.open_flags = O_DIRECT;
  2502. ret = nfs4_open_recover(opendata, state);
  2503. if (ret == -ESTALE)
  2504. d_drop(ctx->dentry);
  2505. nfs4_opendata_put(opendata);
  2506. return ret;
  2507. }
  2508. static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
  2509. {
  2510. struct nfs_server *server = NFS_SERVER(state->inode);
  2511. struct nfs4_exception exception = { };
  2512. int err;
  2513. do {
  2514. err = _nfs4_open_expired(ctx, state);
  2515. trace_nfs4_open_expired(ctx, 0, err);
  2516. if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
  2517. continue;
  2518. switch (err) {
  2519. default:
  2520. goto out;
  2521. case -NFS4ERR_GRACE:
  2522. case -NFS4ERR_DELAY:
  2523. nfs4_handle_exception(server, err, &exception);
  2524. err = 0;
  2525. }
  2526. } while (exception.retry);
  2527. out:
  2528. return err;
  2529. }
  2530. int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
  2531. {
  2532. struct nfs_open_context *ctx;
  2533. int ret;
  2534. ctx = nfs4_state_find_open_context(state);
  2535. if (IS_ERR(ctx))
  2536. return -EAGAIN;
  2537. ret = nfs4_do_open_expired(ctx, state);
  2538. put_nfs_open_context(ctx);
  2539. return ret;
  2540. }
  2541. void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
  2542. const nfs4_stateid *stateid)
  2543. {
  2544. nfs_remove_bad_delegation(state->inode, stateid);
  2545. nfs_state_clear_delegation(state);
  2546. }
  2547. static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
  2548. nfs4_stateid *stateid, const struct cred *cred)
  2549. {
  2550. int status;
  2551. switch (stateid->type) {
  2552. default:
  2553. break;
  2554. case NFS4_INVALID_STATEID_TYPE:
  2555. case NFS4_SPECIAL_STATEID_TYPE:
  2556. case NFS4_FREED_STATEID_TYPE:
  2557. return -NFS4ERR_BAD_STATEID;
  2558. case NFS4_REVOKED_STATEID_TYPE:
  2559. goto out_free;
  2560. }
  2561. status = nfs41_test_stateid(server, stateid, cred);
  2562. switch (status) {
  2563. case -NFS4ERR_EXPIRED:
  2564. case -NFS4ERR_ADMIN_REVOKED:
  2565. case -NFS4ERR_DELEG_REVOKED:
  2566. break;
  2567. default:
  2568. return status;
  2569. }
  2570. out_free:
  2571. /* Ack the revoked state to the server */
  2572. nfs41_free_stateid(server, stateid, cred, true);
  2573. return -NFS4ERR_EXPIRED;
  2574. }
  2575. static int nfs41_check_delegation_stateid(struct nfs4_state *state)
  2576. {
  2577. struct nfs_server *server = NFS_SERVER(state->inode);
  2578. nfs4_stateid stateid;
  2579. struct nfs_delegation *delegation;
  2580. const struct cred *cred = NULL;
  2581. int status, ret = NFS_OK;
  2582. /* Get the delegation credential for use by test/free_stateid */
  2583. rcu_read_lock();
  2584. delegation = rcu_dereference(NFS_I(state->inode)->delegation);
  2585. if (delegation == NULL) {
  2586. rcu_read_unlock();
  2587. nfs_state_clear_delegation(state);
  2588. return NFS_OK;
  2589. }
  2590. spin_lock(&delegation->lock);
  2591. nfs4_stateid_copy(&stateid, &delegation->stateid);
  2592. if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
  2593. &delegation->flags)) {
  2594. spin_unlock(&delegation->lock);
  2595. rcu_read_unlock();
  2596. return NFS_OK;
  2597. }
  2598. if (delegation->cred)
  2599. cred = get_cred(delegation->cred);
  2600. spin_unlock(&delegation->lock);
  2601. rcu_read_unlock();
  2602. status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
  2603. trace_nfs4_test_delegation_stateid(state, NULL, status);
  2604. if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
  2605. nfs_finish_clear_delegation_stateid(state, &stateid);
  2606. else
  2607. ret = status;
  2608. put_cred(cred);
  2609. return ret;
  2610. }
  2611. static void nfs41_delegation_recover_stateid(struct nfs4_state *state)
  2612. {
  2613. nfs4_stateid tmp;
  2614. if (test_bit(NFS_DELEGATED_STATE, &state->flags) &&
  2615. nfs4_copy_delegation_stateid(state->inode, state->state,
  2616. &tmp, NULL) &&
  2617. nfs4_stateid_match_other(&state->stateid, &tmp))
  2618. nfs_state_set_delegation(state, &tmp, state->state);
  2619. else
  2620. nfs_state_clear_delegation(state);
  2621. }
  2622. /**
  2623. * nfs41_check_expired_locks - possibly free a lock stateid
  2624. *
  2625. * @state: NFSv4 state for an inode
  2626. *
  2627. * Returns NFS_OK if recovery for this stateid is now finished.
  2628. * Otherwise a negative NFS4ERR value is returned.
  2629. */
  2630. static int nfs41_check_expired_locks(struct nfs4_state *state)
  2631. {
  2632. int status, ret = NFS_OK;
  2633. struct nfs4_lock_state *lsp, *prev = NULL;
  2634. struct nfs_server *server = NFS_SERVER(state->inode);
  2635. if (!test_bit(LK_STATE_IN_USE, &state->flags))
  2636. goto out;
  2637. spin_lock(&state->state_lock);
  2638. list_for_each_entry(lsp, &state->lock_states, ls_locks) {
  2639. if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
  2640. const struct cred *cred = lsp->ls_state->owner->so_cred;
  2641. refcount_inc(&lsp->ls_count);
  2642. spin_unlock(&state->state_lock);
  2643. nfs4_put_lock_state(prev);
  2644. prev = lsp;
  2645. status = nfs41_test_and_free_expired_stateid(server,
  2646. &lsp->ls_stateid,
  2647. cred);
  2648. trace_nfs4_test_lock_stateid(state, lsp, status);
  2649. if (status == -NFS4ERR_EXPIRED ||
  2650. status == -NFS4ERR_BAD_STATEID) {
  2651. clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
  2652. lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
  2653. if (!recover_lost_locks)
  2654. set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
  2655. } else if (status != NFS_OK) {
  2656. ret = status;
  2657. nfs4_put_lock_state(prev);
  2658. goto out;
  2659. }
  2660. spin_lock(&state->state_lock);
  2661. }
  2662. }
  2663. spin_unlock(&state->state_lock);
  2664. nfs4_put_lock_state(prev);
  2665. out:
  2666. return ret;
  2667. }
  2668. /**
  2669. * nfs41_check_open_stateid - possibly free an open stateid
  2670. *
  2671. * @state: NFSv4 state for an inode
  2672. *
  2673. * Returns NFS_OK if recovery for this stateid is now finished.
  2674. * Otherwise a negative NFS4ERR value is returned.
  2675. */
  2676. static int nfs41_check_open_stateid(struct nfs4_state *state)
  2677. {
  2678. struct nfs_server *server = NFS_SERVER(state->inode);
  2679. nfs4_stateid *stateid = &state->open_stateid;
  2680. const struct cred *cred = state->owner->so_cred;
  2681. int status;
  2682. if (test_bit(NFS_OPEN_STATE, &state->flags) == 0)
  2683. return -NFS4ERR_BAD_STATEID;
  2684. status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
  2685. trace_nfs4_test_open_stateid(state, NULL, status);
  2686. if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
  2687. nfs_state_clear_open_state_flags(state);
  2688. stateid->type = NFS4_INVALID_STATEID_TYPE;
  2689. return status;
  2690. }
  2691. if (nfs_open_stateid_recover_openmode(state))
  2692. return -NFS4ERR_OPENMODE;
  2693. return NFS_OK;
  2694. }
  2695. static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
  2696. {
  2697. int status;
  2698. status = nfs41_check_delegation_stateid(state);
  2699. if (status != NFS_OK)
  2700. return status;
  2701. nfs41_delegation_recover_stateid(state);
  2702. status = nfs41_check_expired_locks(state);
  2703. if (status != NFS_OK)
  2704. return status;
  2705. status = nfs41_check_open_stateid(state);
  2706. if (status != NFS_OK)
  2707. status = nfs4_open_expired(sp, state);
  2708. return status;
  2709. }
  2710. /*
  2711. * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
  2712. * fields corresponding to attributes that were used to store the verifier.
  2713. * Make sure we clobber those fields in the later setattr call
  2714. */
  2715. static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
  2716. struct iattr *sattr, struct nfs4_label **label)
  2717. {
  2718. const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
  2719. __u32 attrset[3];
  2720. unsigned ret;
  2721. unsigned i;
  2722. for (i = 0; i < ARRAY_SIZE(attrset); i++) {
  2723. attrset[i] = opendata->o_res.attrset[i];
  2724. if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
  2725. attrset[i] &= ~bitmask[i];
  2726. }
  2727. ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
  2728. sattr->ia_valid : 0;
  2729. if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
  2730. if (sattr->ia_valid & ATTR_ATIME_SET)
  2731. ret |= ATTR_ATIME_SET;
  2732. else
  2733. ret |= ATTR_ATIME;
  2734. }
  2735. if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
  2736. if (sattr->ia_valid & ATTR_MTIME_SET)
  2737. ret |= ATTR_MTIME_SET;
  2738. else
  2739. ret |= ATTR_MTIME;
  2740. }
  2741. if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
  2742. *label = NULL;
  2743. return ret;
  2744. }
  2745. static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
  2746. struct nfs_open_context *ctx)
  2747. {
  2748. struct nfs4_state_owner *sp = opendata->owner;
  2749. struct nfs_server *server = sp->so_server;
  2750. struct dentry *dentry;
  2751. struct nfs4_state *state;
  2752. fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx);
  2753. struct inode *dir = d_inode(opendata->dir);
  2754. unsigned long dir_verifier;
  2755. int ret;
  2756. dir_verifier = nfs_save_change_attribute(dir);
  2757. ret = _nfs4_proc_open(opendata, ctx);
  2758. if (ret != 0)
  2759. goto out;
  2760. state = _nfs4_opendata_to_nfs4_state(opendata);
  2761. ret = PTR_ERR(state);
  2762. if (IS_ERR(state))
  2763. goto out;
  2764. ctx->state = state;
  2765. if (server->caps & NFS_CAP_POSIX_LOCK)
  2766. set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
  2767. if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
  2768. set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
  2769. if (opendata->o_res.rflags & NFS4_OPEN_RESULT_PRESERVE_UNLINKED)
  2770. set_bit(NFS_INO_PRESERVE_UNLINKED, &NFS_I(state->inode)->flags);
  2771. switch(opendata->o_arg.claim) {
  2772. default:
  2773. break;
  2774. case NFS4_OPEN_CLAIM_NULL:
  2775. case NFS4_OPEN_CLAIM_DELEGATE_CUR:
  2776. case NFS4_OPEN_CLAIM_DELEGATE_PREV:
  2777. if (!opendata->rpc_done)
  2778. break;
  2779. if (opendata->o_res.delegation.type != 0)
  2780. dir_verifier = nfs_save_change_attribute(dir);
  2781. }
  2782. dentry = opendata->dentry;
  2783. nfs_set_verifier(dentry, dir_verifier);
  2784. if (d_really_is_negative(dentry)) {
  2785. struct dentry *alias;
  2786. d_drop(dentry);
  2787. alias = d_splice_alias(igrab(state->inode), dentry);
  2788. /* d_splice_alias() can't fail here - it's a non-directory */
  2789. if (alias) {
  2790. dput(ctx->dentry);
  2791. nfs_set_verifier(alias, dir_verifier);
  2792. ctx->dentry = dentry = alias;
  2793. }
  2794. }
  2795. /* Parse layoutget results before we check for access */
  2796. pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
  2797. ret = nfs4_opendata_access(sp->so_cred, opendata, state, acc_mode);
  2798. if (ret != 0)
  2799. goto out;
  2800. if (d_inode(dentry) == state->inode)
  2801. nfs_inode_attach_open_context(ctx);
  2802. out:
  2803. if (!opendata->cancelled) {
  2804. if (opendata->lgp) {
  2805. nfs4_lgopen_release(opendata->lgp);
  2806. opendata->lgp = NULL;
  2807. }
  2808. nfs4_sequence_free_slot(&opendata->o_res.seq_res);
  2809. }
  2810. return ret;
  2811. }
  2812. /*
  2813. * Returns a referenced nfs4_state
  2814. */
  2815. static int _nfs4_do_open(struct inode *dir,
  2816. struct nfs_open_context *ctx,
  2817. int flags,
  2818. const struct nfs4_open_createattrs *c,
  2819. int *opened)
  2820. {
  2821. struct nfs4_state_owner *sp;
  2822. struct nfs4_state *state = NULL;
  2823. struct nfs_server *server = NFS_SERVER(dir);
  2824. struct nfs4_opendata *opendata;
  2825. struct dentry *dentry = ctx->dentry;
  2826. const struct cred *cred = ctx->cred;
  2827. struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
  2828. fmode_t fmode = _nfs4_ctx_to_openmode(ctx);
  2829. enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
  2830. struct iattr *sattr = c->sattr;
  2831. struct nfs4_label *label = c->label;
  2832. int status;
  2833. /* Protect against reboot recovery conflicts */
  2834. status = -ENOMEM;
  2835. sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
  2836. if (sp == NULL) {
  2837. dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
  2838. goto out_err;
  2839. }
  2840. status = nfs4_client_recover_expired_lease(server->nfs_client);
  2841. if (status != 0)
  2842. goto err_put_state_owner;
  2843. if (d_really_is_positive(dentry))
  2844. nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
  2845. status = -ENOMEM;
  2846. if (d_really_is_positive(dentry))
  2847. claim = NFS4_OPEN_CLAIM_FH;
  2848. opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
  2849. c, claim, GFP_KERNEL);
  2850. if (opendata == NULL)
  2851. goto err_put_state_owner;
  2852. if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
  2853. if (!opendata->f_attr.mdsthreshold) {
  2854. opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
  2855. if (!opendata->f_attr.mdsthreshold)
  2856. goto err_opendata_put;
  2857. }
  2858. opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
  2859. }
  2860. if (d_really_is_positive(dentry))
  2861. opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
  2862. status = _nfs4_open_and_get_state(opendata, ctx);
  2863. if (status != 0)
  2864. goto err_opendata_put;
  2865. state = ctx->state;
  2866. if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
  2867. (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
  2868. unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
  2869. /*
  2870. * send create attributes which was not set by open
  2871. * with an extra setattr.
  2872. */
  2873. if (attrs || label) {
  2874. unsigned ia_old = sattr->ia_valid;
  2875. sattr->ia_valid = attrs;
  2876. nfs_fattr_init(opendata->o_res.f_attr);
  2877. status = nfs4_do_setattr(state->inode, cred,
  2878. opendata->o_res.f_attr, sattr,
  2879. ctx, label);
  2880. if (status == 0) {
  2881. nfs_setattr_update_inode(state->inode, sattr,
  2882. opendata->o_res.f_attr);
  2883. nfs_setsecurity(state->inode, opendata->o_res.f_attr);
  2884. }
  2885. sattr->ia_valid = ia_old;
  2886. }
  2887. }
  2888. if (opened && opendata->file_created)
  2889. *opened = 1;
  2890. if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
  2891. *ctx_th = opendata->f_attr.mdsthreshold;
  2892. opendata->f_attr.mdsthreshold = NULL;
  2893. }
  2894. nfs4_opendata_put(opendata);
  2895. nfs4_put_state_owner(sp);
  2896. return 0;
  2897. err_opendata_put:
  2898. nfs4_opendata_put(opendata);
  2899. err_put_state_owner:
  2900. nfs4_put_state_owner(sp);
  2901. out_err:
  2902. return status;
  2903. }
  2904. static struct nfs4_state *nfs4_do_open(struct inode *dir,
  2905. struct nfs_open_context *ctx,
  2906. int flags,
  2907. struct iattr *sattr,
  2908. struct nfs4_label *label,
  2909. int *opened)
  2910. {
  2911. struct nfs_server *server = NFS_SERVER(dir);
  2912. struct nfs4_exception exception = {
  2913. .interruptible = true,
  2914. };
  2915. struct nfs4_state *res;
  2916. struct nfs4_open_createattrs c = {
  2917. .label = label,
  2918. .sattr = sattr,
  2919. .verf = {
  2920. [0] = (__u32)jiffies,
  2921. [1] = (__u32)current->pid,
  2922. },
  2923. };
  2924. int status;
  2925. do {
  2926. status = _nfs4_do_open(dir, ctx, flags, &c, opened);
  2927. res = ctx->state;
  2928. trace_nfs4_open_file(ctx, flags, status);
  2929. if (status == 0)
  2930. break;
  2931. /* NOTE: BAD_SEQID means the server and client disagree about the
  2932. * book-keeping w.r.t. state-changing operations
  2933. * (OPEN/CLOSE/LOCK/LOCKU...)
  2934. * It is actually a sign of a bug on the client or on the server.
  2935. *
  2936. * If we receive a BAD_SEQID error in the particular case of
  2937. * doing an OPEN, we assume that nfs_increment_open_seqid() will
  2938. * have unhashed the old state_owner for us, and that we can
  2939. * therefore safely retry using a new one. We should still warn
  2940. * the user though...
  2941. */
  2942. if (status == -NFS4ERR_BAD_SEQID) {
  2943. pr_warn_ratelimited("NFS: v4 server %s "
  2944. " returned a bad sequence-id error!\n",
  2945. NFS_SERVER(dir)->nfs_client->cl_hostname);
  2946. exception.retry = 1;
  2947. continue;
  2948. }
  2949. /*
  2950. * BAD_STATEID on OPEN means that the server cancelled our
  2951. * state before it received the OPEN_CONFIRM.
  2952. * Recover by retrying the request as per the discussion
  2953. * on Page 181 of RFC3530.
  2954. */
  2955. if (status == -NFS4ERR_BAD_STATEID) {
  2956. exception.retry = 1;
  2957. continue;
  2958. }
  2959. if (status == -NFS4ERR_EXPIRED) {
  2960. nfs4_schedule_lease_recovery(server->nfs_client);
  2961. exception.retry = 1;
  2962. continue;
  2963. }
  2964. if (status == -EAGAIN) {
  2965. /* We must have found a delegation */
  2966. exception.retry = 1;
  2967. continue;
  2968. }
  2969. if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
  2970. continue;
  2971. res = ERR_PTR(nfs4_handle_exception(server,
  2972. status, &exception));
  2973. } while (exception.retry);
  2974. return res;
  2975. }
  2976. static int _nfs4_do_setattr(struct inode *inode,
  2977. struct nfs_setattrargs *arg,
  2978. struct nfs_setattrres *res,
  2979. const struct cred *cred,
  2980. struct nfs_open_context *ctx)
  2981. {
  2982. struct nfs_server *server = NFS_SERVER(inode);
  2983. struct rpc_message msg = {
  2984. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
  2985. .rpc_argp = arg,
  2986. .rpc_resp = res,
  2987. .rpc_cred = cred,
  2988. };
  2989. const struct cred *delegation_cred = NULL;
  2990. unsigned long timestamp = jiffies;
  2991. bool truncate;
  2992. int status;
  2993. nfs_fattr_init(res->fattr);
  2994. /* Servers should only apply open mode checks for file size changes */
  2995. truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
  2996. if (!truncate) {
  2997. nfs4_inode_make_writeable(inode);
  2998. goto zero_stateid;
  2999. }
  3000. if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
  3001. /* Use that stateid */
  3002. } else if (ctx != NULL && ctx->state) {
  3003. struct nfs_lock_context *l_ctx;
  3004. if (!nfs4_valid_open_stateid(ctx->state))
  3005. return -EBADF;
  3006. l_ctx = nfs_get_lock_context(ctx);
  3007. if (IS_ERR(l_ctx))
  3008. return PTR_ERR(l_ctx);
  3009. status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
  3010. &arg->stateid, &delegation_cred);
  3011. nfs_put_lock_context(l_ctx);
  3012. if (status == -EIO)
  3013. return -EBADF;
  3014. else if (status == -EAGAIN)
  3015. goto zero_stateid;
  3016. } else {
  3017. zero_stateid:
  3018. nfs4_stateid_copy(&arg->stateid, &zero_stateid);
  3019. }
  3020. if (delegation_cred)
  3021. msg.rpc_cred = delegation_cred;
  3022. status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
  3023. put_cred(delegation_cred);
  3024. if (status == 0 && ctx != NULL)
  3025. renew_lease(server, timestamp);
  3026. trace_nfs4_setattr(inode, &arg->stateid, status);
  3027. return status;
  3028. }
  3029. static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
  3030. struct nfs_fattr *fattr, struct iattr *sattr,
  3031. struct nfs_open_context *ctx, struct nfs4_label *ilabel)
  3032. {
  3033. struct nfs_server *server = NFS_SERVER(inode);
  3034. __u32 bitmask[NFS4_BITMASK_SZ];
  3035. struct nfs4_state *state = ctx ? ctx->state : NULL;
  3036. struct nfs_setattrargs arg = {
  3037. .fh = NFS_FH(inode),
  3038. .iap = sattr,
  3039. .server = server,
  3040. .bitmask = bitmask,
  3041. .label = ilabel,
  3042. };
  3043. struct nfs_setattrres res = {
  3044. .fattr = fattr,
  3045. .server = server,
  3046. };
  3047. struct nfs4_exception exception = {
  3048. .state = state,
  3049. .inode = inode,
  3050. .stateid = &arg.stateid,
  3051. };
  3052. unsigned long adjust_flags = NFS_INO_INVALID_CHANGE |
  3053. NFS_INO_INVALID_CTIME;
  3054. int err;
  3055. if (sattr->ia_valid & (ATTR_MODE | ATTR_KILL_SUID | ATTR_KILL_SGID))
  3056. adjust_flags |= NFS_INO_INVALID_MODE;
  3057. if (sattr->ia_valid & (ATTR_UID | ATTR_GID))
  3058. adjust_flags |= NFS_INO_INVALID_OTHER;
  3059. if (sattr->ia_valid & ATTR_ATIME)
  3060. adjust_flags |= NFS_INO_INVALID_ATIME;
  3061. if (sattr->ia_valid & ATTR_MTIME)
  3062. adjust_flags |= NFS_INO_INVALID_MTIME;
  3063. do {
  3064. nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, fattr->label),
  3065. inode, adjust_flags);
  3066. err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
  3067. switch (err) {
  3068. case -NFS4ERR_OPENMODE:
  3069. if (!(sattr->ia_valid & ATTR_SIZE)) {
  3070. pr_warn_once("NFSv4: server %s is incorrectly "
  3071. "applying open mode checks to "
  3072. "a SETATTR that is not "
  3073. "changing file size.\n",
  3074. server->nfs_client->cl_hostname);
  3075. }
  3076. if (state && !(state->state & FMODE_WRITE)) {
  3077. err = -EBADF;
  3078. if (sattr->ia_valid & ATTR_OPEN)
  3079. err = -EACCES;
  3080. goto out;
  3081. }
  3082. }
  3083. err = nfs4_handle_exception(server, err, &exception);
  3084. } while (exception.retry);
  3085. out:
  3086. return err;
  3087. }
  3088. static bool
  3089. nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
  3090. {
  3091. if (inode == NULL || !nfs_have_layout(inode))
  3092. return false;
  3093. return pnfs_wait_on_layoutreturn(inode, task);
  3094. }
  3095. /*
  3096. * Update the seqid of an open stateid
  3097. */
  3098. static void nfs4_sync_open_stateid(nfs4_stateid *dst,
  3099. struct nfs4_state *state)
  3100. {
  3101. __be32 seqid_open;
  3102. u32 dst_seqid;
  3103. int seq;
  3104. for (;;) {
  3105. if (!nfs4_valid_open_stateid(state))
  3106. break;
  3107. seq = read_seqbegin(&state->seqlock);
  3108. if (!nfs4_state_match_open_stateid_other(state, dst)) {
  3109. nfs4_stateid_copy(dst, &state->open_stateid);
  3110. if (read_seqretry(&state->seqlock, seq))
  3111. continue;
  3112. break;
  3113. }
  3114. seqid_open = state->open_stateid.seqid;
  3115. if (read_seqretry(&state->seqlock, seq))
  3116. continue;
  3117. dst_seqid = be32_to_cpu(dst->seqid);
  3118. if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) < 0)
  3119. dst->seqid = seqid_open;
  3120. break;
  3121. }
  3122. }
  3123. /*
  3124. * Update the seqid of an open stateid after receiving
  3125. * NFS4ERR_OLD_STATEID
  3126. */
  3127. static bool nfs4_refresh_open_old_stateid(nfs4_stateid *dst,
  3128. struct nfs4_state *state)
  3129. {
  3130. __be32 seqid_open;
  3131. u32 dst_seqid;
  3132. bool ret;
  3133. int seq, status = -EAGAIN;
  3134. DEFINE_WAIT(wait);
  3135. for (;;) {
  3136. ret = false;
  3137. if (!nfs4_valid_open_stateid(state))
  3138. break;
  3139. seq = read_seqbegin(&state->seqlock);
  3140. if (!nfs4_state_match_open_stateid_other(state, dst)) {
  3141. if (read_seqretry(&state->seqlock, seq))
  3142. continue;
  3143. break;
  3144. }
  3145. write_seqlock(&state->seqlock);
  3146. seqid_open = state->open_stateid.seqid;
  3147. dst_seqid = be32_to_cpu(dst->seqid);
  3148. /* Did another OPEN bump the state's seqid? try again: */
  3149. if ((s32)(be32_to_cpu(seqid_open) - dst_seqid) > 0) {
  3150. dst->seqid = seqid_open;
  3151. write_sequnlock(&state->seqlock);
  3152. ret = true;
  3153. break;
  3154. }
  3155. /* server says we're behind but we haven't seen the update yet */
  3156. set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
  3157. prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
  3158. write_sequnlock(&state->seqlock);
  3159. trace_nfs4_close_stateid_update_wait(state->inode, dst, 0);
  3160. if (fatal_signal_pending(current) || nfs_current_task_exiting())
  3161. status = -EINTR;
  3162. else
  3163. if (schedule_timeout(5*HZ) != 0)
  3164. status = 0;
  3165. finish_wait(&state->waitq, &wait);
  3166. if (!status)
  3167. continue;
  3168. if (status == -EINTR)
  3169. break;
  3170. /* we slept the whole 5 seconds, we must have lost a seqid */
  3171. dst->seqid = cpu_to_be32(dst_seqid + 1);
  3172. ret = true;
  3173. break;
  3174. }
  3175. return ret;
  3176. }
  3177. struct nfs4_closedata {
  3178. struct inode *inode;
  3179. struct nfs4_state *state;
  3180. struct nfs_closeargs arg;
  3181. struct nfs_closeres res;
  3182. struct {
  3183. struct nfs4_layoutreturn_args arg;
  3184. struct nfs4_layoutreturn_res res;
  3185. struct nfs4_xdr_opaque_data ld_private;
  3186. u32 roc_barrier;
  3187. bool roc;
  3188. } lr;
  3189. struct nfs_fattr fattr;
  3190. unsigned long timestamp;
  3191. unsigned short retrans;
  3192. };
  3193. static void nfs4_free_closedata(void *data)
  3194. {
  3195. struct nfs4_closedata *calldata = data;
  3196. struct nfs4_state_owner *sp = calldata->state->owner;
  3197. struct super_block *sb = calldata->state->inode->i_sb;
  3198. if (calldata->lr.roc)
  3199. pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
  3200. calldata->res.lr_ret);
  3201. nfs4_put_open_state(calldata->state);
  3202. nfs_free_seqid(calldata->arg.seqid);
  3203. nfs4_put_state_owner(sp);
  3204. nfs_sb_deactive(sb);
  3205. kfree(calldata);
  3206. }
  3207. static void nfs4_close_done(struct rpc_task *task, void *data)
  3208. {
  3209. struct nfs4_closedata *calldata = data;
  3210. struct nfs4_state *state = calldata->state;
  3211. struct nfs_server *server = NFS_SERVER(calldata->inode);
  3212. nfs4_stateid *res_stateid = NULL;
  3213. struct nfs4_exception exception = {
  3214. .state = state,
  3215. .inode = calldata->inode,
  3216. .stateid = &calldata->arg.stateid,
  3217. .retrans = calldata->retrans,
  3218. };
  3219. if (!nfs4_sequence_done(task, &calldata->res.seq_res))
  3220. return;
  3221. trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
  3222. /* Handle Layoutreturn errors */
  3223. if (pnfs_roc_done(task, &calldata->arg.lr_args, &calldata->res.lr_res,
  3224. &calldata->res.lr_ret) == -EAGAIN)
  3225. goto out_restart;
  3226. /* hmm. we are done with the inode, and in the process of freeing
  3227. * the state_owner. we keep this around to process errors
  3228. */
  3229. switch (task->tk_status) {
  3230. case 0:
  3231. res_stateid = &calldata->res.stateid;
  3232. renew_lease(server, calldata->timestamp);
  3233. break;
  3234. case -NFS4ERR_ACCESS:
  3235. if (calldata->arg.bitmask != NULL) {
  3236. calldata->arg.bitmask = NULL;
  3237. calldata->res.fattr = NULL;
  3238. goto out_restart;
  3239. }
  3240. break;
  3241. case -NFS4ERR_OLD_STATEID:
  3242. /* Did we race with OPEN? */
  3243. if (nfs4_refresh_open_old_stateid(&calldata->arg.stateid,
  3244. state))
  3245. goto out_restart;
  3246. goto out_release;
  3247. case -NFS4ERR_ADMIN_REVOKED:
  3248. case -NFS4ERR_STALE_STATEID:
  3249. case -NFS4ERR_EXPIRED:
  3250. nfs4_free_revoked_stateid(server,
  3251. &calldata->arg.stateid,
  3252. task->tk_msg.rpc_cred);
  3253. fallthrough;
  3254. case -NFS4ERR_BAD_STATEID:
  3255. if (calldata->arg.fmode == 0)
  3256. break;
  3257. fallthrough;
  3258. default:
  3259. task->tk_status = nfs4_async_handle_exception(task,
  3260. server, task->tk_status, &exception);
  3261. calldata->retrans = exception.retrans;
  3262. if (exception.retry)
  3263. goto out_restart;
  3264. }
  3265. nfs_clear_open_stateid(state, &calldata->arg.stateid,
  3266. res_stateid, calldata->arg.fmode);
  3267. out_release:
  3268. task->tk_status = 0;
  3269. nfs_release_seqid(calldata->arg.seqid);
  3270. nfs_refresh_inode(calldata->inode, &calldata->fattr);
  3271. dprintk("%s: ret = %d\n", __func__, task->tk_status);
  3272. return;
  3273. out_restart:
  3274. task->tk_status = 0;
  3275. rpc_restart_call_prepare(task);
  3276. goto out_release;
  3277. }
  3278. static void nfs4_close_prepare(struct rpc_task *task, void *data)
  3279. {
  3280. struct nfs4_closedata *calldata = data;
  3281. struct nfs4_state *state = calldata->state;
  3282. struct inode *inode = calldata->inode;
  3283. struct nfs_server *server = NFS_SERVER(inode);
  3284. struct pnfs_layout_hdr *lo;
  3285. bool is_rdonly, is_wronly, is_rdwr;
  3286. int call_close = 0;
  3287. if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
  3288. goto out_wait;
  3289. task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
  3290. spin_lock(&state->owner->so_lock);
  3291. is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
  3292. is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
  3293. is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
  3294. /* Calculate the change in open mode */
  3295. calldata->arg.fmode = 0;
  3296. if (state->n_rdwr == 0) {
  3297. if (state->n_rdonly == 0)
  3298. call_close |= is_rdonly;
  3299. else if (is_rdonly)
  3300. calldata->arg.fmode |= FMODE_READ;
  3301. if (state->n_wronly == 0)
  3302. call_close |= is_wronly;
  3303. else if (is_wronly)
  3304. calldata->arg.fmode |= FMODE_WRITE;
  3305. if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
  3306. call_close |= is_rdwr;
  3307. } else if (is_rdwr)
  3308. calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
  3309. nfs4_sync_open_stateid(&calldata->arg.stateid, state);
  3310. if (!nfs4_valid_open_stateid(state))
  3311. call_close = 0;
  3312. spin_unlock(&state->owner->so_lock);
  3313. if (!call_close) {
  3314. /* Note: exit _without_ calling nfs4_close_done */
  3315. goto out_no_action;
  3316. }
  3317. if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
  3318. nfs_release_seqid(calldata->arg.seqid);
  3319. goto out_wait;
  3320. }
  3321. lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
  3322. if (lo && !pnfs_layout_is_valid(lo)) {
  3323. calldata->arg.lr_args = NULL;
  3324. calldata->res.lr_res = NULL;
  3325. }
  3326. if (calldata->arg.fmode == 0)
  3327. task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
  3328. if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
  3329. /* Close-to-open cache consistency revalidation */
  3330. if (!nfs4_have_delegation(inode, FMODE_READ, 0)) {
  3331. nfs4_bitmask_set(calldata->arg.bitmask_store,
  3332. server->cache_consistency_bitmask,
  3333. inode, 0);
  3334. calldata->arg.bitmask = calldata->arg.bitmask_store;
  3335. } else
  3336. calldata->arg.bitmask = NULL;
  3337. }
  3338. calldata->arg.share_access =
  3339. nfs4_fmode_to_share_access(calldata->arg.fmode);
  3340. if (calldata->res.fattr == NULL)
  3341. calldata->arg.bitmask = NULL;
  3342. else if (calldata->arg.bitmask == NULL)
  3343. calldata->res.fattr = NULL;
  3344. calldata->timestamp = jiffies;
  3345. if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
  3346. &calldata->arg.seq_args,
  3347. &calldata->res.seq_res,
  3348. task) != 0)
  3349. nfs_release_seqid(calldata->arg.seqid);
  3350. return;
  3351. out_no_action:
  3352. task->tk_action = NULL;
  3353. out_wait:
  3354. nfs4_sequence_done(task, &calldata->res.seq_res);
  3355. }
  3356. static const struct rpc_call_ops nfs4_close_ops = {
  3357. .rpc_call_prepare = nfs4_close_prepare,
  3358. .rpc_call_done = nfs4_close_done,
  3359. .rpc_release = nfs4_free_closedata,
  3360. };
  3361. /*
  3362. * It is possible for data to be read/written from a mem-mapped file
  3363. * after the sys_close call (which hits the vfs layer as a flush).
  3364. * This means that we can't safely call nfsv4 close on a file until
  3365. * the inode is cleared. This in turn means that we are not good
  3366. * NFSv4 citizens - we do not indicate to the server to update the file's
  3367. * share state even when we are done with one of the three share
  3368. * stateid's in the inode.
  3369. *
  3370. * NOTE: Caller must be holding the sp->so_owner semaphore!
  3371. */
  3372. int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
  3373. {
  3374. struct nfs_server *server = NFS_SERVER(state->inode);
  3375. struct nfs_client *clp = server->nfs_client;
  3376. struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
  3377. struct nfs4_closedata *calldata;
  3378. struct nfs4_state_owner *sp = state->owner;
  3379. struct rpc_task *task;
  3380. struct rpc_message msg = {
  3381. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
  3382. .rpc_cred = state->owner->so_cred,
  3383. };
  3384. struct rpc_task_setup task_setup_data = {
  3385. .rpc_client = server->client,
  3386. .rpc_message = &msg,
  3387. .callback_ops = &nfs4_close_ops,
  3388. .workqueue = nfsiod_workqueue,
  3389. .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
  3390. };
  3391. int status = -ENOMEM;
  3392. if (nfs_server_capable(state->inode, NFS_CAP_MOVEABLE))
  3393. task_setup_data.flags |= RPC_TASK_MOVEABLE;
  3394. nfs4_state_protect(clp, NFS_SP4_MACH_CRED_CLEANUP,
  3395. &task_setup_data.rpc_client, &msg);
  3396. calldata = kzalloc_obj(*calldata, gfp_mask);
  3397. if (calldata == NULL)
  3398. goto out;
  3399. nfs4_init_sequence(clp, &calldata->arg.seq_args,
  3400. &calldata->res.seq_res, 1, 0);
  3401. calldata->inode = state->inode;
  3402. calldata->state = state;
  3403. calldata->arg.fh = NFS_FH(state->inode);
  3404. if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
  3405. goto out_free_calldata;
  3406. /* Serialization for the sequence id */
  3407. alloc_seqid = clp->cl_mvops->alloc_seqid;
  3408. calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
  3409. if (IS_ERR(calldata->arg.seqid))
  3410. goto out_free_calldata;
  3411. nfs_fattr_init(&calldata->fattr);
  3412. calldata->arg.fmode = 0;
  3413. calldata->lr.arg.ld_private = &calldata->lr.ld_private;
  3414. calldata->res.fattr = &calldata->fattr;
  3415. calldata->res.seqid = calldata->arg.seqid;
  3416. calldata->res.server = server;
  3417. calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
  3418. calldata->lr.roc = pnfs_roc(state->inode, &calldata->lr.arg,
  3419. &calldata->lr.res, msg.rpc_cred, wait);
  3420. if (calldata->lr.roc) {
  3421. calldata->arg.lr_args = &calldata->lr.arg;
  3422. calldata->res.lr_res = &calldata->lr.res;
  3423. }
  3424. nfs_sb_active(calldata->inode->i_sb);
  3425. msg.rpc_argp = &calldata->arg;
  3426. msg.rpc_resp = &calldata->res;
  3427. task_setup_data.callback_data = calldata;
  3428. task = rpc_run_task(&task_setup_data);
  3429. if (IS_ERR(task))
  3430. return PTR_ERR(task);
  3431. status = 0;
  3432. if (wait)
  3433. status = rpc_wait_for_completion_task(task);
  3434. rpc_put_task(task);
  3435. return status;
  3436. out_free_calldata:
  3437. kfree(calldata);
  3438. out:
  3439. nfs4_put_open_state(state);
  3440. nfs4_put_state_owner(sp);
  3441. return status;
  3442. }
  3443. static struct inode *
  3444. nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
  3445. int open_flags, struct iattr *attr, int *opened)
  3446. {
  3447. struct nfs4_state *state;
  3448. struct nfs4_label l, *label;
  3449. label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
  3450. /* Protect against concurrent sillydeletes */
  3451. state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
  3452. nfs4_label_release_security(label);
  3453. if (IS_ERR(state))
  3454. return ERR_CAST(state);
  3455. return state->inode;
  3456. }
  3457. static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
  3458. {
  3459. struct dentry *dentry = ctx->dentry;
  3460. if (ctx->state == NULL)
  3461. return;
  3462. if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
  3463. nfs4_inode_set_return_delegation_on_close(d_inode(dentry));
  3464. if (is_sync)
  3465. nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
  3466. else
  3467. nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
  3468. }
  3469. #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
  3470. #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
  3471. #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_OPEN_ARGUMENTS - 1UL)
  3472. #define FATTR4_WORD2_NFS42_TIME_DELEG_MASK \
  3473. (FATTR4_WORD2_TIME_DELEG_MODIFY|FATTR4_WORD2_TIME_DELEG_ACCESS)
  3474. static bool nfs4_server_delegtime_capable(struct nfs4_server_caps_res *res)
  3475. {
  3476. u32 share_access_want = res->open_caps.oa_share_access_want[0];
  3477. u32 attr_bitmask = res->attr_bitmask[2];
  3478. return (share_access_want & NFS4_SHARE_WANT_DELEG_TIMESTAMPS) &&
  3479. ((attr_bitmask & FATTR4_WORD2_NFS42_TIME_DELEG_MASK) ==
  3480. FATTR4_WORD2_NFS42_TIME_DELEG_MASK);
  3481. }
  3482. static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
  3483. {
  3484. u32 minorversion = server->nfs_client->cl_minorversion;
  3485. u32 bitmask[3] = {
  3486. [0] = FATTR4_WORD0_SUPPORTED_ATTRS,
  3487. };
  3488. struct nfs4_server_caps_arg args = {
  3489. .fhandle = fhandle,
  3490. .bitmask = bitmask,
  3491. };
  3492. struct nfs4_server_caps_res res = {};
  3493. struct rpc_message msg = {
  3494. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
  3495. .rpc_argp = &args,
  3496. .rpc_resp = &res,
  3497. };
  3498. int status;
  3499. int i;
  3500. bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
  3501. FATTR4_WORD0_FH_EXPIRE_TYPE |
  3502. FATTR4_WORD0_LINK_SUPPORT |
  3503. FATTR4_WORD0_SYMLINK_SUPPORT |
  3504. FATTR4_WORD0_ACLSUPPORT |
  3505. FATTR4_WORD0_CASE_INSENSITIVE |
  3506. FATTR4_WORD0_CASE_PRESERVING;
  3507. if (minorversion)
  3508. bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
  3509. if (minorversion > 1)
  3510. bitmask[2] |= FATTR4_WORD2_OPEN_ARGUMENTS;
  3511. status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  3512. if (status == 0) {
  3513. bitmask[0] = (FATTR4_WORD0_SUPPORTED_ATTRS |
  3514. FATTR4_WORD0_FH_EXPIRE_TYPE |
  3515. FATTR4_WORD0_LINK_SUPPORT |
  3516. FATTR4_WORD0_SYMLINK_SUPPORT |
  3517. FATTR4_WORD0_ACLSUPPORT |
  3518. FATTR4_WORD0_CASE_INSENSITIVE |
  3519. FATTR4_WORD0_CASE_PRESERVING) &
  3520. res.attr_bitmask[0];
  3521. /* Sanity check the server answers */
  3522. switch (minorversion) {
  3523. case 0:
  3524. res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
  3525. res.attr_bitmask[2] = 0;
  3526. break;
  3527. case 1:
  3528. res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
  3529. bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT &
  3530. res.attr_bitmask[2];
  3531. break;
  3532. case 2:
  3533. res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
  3534. bitmask[2] = (FATTR4_WORD2_SUPPATTR_EXCLCREAT |
  3535. FATTR4_WORD2_OPEN_ARGUMENTS) &
  3536. res.attr_bitmask[2];
  3537. }
  3538. memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
  3539. server->caps &=
  3540. ~(NFS_CAP_ACLS | NFS_CAP_HARDLINKS | NFS_CAP_SYMLINKS |
  3541. NFS_CAP_SECURITY_LABEL | NFS_CAP_FS_LOCATIONS |
  3542. NFS_CAP_OPEN_XOR | NFS_CAP_DELEGTIME);
  3543. server->fattr_valid = NFS_ATTR_FATTR_V4;
  3544. if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
  3545. res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
  3546. server->caps |= NFS_CAP_ACLS;
  3547. if (res.has_links != 0)
  3548. server->caps |= NFS_CAP_HARDLINKS;
  3549. if (res.has_symlinks != 0)
  3550. server->caps |= NFS_CAP_SYMLINKS;
  3551. if (res.case_insensitive)
  3552. server->caps |= NFS_CAP_CASE_INSENSITIVE;
  3553. if (res.case_preserving)
  3554. server->caps |= NFS_CAP_CASE_PRESERVING;
  3555. #ifdef CONFIG_NFS_V4_SECURITY_LABEL
  3556. if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
  3557. server->caps |= NFS_CAP_SECURITY_LABEL;
  3558. #endif
  3559. if (res.attr_bitmask[0] & FATTR4_WORD0_FS_LOCATIONS)
  3560. server->caps |= NFS_CAP_FS_LOCATIONS;
  3561. if (!(res.attr_bitmask[0] & FATTR4_WORD0_FILEID))
  3562. server->fattr_valid &= ~NFS_ATTR_FATTR_FILEID;
  3563. if (!(res.attr_bitmask[1] & FATTR4_WORD1_MODE))
  3564. server->fattr_valid &= ~NFS_ATTR_FATTR_MODE;
  3565. if (!(res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS))
  3566. server->fattr_valid &= ~NFS_ATTR_FATTR_NLINK;
  3567. if (!(res.attr_bitmask[1] & FATTR4_WORD1_OWNER))
  3568. server->fattr_valid &= ~(NFS_ATTR_FATTR_OWNER |
  3569. NFS_ATTR_FATTR_OWNER_NAME);
  3570. if (!(res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP))
  3571. server->fattr_valid &= ~(NFS_ATTR_FATTR_GROUP |
  3572. NFS_ATTR_FATTR_GROUP_NAME);
  3573. if (!(res.attr_bitmask[1] & FATTR4_WORD1_SPACE_USED))
  3574. server->fattr_valid &= ~NFS_ATTR_FATTR_SPACE_USED;
  3575. if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS))
  3576. server->fattr_valid &= ~NFS_ATTR_FATTR_ATIME;
  3577. if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA))
  3578. server->fattr_valid &= ~NFS_ATTR_FATTR_CTIME;
  3579. if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY))
  3580. server->fattr_valid &= ~NFS_ATTR_FATTR_MTIME;
  3581. if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY))
  3582. server->fattr_valid &= ~NFS_ATTR_FATTR_MTIME;
  3583. if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_CREATE))
  3584. server->fattr_valid &= ~NFS_ATTR_FATTR_BTIME;
  3585. memcpy(server->attr_bitmask_nl, res.attr_bitmask,
  3586. sizeof(server->attr_bitmask));
  3587. server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
  3588. if (res.open_caps.oa_share_access_want[0] &
  3589. NFS4_SHARE_WANT_OPEN_XOR_DELEGATION)
  3590. server->caps |= NFS_CAP_OPEN_XOR;
  3591. if (nfs4_server_delegtime_capable(&res))
  3592. server->caps |= NFS_CAP_DELEGTIME;
  3593. memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
  3594. server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
  3595. server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
  3596. server->cache_consistency_bitmask[2] = 0;
  3597. /* Avoid a regression due to buggy server */
  3598. for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
  3599. res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
  3600. memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
  3601. sizeof(server->exclcreat_bitmask));
  3602. server->acl_bitmask = res.acl_bitmask;
  3603. server->fh_expire_type = res.fh_expire_type;
  3604. }
  3605. return status;
  3606. }
  3607. int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
  3608. {
  3609. struct nfs4_exception exception = {
  3610. .interruptible = true,
  3611. };
  3612. int err;
  3613. do {
  3614. err = nfs4_handle_exception(server,
  3615. _nfs4_server_capabilities(server, fhandle),
  3616. &exception);
  3617. } while (exception.retry);
  3618. return err;
  3619. }
  3620. static void test_fs_location_for_trunking(struct nfs4_fs_location *location,
  3621. struct nfs_client *clp,
  3622. struct nfs_server *server)
  3623. {
  3624. int i;
  3625. for (i = 0; i < location->nservers; i++) {
  3626. struct nfs4_string *srv_loc = &location->servers[i];
  3627. struct sockaddr_storage addr;
  3628. size_t addrlen;
  3629. struct xprt_create xprt_args = {
  3630. .ident = 0,
  3631. .net = clp->cl_net,
  3632. };
  3633. struct nfs4_add_xprt_data xprtdata = {
  3634. .clp = clp,
  3635. };
  3636. struct rpc_add_xprt_test rpcdata = {
  3637. .add_xprt_test = clp->cl_mvops->session_trunk,
  3638. .data = &xprtdata,
  3639. };
  3640. char *servername = NULL;
  3641. if (!srv_loc->len)
  3642. continue;
  3643. addrlen = nfs_parse_server_name(srv_loc->data, srv_loc->len,
  3644. &addr, sizeof(addr),
  3645. clp->cl_net, server->port);
  3646. if (!addrlen)
  3647. return;
  3648. xprt_args.dstaddr = (struct sockaddr *)&addr;
  3649. xprt_args.addrlen = addrlen;
  3650. servername = kmalloc(srv_loc->len + 1, GFP_KERNEL);
  3651. if (!servername)
  3652. return;
  3653. memcpy(servername, srv_loc->data, srv_loc->len);
  3654. servername[srv_loc->len] = '\0';
  3655. xprt_args.servername = servername;
  3656. xprtdata.cred = nfs4_get_clid_cred(clp);
  3657. rpc_clnt_add_xprt(clp->cl_rpcclient, &xprt_args,
  3658. rpc_clnt_setup_test_and_add_xprt,
  3659. &rpcdata);
  3660. if (xprtdata.cred)
  3661. put_cred(xprtdata.cred);
  3662. kfree(servername);
  3663. }
  3664. }
  3665. static bool _is_same_nfs4_pathname(struct nfs4_pathname *path1,
  3666. struct nfs4_pathname *path2)
  3667. {
  3668. int i;
  3669. if (path1->ncomponents != path2->ncomponents)
  3670. return false;
  3671. for (i = 0; i < path1->ncomponents; i++) {
  3672. if (path1->components[i].len != path2->components[i].len)
  3673. return false;
  3674. if (memcmp(path1->components[i].data, path2->components[i].data,
  3675. path1->components[i].len))
  3676. return false;
  3677. }
  3678. return true;
  3679. }
  3680. static int _nfs4_discover_trunking(struct nfs_server *server,
  3681. struct nfs_fh *fhandle)
  3682. {
  3683. struct nfs4_fs_locations *locations = NULL;
  3684. struct page *page;
  3685. const struct cred *cred;
  3686. struct nfs_client *clp = server->nfs_client;
  3687. const struct nfs4_state_maintenance_ops *ops =
  3688. clp->cl_mvops->state_renewal_ops;
  3689. int status = -ENOMEM, i;
  3690. cred = ops->get_state_renewal_cred(clp);
  3691. if (cred == NULL) {
  3692. cred = nfs4_get_clid_cred(clp);
  3693. if (cred == NULL)
  3694. return -ENOKEY;
  3695. }
  3696. page = alloc_page(GFP_KERNEL);
  3697. if (!page)
  3698. goto out_put_cred;
  3699. locations = kmalloc_obj(struct nfs4_fs_locations);
  3700. if (!locations)
  3701. goto out_free;
  3702. locations->fattr = nfs_alloc_fattr();
  3703. if (!locations->fattr)
  3704. goto out_free_2;
  3705. status = nfs4_proc_get_locations(server, fhandle, locations, page,
  3706. cred);
  3707. if (status)
  3708. goto out_free_3;
  3709. for (i = 0; i < locations->nlocations; i++) {
  3710. if (!_is_same_nfs4_pathname(&locations->fs_path,
  3711. &locations->locations[i].rootpath))
  3712. continue;
  3713. test_fs_location_for_trunking(&locations->locations[i], clp,
  3714. server);
  3715. }
  3716. out_free_3:
  3717. kfree(locations->fattr);
  3718. out_free_2:
  3719. kfree(locations);
  3720. out_free:
  3721. __free_page(page);
  3722. out_put_cred:
  3723. put_cred(cred);
  3724. return status;
  3725. }
  3726. static int nfs4_discover_trunking(struct nfs_server *server,
  3727. struct nfs_fh *fhandle)
  3728. {
  3729. struct nfs4_exception exception = {
  3730. .interruptible = true,
  3731. };
  3732. struct nfs_client *clp = server->nfs_client;
  3733. int err = 0;
  3734. if (!nfs4_has_session(clp))
  3735. goto out;
  3736. do {
  3737. err = nfs4_handle_exception(server,
  3738. _nfs4_discover_trunking(server, fhandle),
  3739. &exception);
  3740. } while (exception.retry);
  3741. out:
  3742. return err;
  3743. }
  3744. static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
  3745. struct nfs_fattr *fattr)
  3746. {
  3747. u32 bitmask[3] = {
  3748. [0] = FATTR4_WORD0_TYPE | FATTR4_WORD0_CHANGE |
  3749. FATTR4_WORD0_SIZE | FATTR4_WORD0_FSID,
  3750. };
  3751. struct nfs4_lookup_root_arg args = {
  3752. .bitmask = bitmask,
  3753. };
  3754. struct nfs4_lookup_res res = {
  3755. .server = server,
  3756. .fattr = fattr,
  3757. .fh = fhandle,
  3758. };
  3759. struct rpc_message msg = {
  3760. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
  3761. .rpc_argp = &args,
  3762. .rpc_resp = &res,
  3763. };
  3764. nfs_fattr_init(fattr);
  3765. return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  3766. }
  3767. static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
  3768. struct nfs_fattr *fattr)
  3769. {
  3770. struct nfs4_exception exception = {
  3771. .interruptible = true,
  3772. };
  3773. int err;
  3774. do {
  3775. err = _nfs4_lookup_root(server, fhandle, fattr);
  3776. trace_nfs4_lookup_root(server, fhandle, fattr, err);
  3777. switch (err) {
  3778. case 0:
  3779. case -NFS4ERR_WRONGSEC:
  3780. goto out;
  3781. default:
  3782. err = nfs4_handle_exception(server, err, &exception);
  3783. }
  3784. } while (exception.retry);
  3785. out:
  3786. return err;
  3787. }
  3788. static int nfs4_lookup_root_sec(struct nfs_server *server,
  3789. struct nfs_fh *fhandle, struct nfs_fattr *fattr,
  3790. rpc_authflavor_t flavor)
  3791. {
  3792. struct rpc_auth_create_args auth_args = {
  3793. .pseudoflavor = flavor,
  3794. };
  3795. struct rpc_auth *auth;
  3796. auth = rpcauth_create(&auth_args, server->client);
  3797. if (IS_ERR(auth))
  3798. return -EACCES;
  3799. return nfs4_lookup_root(server, fhandle, fattr);
  3800. }
  3801. /*
  3802. * Retry pseudoroot lookup with various security flavors. We do this when:
  3803. *
  3804. * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
  3805. * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
  3806. *
  3807. * Returns zero on success, or a negative NFS4ERR value, or a
  3808. * negative errno value.
  3809. */
  3810. int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
  3811. struct nfs_fattr *fattr)
  3812. {
  3813. /* Per 3530bis 15.33.5 */
  3814. static const rpc_authflavor_t flav_array[] = {
  3815. RPC_AUTH_GSS_KRB5P,
  3816. RPC_AUTH_GSS_KRB5I,
  3817. RPC_AUTH_GSS_KRB5,
  3818. RPC_AUTH_UNIX, /* courtesy */
  3819. RPC_AUTH_NULL,
  3820. };
  3821. int status = -EPERM;
  3822. size_t i;
  3823. if (server->auth_info.flavor_len > 0) {
  3824. /* try each flavor specified by user */
  3825. for (i = 0; i < server->auth_info.flavor_len; i++) {
  3826. status = nfs4_lookup_root_sec(
  3827. server, fhandle, fattr,
  3828. server->auth_info.flavors[i]);
  3829. if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
  3830. continue;
  3831. break;
  3832. }
  3833. } else {
  3834. /* no flavors specified by user, try default list */
  3835. for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
  3836. status = nfs4_lookup_root_sec(server, fhandle, fattr,
  3837. flav_array[i]);
  3838. if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
  3839. continue;
  3840. break;
  3841. }
  3842. }
  3843. /*
  3844. * -EACCES could mean that the user doesn't have correct permissions
  3845. * to access the mount. It could also mean that we tried to mount
  3846. * with a gss auth flavor, but rpc.gssd isn't running. Either way,
  3847. * existing mount programs don't handle -EACCES very well so it should
  3848. * be mapped to -EPERM instead.
  3849. */
  3850. if (status == -EACCES)
  3851. status = -EPERM;
  3852. return status;
  3853. }
  3854. /**
  3855. * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
  3856. * @server: initialized nfs_server handle
  3857. * @fhandle: we fill in the pseudo-fs root file handle
  3858. * @fattr: we fill in a bare bones struct fattr
  3859. * @auth_probe: probe the auth flavours
  3860. *
  3861. * Returns zero on success, or a negative errno.
  3862. */
  3863. int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
  3864. struct nfs_fattr *fattr, bool auth_probe)
  3865. {
  3866. int status = 0;
  3867. if (!auth_probe)
  3868. status = nfs4_lookup_root(server, fhandle, fattr);
  3869. if (auth_probe || status == NFS4ERR_WRONGSEC)
  3870. status = server->nfs_client->cl_mvops->find_root_sec(
  3871. server, fhandle, fattr);
  3872. return nfs4_map_errors(status);
  3873. }
  3874. static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
  3875. struct nfs_fsinfo *info)
  3876. {
  3877. int error;
  3878. struct nfs_fattr *fattr = info->fattr;
  3879. error = nfs4_server_capabilities(server, mntfh);
  3880. if (error < 0) {
  3881. dprintk("nfs4_get_root: getcaps error = %d\n", -error);
  3882. return error;
  3883. }
  3884. error = nfs4_proc_getattr(server, mntfh, fattr, NULL);
  3885. if (error < 0) {
  3886. dprintk("nfs4_get_root: getattr error = %d\n", -error);
  3887. goto out;
  3888. }
  3889. if (fattr->valid & NFS_ATTR_FATTR_FSID &&
  3890. !nfs_fsid_equal(&server->fsid, &fattr->fsid))
  3891. memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
  3892. out:
  3893. return error;
  3894. }
  3895. /*
  3896. * Get locations and (maybe) other attributes of a referral.
  3897. * Note that we'll actually follow the referral later when
  3898. * we detect fsid mismatch in inode revalidation
  3899. */
  3900. static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
  3901. const struct qstr *name, struct nfs_fattr *fattr,
  3902. struct nfs_fh *fhandle)
  3903. {
  3904. int status = -ENOMEM;
  3905. struct page *page = NULL;
  3906. struct nfs4_fs_locations *locations = NULL;
  3907. page = alloc_page(GFP_KERNEL);
  3908. if (page == NULL)
  3909. goto out;
  3910. locations = kmalloc_obj(struct nfs4_fs_locations);
  3911. if (locations == NULL)
  3912. goto out;
  3913. locations->fattr = fattr;
  3914. status = nfs4_proc_fs_locations(client, dir, name, locations, page);
  3915. if (status != 0)
  3916. goto out;
  3917. /*
  3918. * If the fsid didn't change, this is a migration event, not a
  3919. * referral. Cause us to drop into the exception handler, which
  3920. * will kick off migration recovery.
  3921. */
  3922. if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &fattr->fsid)) {
  3923. dprintk("%s: server did not return a different fsid for"
  3924. " a referral at %s\n", __func__, name->name);
  3925. status = -NFS4ERR_MOVED;
  3926. goto out;
  3927. }
  3928. /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
  3929. nfs_fixup_referral_attributes(fattr);
  3930. memset(fhandle, 0, sizeof(struct nfs_fh));
  3931. out:
  3932. if (page)
  3933. __free_page(page);
  3934. kfree(locations);
  3935. return status;
  3936. }
  3937. static bool should_request_dir_deleg(struct inode *inode)
  3938. {
  3939. if (!directory_delegations)
  3940. return false;
  3941. if (!inode)
  3942. return false;
  3943. if (!S_ISDIR(inode->i_mode))
  3944. return false;
  3945. if (!nfs_server_capable(inode, NFS_CAP_DIR_DELEG))
  3946. return false;
  3947. if (!test_and_clear_bit(NFS_INO_REQ_DIR_DELEG, &(NFS_I(inode)->flags)))
  3948. return false;
  3949. if (nfs4_have_delegation(inode, FMODE_READ, 0))
  3950. return false;
  3951. return true;
  3952. }
  3953. static void nfs4_call_getattr_prepare(struct rpc_task *task, void *calldata)
  3954. {
  3955. struct nfs4_call_sync_data *data = calldata;
  3956. nfs4_setup_sequence(data->seq_server->nfs_client, data->seq_args,
  3957. data->seq_res, task);
  3958. }
  3959. static void nfs4_call_getattr_done(struct rpc_task *task, void *calldata)
  3960. {
  3961. struct nfs4_call_sync_data *data = calldata;
  3962. nfs4_sequence_process(task, data->seq_res);
  3963. }
  3964. static const struct rpc_call_ops nfs4_call_getattr_ops = {
  3965. .rpc_call_prepare = nfs4_call_getattr_prepare,
  3966. .rpc_call_done = nfs4_call_getattr_done,
  3967. };
  3968. static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
  3969. struct nfs_fattr *fattr, struct inode *inode)
  3970. {
  3971. __u32 bitmask[NFS4_BITMASK_SZ];
  3972. struct nfs4_getattr_arg args = {
  3973. .fh = fhandle,
  3974. .bitmask = bitmask,
  3975. };
  3976. struct nfs4_getattr_res res = {
  3977. .fattr = fattr,
  3978. .server = server,
  3979. };
  3980. struct rpc_message msg = {
  3981. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
  3982. .rpc_argp = &args,
  3983. .rpc_resp = &res,
  3984. };
  3985. struct nfs4_call_sync_data data = {
  3986. .seq_server = server,
  3987. .seq_args = &args.seq_args,
  3988. .seq_res = &res.seq_res,
  3989. };
  3990. struct rpc_task_setup task_setup = {
  3991. .rpc_client = server->client,
  3992. .rpc_message = &msg,
  3993. .callback_ops = &nfs4_call_getattr_ops,
  3994. .callback_data = &data,
  3995. };
  3996. struct nfs_client *clp = server->nfs_client;
  3997. struct nfs4_gdd_res gdd_res;
  3998. int status;
  3999. if (nfs4_has_session(clp))
  4000. task_setup.flags = RPC_TASK_MOVEABLE;
  4001. /* Is this is an attribute revalidation, subject to softreval? */
  4002. if (inode && (server->flags & NFS_MOUNT_SOFTREVAL))
  4003. task_setup.flags |= RPC_TASK_TIMEOUT;
  4004. args.get_dir_deleg = should_request_dir_deleg(inode);
  4005. if (args.get_dir_deleg)
  4006. res.gdd_res = &gdd_res;
  4007. nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, fattr->label), inode, 0);
  4008. nfs_fattr_init(fattr);
  4009. nfs4_init_sequence(clp, &args.seq_args, &res.seq_res, 0, 0);
  4010. status = nfs4_call_sync_custom(&task_setup);
  4011. if (args.get_dir_deleg) {
  4012. switch (status) {
  4013. case 0:
  4014. if (gdd_res.status != GDD4_OK)
  4015. break;
  4016. nfs_inode_set_delegation(inode, current_cred(),
  4017. FMODE_READ, &gdd_res.deleg, 0,
  4018. NFS4_OPEN_DELEGATE_READ);
  4019. break;
  4020. case -ENOTSUPP:
  4021. case -EOPNOTSUPP:
  4022. server->caps &= ~NFS_CAP_DIR_DELEG;
  4023. }
  4024. }
  4025. nfs4_sequence_free_slot(&res.seq_res);
  4026. return status;
  4027. }
  4028. int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
  4029. struct nfs_fattr *fattr, struct inode *inode)
  4030. {
  4031. struct nfs4_exception exception = {
  4032. .interruptible = true,
  4033. };
  4034. int err;
  4035. do {
  4036. err = _nfs4_proc_getattr(server, fhandle, fattr, inode);
  4037. trace_nfs4_getattr(server, fhandle, fattr, err);
  4038. switch (err) {
  4039. default:
  4040. err = nfs4_handle_exception(server, err, &exception);
  4041. break;
  4042. case -ENOTSUPP:
  4043. case -EOPNOTSUPP:
  4044. exception.retry = true;
  4045. }
  4046. } while (exception.retry);
  4047. return err;
  4048. }
  4049. /*
  4050. * The file is not closed if it is opened due to the a request to change
  4051. * the size of the file. The open call will not be needed once the
  4052. * VFS layer lookup-intents are implemented.
  4053. *
  4054. * Close is called when the inode is destroyed.
  4055. * If we haven't opened the file for O_WRONLY, we
  4056. * need to in the size_change case to obtain a stateid.
  4057. *
  4058. * Got race?
  4059. * Because OPEN is always done by name in nfsv4, it is
  4060. * possible that we opened a different file by the same
  4061. * name. We can recognize this race condition, but we
  4062. * can't do anything about it besides returning an error.
  4063. *
  4064. * This will be fixed with VFS changes (lookup-intent).
  4065. */
  4066. static int
  4067. nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
  4068. struct iattr *sattr)
  4069. {
  4070. struct inode *inode = d_inode(dentry);
  4071. const struct cred *cred = NULL;
  4072. struct nfs_open_context *ctx = NULL;
  4073. int status;
  4074. if (pnfs_ld_layoutret_on_setattr(inode) &&
  4075. sattr->ia_valid & ATTR_SIZE &&
  4076. sattr->ia_size < i_size_read(inode))
  4077. pnfs_commit_and_return_layout(inode);
  4078. nfs_fattr_init(fattr);
  4079. /* Deal with open(O_TRUNC) */
  4080. if (sattr->ia_valid & ATTR_OPEN)
  4081. sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
  4082. /* Optimization: if the end result is no change, don't RPC */
  4083. if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
  4084. return 0;
  4085. /* Search for an existing open(O_WRITE) file */
  4086. if (sattr->ia_valid & ATTR_FILE) {
  4087. ctx = nfs_file_open_context(sattr->ia_file);
  4088. if (ctx)
  4089. cred = ctx->cred;
  4090. }
  4091. /* Return any delegations if we're going to change ACLs */
  4092. if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
  4093. nfs4_inode_make_writeable(inode);
  4094. status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL);
  4095. if (status == 0) {
  4096. nfs_setattr_update_inode(inode, sattr, fattr);
  4097. nfs_setsecurity(inode, fattr);
  4098. }
  4099. return status;
  4100. }
  4101. static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
  4102. struct dentry *dentry, const struct qstr *name,
  4103. struct nfs_fh *fhandle, struct nfs_fattr *fattr)
  4104. {
  4105. struct nfs_server *server = NFS_SERVER(dir);
  4106. int status;
  4107. struct nfs4_lookup_arg args = {
  4108. .bitmask = server->attr_bitmask,
  4109. .dir_fh = NFS_FH(dir),
  4110. .name = name,
  4111. };
  4112. struct nfs4_lookup_res res = {
  4113. .server = server,
  4114. .fattr = fattr,
  4115. .fh = fhandle,
  4116. };
  4117. struct rpc_message msg = {
  4118. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
  4119. .rpc_argp = &args,
  4120. .rpc_resp = &res,
  4121. };
  4122. unsigned short task_flags = 0;
  4123. if (nfs_server_capable(dir, NFS_CAP_MOVEABLE))
  4124. task_flags = RPC_TASK_MOVEABLE;
  4125. /* Is this is an attribute revalidation, subject to softreval? */
  4126. if (nfs_lookup_is_soft_revalidate(dentry))
  4127. task_flags |= RPC_TASK_TIMEOUT;
  4128. args.bitmask = nfs4_bitmask(server, fattr->label);
  4129. nfs_fattr_init(fattr);
  4130. dprintk("NFS call lookup %pd2\n", dentry);
  4131. nfs4_init_sequence(server->nfs_client, &args.seq_args, &res.seq_res, 0, 0);
  4132. status = nfs4_do_call_sync(clnt, server, &msg,
  4133. &args.seq_args, &res.seq_res, task_flags);
  4134. dprintk("NFS reply lookup: %d\n", status);
  4135. return status;
  4136. }
  4137. static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
  4138. {
  4139. fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
  4140. NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
  4141. fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
  4142. fattr->nlink = 2;
  4143. }
  4144. static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
  4145. struct dentry *dentry, const struct qstr *name,
  4146. struct nfs_fh *fhandle, struct nfs_fattr *fattr)
  4147. {
  4148. struct nfs4_exception exception = {
  4149. .interruptible = true,
  4150. };
  4151. struct rpc_clnt *client = *clnt;
  4152. int err;
  4153. do {
  4154. err = _nfs4_proc_lookup(client, dir, dentry, name, fhandle, fattr);
  4155. trace_nfs4_lookup(dir, name, err);
  4156. switch (err) {
  4157. case -NFS4ERR_BADNAME:
  4158. err = -ENOENT;
  4159. goto out;
  4160. case -NFS4ERR_MOVED:
  4161. err = nfs4_get_referral(client, dir, name, fattr, fhandle);
  4162. if (err == -NFS4ERR_MOVED)
  4163. err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
  4164. goto out;
  4165. case -NFS4ERR_WRONGSEC:
  4166. err = -EPERM;
  4167. if (client != *clnt)
  4168. goto out;
  4169. client = nfs4_negotiate_security(client, dir, name);
  4170. if (IS_ERR(client))
  4171. return PTR_ERR(client);
  4172. exception.retry = 1;
  4173. break;
  4174. default:
  4175. err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
  4176. }
  4177. } while (exception.retry);
  4178. out:
  4179. if (err == 0)
  4180. *clnt = client;
  4181. else if (client != *clnt)
  4182. rpc_shutdown_client(client);
  4183. return err;
  4184. }
  4185. static int nfs4_proc_lookup(struct inode *dir, struct dentry *dentry, const struct qstr *name,
  4186. struct nfs_fh *fhandle, struct nfs_fattr *fattr)
  4187. {
  4188. int status;
  4189. struct rpc_clnt *client = NFS_CLIENT(dir);
  4190. status = nfs4_proc_lookup_common(&client, dir, dentry, name, fhandle, fattr);
  4191. if (client != NFS_CLIENT(dir)) {
  4192. rpc_shutdown_client(client);
  4193. nfs_fixup_secinfo_attributes(fattr);
  4194. }
  4195. return status;
  4196. }
  4197. struct rpc_clnt *
  4198. nfs4_proc_lookup_mountpoint(struct inode *dir, struct dentry *dentry,
  4199. struct nfs_fh *fhandle, struct nfs_fattr *fattr)
  4200. {
  4201. struct rpc_clnt *client = NFS_CLIENT(dir);
  4202. int status;
  4203. status = nfs4_proc_lookup_common(&client, dir, dentry, &dentry->d_name,
  4204. fhandle, fattr);
  4205. if (status < 0)
  4206. return ERR_PTR(status);
  4207. return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
  4208. }
  4209. static int _nfs4_proc_lookupp(struct inode *inode,
  4210. struct nfs_fh *fhandle, struct nfs_fattr *fattr)
  4211. {
  4212. struct rpc_clnt *clnt = NFS_CLIENT(inode);
  4213. struct nfs_server *server = NFS_SERVER(inode);
  4214. int status;
  4215. struct nfs4_lookupp_arg args = {
  4216. .bitmask = server->attr_bitmask,
  4217. .fh = NFS_FH(inode),
  4218. };
  4219. struct nfs4_lookupp_res res = {
  4220. .server = server,
  4221. .fattr = fattr,
  4222. .fh = fhandle,
  4223. };
  4224. struct rpc_message msg = {
  4225. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
  4226. .rpc_argp = &args,
  4227. .rpc_resp = &res,
  4228. };
  4229. unsigned short task_flags = 0;
  4230. if (server->flags & NFS_MOUNT_SOFTREVAL)
  4231. task_flags |= RPC_TASK_TIMEOUT;
  4232. if (server->caps & NFS_CAP_MOVEABLE)
  4233. task_flags |= RPC_TASK_MOVEABLE;
  4234. args.bitmask = nfs4_bitmask(server, fattr->label);
  4235. nfs_fattr_init(fattr);
  4236. nfs4_init_sequence(server->nfs_client, &args.seq_args, &res.seq_res, 0, 0);
  4237. dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
  4238. status = nfs4_do_call_sync(clnt, server, &msg, &args.seq_args,
  4239. &res.seq_res, task_flags);
  4240. dprintk("NFS reply lookupp: %d\n", status);
  4241. return status;
  4242. }
  4243. static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
  4244. struct nfs_fattr *fattr)
  4245. {
  4246. struct nfs4_exception exception = {
  4247. .interruptible = true,
  4248. };
  4249. int err;
  4250. do {
  4251. err = _nfs4_proc_lookupp(inode, fhandle, fattr);
  4252. trace_nfs4_lookupp(inode, err);
  4253. err = nfs4_handle_exception(NFS_SERVER(inode), err,
  4254. &exception);
  4255. } while (exception.retry);
  4256. return err;
  4257. }
  4258. static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry,
  4259. const struct cred *cred)
  4260. {
  4261. struct nfs_server *server = NFS_SERVER(inode);
  4262. struct nfs4_accessargs args = {
  4263. .fh = NFS_FH(inode),
  4264. .access = entry->mask,
  4265. };
  4266. struct nfs4_accessres res = {
  4267. .server = server,
  4268. };
  4269. struct rpc_message msg = {
  4270. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
  4271. .rpc_argp = &args,
  4272. .rpc_resp = &res,
  4273. .rpc_cred = cred,
  4274. };
  4275. int status = 0;
  4276. if (!nfs4_have_delegation(inode, FMODE_READ, 0)) {
  4277. nfs_request_directory_delegation(inode);
  4278. res.fattr = nfs_alloc_fattr();
  4279. if (res.fattr == NULL)
  4280. return -ENOMEM;
  4281. args.bitmask = server->cache_consistency_bitmask;
  4282. }
  4283. status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  4284. if (!status) {
  4285. nfs_access_set_mask(entry, res.access);
  4286. if (res.fattr)
  4287. nfs_refresh_inode(inode, res.fattr);
  4288. }
  4289. nfs_free_fattr(res.fattr);
  4290. return status;
  4291. }
  4292. static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry,
  4293. const struct cred *cred)
  4294. {
  4295. struct nfs4_exception exception = {
  4296. .interruptible = true,
  4297. };
  4298. int err;
  4299. do {
  4300. err = _nfs4_proc_access(inode, entry, cred);
  4301. trace_nfs4_access(inode, err);
  4302. err = nfs4_handle_exception(NFS_SERVER(inode), err,
  4303. &exception);
  4304. } while (exception.retry);
  4305. return err;
  4306. }
  4307. /*
  4308. * TODO: For the time being, we don't try to get any attributes
  4309. * along with any of the zero-copy operations READ, READDIR,
  4310. * READLINK, WRITE.
  4311. *
  4312. * In the case of the first three, we want to put the GETATTR
  4313. * after the read-type operation -- this is because it is hard
  4314. * to predict the length of a GETATTR response in v4, and thus
  4315. * align the READ data correctly. This means that the GETATTR
  4316. * may end up partially falling into the page cache, and we should
  4317. * shift it into the 'tail' of the xdr_buf before processing.
  4318. * To do this efficiently, we need to know the total length
  4319. * of data received, which doesn't seem to be available outside
  4320. * of the RPC layer.
  4321. *
  4322. * In the case of WRITE, we also want to put the GETATTR after
  4323. * the operation -- in this case because we want to make sure
  4324. * we get the post-operation mtime and size.
  4325. *
  4326. * Both of these changes to the XDR layer would in fact be quite
  4327. * minor, but I decided to leave them for a subsequent patch.
  4328. */
  4329. static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
  4330. unsigned int pgbase, unsigned int pglen)
  4331. {
  4332. struct nfs4_readlink args = {
  4333. .fh = NFS_FH(inode),
  4334. .pgbase = pgbase,
  4335. .pglen = pglen,
  4336. .pages = &page,
  4337. };
  4338. struct nfs4_readlink_res res;
  4339. struct rpc_message msg = {
  4340. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
  4341. .rpc_argp = &args,
  4342. .rpc_resp = &res,
  4343. };
  4344. return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
  4345. }
  4346. static int nfs4_proc_readlink(struct inode *inode, struct page *page,
  4347. unsigned int pgbase, unsigned int pglen)
  4348. {
  4349. struct nfs4_exception exception = {
  4350. .interruptible = true,
  4351. };
  4352. int err;
  4353. do {
  4354. err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
  4355. trace_nfs4_readlink(inode, err);
  4356. err = nfs4_handle_exception(NFS_SERVER(inode), err,
  4357. &exception);
  4358. } while (exception.retry);
  4359. return err;
  4360. }
  4361. /*
  4362. * This is just for mknod. open(O_CREAT) will always do ->open_context().
  4363. */
  4364. static int
  4365. nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
  4366. int flags)
  4367. {
  4368. struct nfs_server *server = NFS_SERVER(dir);
  4369. struct nfs4_label l, *ilabel;
  4370. struct nfs_open_context *ctx;
  4371. struct nfs4_state *state;
  4372. int status = 0;
  4373. ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
  4374. if (IS_ERR(ctx))
  4375. return PTR_ERR(ctx);
  4376. ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
  4377. nfs_request_directory_delegation(dir);
  4378. if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
  4379. sattr->ia_mode &= ~current_umask();
  4380. state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
  4381. if (IS_ERR(state)) {
  4382. status = PTR_ERR(state);
  4383. goto out;
  4384. }
  4385. out:
  4386. nfs4_label_release_security(ilabel);
  4387. put_nfs_open_context(ctx);
  4388. return status;
  4389. }
  4390. static int
  4391. _nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
  4392. {
  4393. struct nfs_server *server = NFS_SERVER(dir);
  4394. struct nfs_removeargs args = {
  4395. .fh = NFS_FH(dir),
  4396. .name = *name,
  4397. };
  4398. struct nfs_removeres res = {
  4399. .server = server,
  4400. };
  4401. struct rpc_message msg = {
  4402. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
  4403. .rpc_argp = &args,
  4404. .rpc_resp = &res,
  4405. };
  4406. unsigned long timestamp = jiffies;
  4407. int status;
  4408. status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
  4409. if (status == 0) {
  4410. spin_lock(&dir->i_lock);
  4411. /* Removing a directory decrements nlink in the parent */
  4412. if (ftype == NF4DIR && dir->i_nlink > 2)
  4413. nfs4_dec_nlink_locked(dir);
  4414. nfs4_update_changeattr_locked(dir, &res.cinfo, timestamp,
  4415. NFS_INO_INVALID_DATA);
  4416. spin_unlock(&dir->i_lock);
  4417. }
  4418. return status;
  4419. }
  4420. static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
  4421. {
  4422. struct nfs4_exception exception = {
  4423. .interruptible = true,
  4424. };
  4425. struct inode *inode = d_inode(dentry);
  4426. int err;
  4427. if (inode) {
  4428. if (inode->i_nlink == 1)
  4429. nfs4_inode_return_delegation(inode);
  4430. else
  4431. nfs4_inode_make_writeable(inode);
  4432. }
  4433. do {
  4434. err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
  4435. trace_nfs4_remove(dir, &dentry->d_name, err);
  4436. err = nfs4_handle_exception(NFS_SERVER(dir), err,
  4437. &exception);
  4438. } while (exception.retry);
  4439. return err;
  4440. }
  4441. static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
  4442. {
  4443. struct nfs4_exception exception = {
  4444. .interruptible = true,
  4445. };
  4446. int err;
  4447. do {
  4448. err = _nfs4_proc_remove(dir, name, NF4DIR);
  4449. trace_nfs4_remove(dir, name, err);
  4450. err = nfs4_handle_exception(NFS_SERVER(dir), err,
  4451. &exception);
  4452. } while (exception.retry);
  4453. return err;
  4454. }
  4455. static void nfs4_proc_unlink_setup(struct rpc_message *msg,
  4456. struct dentry *dentry,
  4457. struct inode *inode)
  4458. {
  4459. struct nfs_removeargs *args = msg->rpc_argp;
  4460. struct nfs_removeres *res = msg->rpc_resp;
  4461. struct nfs_server *server = NFS_SB(dentry->d_sb);
  4462. res->server = server;
  4463. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
  4464. nfs4_init_sequence(server->nfs_client, &args->seq_args,
  4465. &res->seq_res, 1, 0);
  4466. nfs_fattr_init(res->dir_attr);
  4467. nfs_request_directory_delegation(d_inode(dentry->d_parent));
  4468. if (inode) {
  4469. nfs4_inode_return_delegation(inode);
  4470. nfs_d_prune_case_insensitive_aliases(inode);
  4471. }
  4472. }
  4473. static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
  4474. {
  4475. nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
  4476. &data->args.seq_args,
  4477. &data->res.seq_res,
  4478. task);
  4479. }
  4480. static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
  4481. {
  4482. struct nfs_unlinkdata *data = task->tk_calldata;
  4483. struct nfs_removeres *res = &data->res;
  4484. if (!nfs4_sequence_done(task, &res->seq_res))
  4485. return 0;
  4486. if (nfs4_async_handle_error(task, res->server, NULL,
  4487. &data->timeout) == -EAGAIN)
  4488. return 0;
  4489. if (task->tk_status == 0)
  4490. nfs4_update_changeattr(dir, &res->cinfo,
  4491. res->dir_attr->time_start,
  4492. NFS_INO_INVALID_DATA);
  4493. return 1;
  4494. }
  4495. static void nfs4_proc_rename_setup(struct rpc_message *msg,
  4496. struct dentry *old_dentry,
  4497. struct dentry *new_dentry,
  4498. struct inode *same_parent)
  4499. {
  4500. struct nfs_server *server = NFS_SB(old_dentry->d_sb);
  4501. struct nfs_renameargs *arg = msg->rpc_argp;
  4502. struct nfs_renameres *res = msg->rpc_resp;
  4503. struct inode *old_inode = d_inode(old_dentry);
  4504. struct inode *new_inode = d_inode(new_dentry);
  4505. if (old_inode)
  4506. nfs4_inode_make_writeable(old_inode);
  4507. if (new_inode)
  4508. nfs4_inode_return_delegation(new_inode);
  4509. if (same_parent)
  4510. nfs_request_directory_delegation(same_parent);
  4511. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
  4512. res->server = server;
  4513. nfs4_init_sequence(server->nfs_client, &arg->seq_args,
  4514. &res->seq_res, 1, 0);
  4515. }
  4516. static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
  4517. {
  4518. nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
  4519. &data->args.seq_args,
  4520. &data->res.seq_res,
  4521. task);
  4522. }
  4523. static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
  4524. struct inode *new_dir)
  4525. {
  4526. struct nfs_renamedata *data = task->tk_calldata;
  4527. struct nfs_renameres *res = &data->res;
  4528. if (!nfs4_sequence_done(task, &res->seq_res))
  4529. return 0;
  4530. if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
  4531. return 0;
  4532. if (task->tk_status == 0) {
  4533. nfs_d_prune_case_insensitive_aliases(d_inode(data->old_dentry));
  4534. if (new_dir != old_dir) {
  4535. /* Note: If we moved a directory, nlink will change */
  4536. nfs4_update_changeattr(old_dir, &res->old_cinfo,
  4537. res->old_fattr->time_start,
  4538. NFS_INO_INVALID_NLINK |
  4539. NFS_INO_INVALID_DATA);
  4540. nfs4_update_changeattr(new_dir, &res->new_cinfo,
  4541. res->new_fattr->time_start,
  4542. NFS_INO_INVALID_NLINK |
  4543. NFS_INO_INVALID_DATA);
  4544. } else
  4545. nfs4_update_changeattr(old_dir, &res->old_cinfo,
  4546. res->old_fattr->time_start,
  4547. NFS_INO_INVALID_DATA);
  4548. }
  4549. return 1;
  4550. }
  4551. static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
  4552. {
  4553. struct nfs_server *server = NFS_SERVER(inode);
  4554. __u32 bitmask[NFS4_BITMASK_SZ];
  4555. struct nfs4_link_arg arg = {
  4556. .fh = NFS_FH(inode),
  4557. .dir_fh = NFS_FH(dir),
  4558. .name = name,
  4559. .bitmask = bitmask,
  4560. };
  4561. struct nfs4_link_res res = {
  4562. .server = server,
  4563. };
  4564. struct rpc_message msg = {
  4565. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
  4566. .rpc_argp = &arg,
  4567. .rpc_resp = &res,
  4568. };
  4569. int status = -ENOMEM;
  4570. res.fattr = nfs_alloc_fattr_with_label(server);
  4571. if (res.fattr == NULL)
  4572. goto out;
  4573. nfs4_inode_make_writeable(inode);
  4574. nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, res.fattr->label),
  4575. inode,
  4576. NFS_INO_INVALID_CHANGE | NFS_INO_INVALID_CTIME);
  4577. status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
  4578. if (!status) {
  4579. nfs4_update_changeattr(dir, &res.cinfo, res.fattr->time_start,
  4580. NFS_INO_INVALID_DATA);
  4581. nfs4_inc_nlink(inode);
  4582. status = nfs_post_op_update_inode(inode, res.fattr);
  4583. if (!status)
  4584. nfs_setsecurity(inode, res.fattr);
  4585. }
  4586. out:
  4587. nfs_free_fattr(res.fattr);
  4588. return status;
  4589. }
  4590. static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
  4591. {
  4592. struct nfs4_exception exception = {
  4593. .interruptible = true,
  4594. };
  4595. int err;
  4596. do {
  4597. err = nfs4_handle_exception(NFS_SERVER(inode),
  4598. _nfs4_proc_link(inode, dir, name),
  4599. &exception);
  4600. } while (exception.retry);
  4601. return err;
  4602. }
  4603. struct nfs4_createdata {
  4604. struct rpc_message msg;
  4605. struct nfs4_create_arg arg;
  4606. struct nfs4_create_res res;
  4607. struct nfs_fh fh;
  4608. struct nfs_fattr fattr;
  4609. };
  4610. static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
  4611. const struct qstr *name, struct iattr *sattr, u32 ftype)
  4612. {
  4613. struct nfs4_createdata *data;
  4614. data = kzalloc_obj(*data);
  4615. if (data != NULL) {
  4616. struct nfs_server *server = NFS_SERVER(dir);
  4617. data->fattr.label = nfs4_label_alloc(server, GFP_KERNEL);
  4618. if (IS_ERR(data->fattr.label))
  4619. goto out_free;
  4620. data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
  4621. data->msg.rpc_argp = &data->arg;
  4622. data->msg.rpc_resp = &data->res;
  4623. data->arg.dir_fh = NFS_FH(dir);
  4624. data->arg.server = server;
  4625. data->arg.name = name;
  4626. data->arg.attrs = sattr;
  4627. data->arg.ftype = ftype;
  4628. data->arg.bitmask = nfs4_bitmask(server, data->fattr.label);
  4629. data->arg.umask = current_umask();
  4630. data->res.server = server;
  4631. data->res.fh = &data->fh;
  4632. data->res.fattr = &data->fattr;
  4633. nfs_fattr_init(data->res.fattr);
  4634. }
  4635. return data;
  4636. out_free:
  4637. kfree(data);
  4638. return NULL;
  4639. }
  4640. static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
  4641. {
  4642. int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
  4643. &data->arg.seq_args, &data->res.seq_res, 1);
  4644. if (status == 0) {
  4645. spin_lock(&dir->i_lock);
  4646. nfs4_update_changeattr_locked(dir, &data->res.dir_cinfo,
  4647. data->res.fattr->time_start,
  4648. NFS_INO_INVALID_DATA);
  4649. spin_unlock(&dir->i_lock);
  4650. status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
  4651. }
  4652. return status;
  4653. }
  4654. static struct dentry *nfs4_do_mkdir(struct inode *dir, struct dentry *dentry,
  4655. struct nfs4_createdata *data, int *statusp)
  4656. {
  4657. struct dentry *ret;
  4658. *statusp = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
  4659. &data->arg.seq_args, &data->res.seq_res, 1);
  4660. if (*statusp)
  4661. return NULL;
  4662. spin_lock(&dir->i_lock);
  4663. /* Creating a directory bumps nlink in the parent */
  4664. nfs4_inc_nlink_locked(dir);
  4665. nfs4_update_changeattr_locked(dir, &data->res.dir_cinfo,
  4666. data->res.fattr->time_start,
  4667. NFS_INO_INVALID_DATA);
  4668. spin_unlock(&dir->i_lock);
  4669. ret = nfs_add_or_obtain(dentry, data->res.fh, data->res.fattr);
  4670. if (!IS_ERR(ret))
  4671. return ret;
  4672. *statusp = PTR_ERR(ret);
  4673. return NULL;
  4674. }
  4675. static void nfs4_free_createdata(struct nfs4_createdata *data)
  4676. {
  4677. nfs4_label_free(data->fattr.label);
  4678. kfree(data);
  4679. }
  4680. static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
  4681. struct folio *folio, unsigned int len, struct iattr *sattr,
  4682. struct nfs4_label *label)
  4683. {
  4684. struct page *page = &folio->page;
  4685. struct nfs4_createdata *data;
  4686. int status = -ENAMETOOLONG;
  4687. if (len > NFS4_MAXPATHLEN)
  4688. goto out;
  4689. status = -ENOMEM;
  4690. data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
  4691. if (data == NULL)
  4692. goto out;
  4693. data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
  4694. data->arg.u.symlink.pages = &page;
  4695. data->arg.u.symlink.len = len;
  4696. data->arg.label = label;
  4697. status = nfs4_do_create(dir, dentry, data);
  4698. nfs4_free_createdata(data);
  4699. out:
  4700. return status;
  4701. }
  4702. static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
  4703. struct folio *folio, unsigned int len, struct iattr *sattr)
  4704. {
  4705. struct nfs4_exception exception = {
  4706. .interruptible = true,
  4707. };
  4708. struct nfs4_label l, *label;
  4709. int err;
  4710. label = nfs4_label_init_security(dir, dentry, sattr, &l);
  4711. do {
  4712. err = _nfs4_proc_symlink(dir, dentry, folio, len, sattr, label);
  4713. trace_nfs4_symlink(dir, &dentry->d_name, err);
  4714. err = nfs4_handle_exception(NFS_SERVER(dir), err,
  4715. &exception);
  4716. } while (exception.retry);
  4717. nfs4_label_release_security(label);
  4718. return err;
  4719. }
  4720. static struct dentry *_nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
  4721. struct iattr *sattr,
  4722. struct nfs4_label *label, int *statusp)
  4723. {
  4724. struct nfs4_createdata *data;
  4725. struct dentry *ret = NULL;
  4726. *statusp = -ENOMEM;
  4727. data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
  4728. if (data == NULL)
  4729. goto out;
  4730. data->arg.label = label;
  4731. ret = nfs4_do_mkdir(dir, dentry, data, statusp);
  4732. nfs4_free_createdata(data);
  4733. out:
  4734. return ret;
  4735. }
  4736. static struct dentry *nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
  4737. struct iattr *sattr)
  4738. {
  4739. struct nfs_server *server = NFS_SERVER(dir);
  4740. struct nfs4_exception exception = {
  4741. .interruptible = true,
  4742. };
  4743. struct nfs4_label l, *label;
  4744. struct dentry *alias;
  4745. int err;
  4746. label = nfs4_label_init_security(dir, dentry, sattr, &l);
  4747. if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
  4748. sattr->ia_mode &= ~current_umask();
  4749. do {
  4750. alias = _nfs4_proc_mkdir(dir, dentry, sattr, label, &err);
  4751. trace_nfs4_mkdir(dir, &dentry->d_name, err);
  4752. if (err)
  4753. alias = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
  4754. err,
  4755. &exception));
  4756. } while (exception.retry);
  4757. nfs4_label_release_security(label);
  4758. return alias;
  4759. }
  4760. static int _nfs4_proc_readdir(struct nfs_readdir_arg *nr_arg,
  4761. struct nfs_readdir_res *nr_res)
  4762. {
  4763. struct inode *dir = d_inode(nr_arg->dentry);
  4764. struct nfs_server *server = NFS_SERVER(dir);
  4765. struct nfs4_readdir_arg args = {
  4766. .fh = NFS_FH(dir),
  4767. .pages = nr_arg->pages,
  4768. .pgbase = 0,
  4769. .count = nr_arg->page_len,
  4770. .plus = nr_arg->plus,
  4771. };
  4772. struct nfs4_readdir_res res;
  4773. struct rpc_message msg = {
  4774. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
  4775. .rpc_argp = &args,
  4776. .rpc_resp = &res,
  4777. .rpc_cred = nr_arg->cred,
  4778. };
  4779. int status;
  4780. dprintk("%s: dentry = %pd2, cookie = %llu\n", __func__,
  4781. nr_arg->dentry, (unsigned long long)nr_arg->cookie);
  4782. if (!(server->caps & NFS_CAP_SECURITY_LABEL))
  4783. args.bitmask = server->attr_bitmask_nl;
  4784. else
  4785. args.bitmask = server->attr_bitmask;
  4786. nfs4_setup_readdir(nr_arg->cookie, nr_arg->verf, nr_arg->dentry, &args);
  4787. res.pgbase = args.pgbase;
  4788. status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
  4789. &res.seq_res, 0);
  4790. if (status >= 0) {
  4791. memcpy(nr_res->verf, res.verifier.data, NFS4_VERIFIER_SIZE);
  4792. status += args.pgbase;
  4793. }
  4794. nfs_invalidate_atime(dir);
  4795. dprintk("%s: returns %d\n", __func__, status);
  4796. return status;
  4797. }
  4798. static int nfs4_proc_readdir(struct nfs_readdir_arg *arg,
  4799. struct nfs_readdir_res *res)
  4800. {
  4801. struct nfs4_exception exception = {
  4802. .interruptible = true,
  4803. };
  4804. int err;
  4805. do {
  4806. err = _nfs4_proc_readdir(arg, res);
  4807. trace_nfs4_readdir(d_inode(arg->dentry), err);
  4808. err = nfs4_handle_exception(NFS_SERVER(d_inode(arg->dentry)),
  4809. err, &exception);
  4810. } while (exception.retry);
  4811. return err;
  4812. }
  4813. static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
  4814. struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
  4815. {
  4816. struct nfs4_createdata *data;
  4817. int mode = sattr->ia_mode;
  4818. int status = -ENOMEM;
  4819. data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
  4820. if (data == NULL)
  4821. goto out;
  4822. if (S_ISFIFO(mode))
  4823. data->arg.ftype = NF4FIFO;
  4824. else if (S_ISBLK(mode)) {
  4825. data->arg.ftype = NF4BLK;
  4826. data->arg.u.device.specdata1 = MAJOR(rdev);
  4827. data->arg.u.device.specdata2 = MINOR(rdev);
  4828. }
  4829. else if (S_ISCHR(mode)) {
  4830. data->arg.ftype = NF4CHR;
  4831. data->arg.u.device.specdata1 = MAJOR(rdev);
  4832. data->arg.u.device.specdata2 = MINOR(rdev);
  4833. } else if (!S_ISSOCK(mode)) {
  4834. status = -EINVAL;
  4835. goto out_free;
  4836. }
  4837. data->arg.label = label;
  4838. status = nfs4_do_create(dir, dentry, data);
  4839. out_free:
  4840. nfs4_free_createdata(data);
  4841. out:
  4842. return status;
  4843. }
  4844. static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
  4845. struct iattr *sattr, dev_t rdev)
  4846. {
  4847. struct nfs_server *server = NFS_SERVER(dir);
  4848. struct nfs4_exception exception = {
  4849. .interruptible = true,
  4850. };
  4851. struct nfs4_label l, *label;
  4852. int err;
  4853. label = nfs4_label_init_security(dir, dentry, sattr, &l);
  4854. if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
  4855. sattr->ia_mode &= ~current_umask();
  4856. do {
  4857. err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
  4858. trace_nfs4_mknod(dir, &dentry->d_name, err);
  4859. err = nfs4_handle_exception(NFS_SERVER(dir), err,
  4860. &exception);
  4861. } while (exception.retry);
  4862. nfs4_label_release_security(label);
  4863. return err;
  4864. }
  4865. static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
  4866. struct nfs_fsstat *fsstat)
  4867. {
  4868. struct nfs4_statfs_arg args = {
  4869. .fh = fhandle,
  4870. .bitmask = server->attr_bitmask,
  4871. };
  4872. struct nfs4_statfs_res res = {
  4873. .fsstat = fsstat,
  4874. };
  4875. struct rpc_message msg = {
  4876. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
  4877. .rpc_argp = &args,
  4878. .rpc_resp = &res,
  4879. };
  4880. nfs_fattr_init(fsstat->fattr);
  4881. return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  4882. }
  4883. static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
  4884. {
  4885. struct nfs4_exception exception = {
  4886. .interruptible = true,
  4887. };
  4888. int err;
  4889. do {
  4890. err = nfs4_handle_exception(server,
  4891. _nfs4_proc_statfs(server, fhandle, fsstat),
  4892. &exception);
  4893. } while (exception.retry);
  4894. return err;
  4895. }
  4896. static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
  4897. struct nfs_fsinfo *fsinfo)
  4898. {
  4899. struct nfs4_fsinfo_arg args = {
  4900. .fh = fhandle,
  4901. .bitmask = server->attr_bitmask,
  4902. };
  4903. struct nfs4_fsinfo_res res = {
  4904. .fsinfo = fsinfo,
  4905. };
  4906. struct rpc_message msg = {
  4907. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
  4908. .rpc_argp = &args,
  4909. .rpc_resp = &res,
  4910. };
  4911. return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  4912. }
  4913. static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
  4914. {
  4915. struct nfs4_exception exception = {
  4916. .interruptible = true,
  4917. };
  4918. int err;
  4919. do {
  4920. err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
  4921. trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
  4922. if (err == 0) {
  4923. nfs4_set_lease_period(server->nfs_client, fsinfo->lease_time);
  4924. break;
  4925. }
  4926. err = nfs4_handle_exception(server, err, &exception);
  4927. } while (exception.retry);
  4928. return err;
  4929. }
  4930. static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
  4931. {
  4932. int error;
  4933. nfs_fattr_init(fsinfo->fattr);
  4934. error = nfs4_do_fsinfo(server, fhandle, fsinfo);
  4935. if (error == 0) {
  4936. /* block layout checks this! */
  4937. server->pnfs_blksize = fsinfo->blksize;
  4938. set_pnfs_layoutdriver(server, fhandle, fsinfo);
  4939. }
  4940. return error;
  4941. }
  4942. static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
  4943. struct nfs_pathconf *pathconf)
  4944. {
  4945. struct nfs4_pathconf_arg args = {
  4946. .fh = fhandle,
  4947. .bitmask = server->attr_bitmask,
  4948. };
  4949. struct nfs4_pathconf_res res = {
  4950. .pathconf = pathconf,
  4951. };
  4952. struct rpc_message msg = {
  4953. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
  4954. .rpc_argp = &args,
  4955. .rpc_resp = &res,
  4956. };
  4957. /* None of the pathconf attributes are mandatory to implement */
  4958. if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
  4959. memset(pathconf, 0, sizeof(*pathconf));
  4960. return 0;
  4961. }
  4962. nfs_fattr_init(pathconf->fattr);
  4963. return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  4964. }
  4965. static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
  4966. struct nfs_pathconf *pathconf)
  4967. {
  4968. struct nfs4_exception exception = {
  4969. .interruptible = true,
  4970. };
  4971. int err;
  4972. do {
  4973. err = nfs4_handle_exception(server,
  4974. _nfs4_proc_pathconf(server, fhandle, pathconf),
  4975. &exception);
  4976. } while (exception.retry);
  4977. return err;
  4978. }
  4979. int nfs4_set_rw_stateid(nfs4_stateid *stateid,
  4980. const struct nfs_open_context *ctx,
  4981. const struct nfs_lock_context *l_ctx,
  4982. fmode_t fmode)
  4983. {
  4984. return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
  4985. }
  4986. EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
  4987. static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
  4988. const struct nfs_open_context *ctx,
  4989. const struct nfs_lock_context *l_ctx,
  4990. fmode_t fmode)
  4991. {
  4992. nfs4_stateid _current_stateid;
  4993. /* If the current stateid represents a lost lock, then exit */
  4994. if (nfs4_set_rw_stateid(&_current_stateid, ctx, l_ctx, fmode) == -EIO)
  4995. return true;
  4996. return nfs4_stateid_match(stateid, &_current_stateid);
  4997. }
  4998. static bool nfs4_error_stateid_expired(int err)
  4999. {
  5000. switch (err) {
  5001. case -NFS4ERR_DELEG_REVOKED:
  5002. case -NFS4ERR_ADMIN_REVOKED:
  5003. case -NFS4ERR_BAD_STATEID:
  5004. case -NFS4ERR_STALE_STATEID:
  5005. case -NFS4ERR_OLD_STATEID:
  5006. case -NFS4ERR_OPENMODE:
  5007. case -NFS4ERR_EXPIRED:
  5008. return true;
  5009. }
  5010. return false;
  5011. }
  5012. static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
  5013. {
  5014. struct nfs_server *server = NFS_SERVER(hdr->inode);
  5015. trace_nfs4_read(hdr, task->tk_status);
  5016. if (task->tk_status < 0) {
  5017. struct nfs4_exception exception = {
  5018. .inode = hdr->inode,
  5019. .state = hdr->args.context->state,
  5020. .stateid = &hdr->args.stateid,
  5021. .retrans = hdr->retrans,
  5022. };
  5023. task->tk_status = nfs4_async_handle_exception(task,
  5024. server, task->tk_status, &exception);
  5025. hdr->retrans = exception.retrans;
  5026. if (exception.retry) {
  5027. rpc_restart_call_prepare(task);
  5028. return -EAGAIN;
  5029. }
  5030. }
  5031. if (task->tk_status > 0)
  5032. renew_lease(server, hdr->timestamp);
  5033. return 0;
  5034. }
  5035. static bool nfs4_read_stateid_changed(struct rpc_task *task,
  5036. struct nfs_pgio_args *args)
  5037. {
  5038. if (!nfs4_error_stateid_expired(task->tk_status) ||
  5039. nfs4_stateid_is_current(&args->stateid,
  5040. args->context,
  5041. args->lock_context,
  5042. FMODE_READ))
  5043. return false;
  5044. rpc_restart_call_prepare(task);
  5045. return true;
  5046. }
  5047. static bool nfs4_read_plus_not_supported(struct rpc_task *task,
  5048. struct nfs_pgio_header *hdr)
  5049. {
  5050. struct nfs_server *server = NFS_SERVER(hdr->inode);
  5051. struct rpc_message *msg = &task->tk_msg;
  5052. if (msg->rpc_proc == &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS] &&
  5053. task->tk_status == -ENOTSUPP) {
  5054. server->caps &= ~NFS_CAP_READ_PLUS;
  5055. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
  5056. rpc_restart_call_prepare(task);
  5057. return true;
  5058. }
  5059. return false;
  5060. }
  5061. static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
  5062. {
  5063. if (!nfs4_sequence_done(task, &hdr->res.seq_res))
  5064. return -EAGAIN;
  5065. if (nfs4_read_stateid_changed(task, &hdr->args))
  5066. return -EAGAIN;
  5067. if (nfs4_read_plus_not_supported(task, hdr))
  5068. return -EAGAIN;
  5069. if (task->tk_status > 0)
  5070. nfs_invalidate_atime(hdr->inode);
  5071. return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
  5072. nfs4_read_done_cb(task, hdr);
  5073. }
  5074. #if defined CONFIG_NFS_V4_2 && defined CONFIG_NFS_V4_2_READ_PLUS
  5075. static bool nfs42_read_plus_support(struct nfs_pgio_header *hdr,
  5076. struct rpc_message *msg)
  5077. {
  5078. /* Note: We don't use READ_PLUS with pNFS yet */
  5079. if (nfs_server_capable(hdr->inode, NFS_CAP_READ_PLUS) && !hdr->ds_clp) {
  5080. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS];
  5081. return nfs_read_alloc_scratch(hdr, READ_PLUS_SCRATCH_SIZE);
  5082. }
  5083. return false;
  5084. }
  5085. #else
  5086. static bool nfs42_read_plus_support(struct nfs_pgio_header *hdr,
  5087. struct rpc_message *msg)
  5088. {
  5089. return false;
  5090. }
  5091. #endif /* CONFIG_NFS_V4_2 */
  5092. static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
  5093. struct rpc_message *msg)
  5094. {
  5095. hdr->timestamp = jiffies;
  5096. if (!hdr->pgio_done_cb)
  5097. hdr->pgio_done_cb = nfs4_read_done_cb;
  5098. if (!nfs42_read_plus_support(hdr, msg))
  5099. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
  5100. nfs4_init_sequence(NFS_SERVER(hdr->inode)->nfs_client,
  5101. &hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
  5102. }
  5103. static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
  5104. struct nfs_pgio_header *hdr)
  5105. {
  5106. if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
  5107. &hdr->args.seq_args,
  5108. &hdr->res.seq_res,
  5109. task))
  5110. return 0;
  5111. if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
  5112. hdr->args.lock_context,
  5113. hdr->rw_mode) == -EIO)
  5114. return -EIO;
  5115. if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
  5116. return -EIO;
  5117. return 0;
  5118. }
  5119. static int nfs4_write_done_cb(struct rpc_task *task,
  5120. struct nfs_pgio_header *hdr)
  5121. {
  5122. struct inode *inode = hdr->inode;
  5123. trace_nfs4_write(hdr, task->tk_status);
  5124. if (task->tk_status < 0) {
  5125. struct nfs4_exception exception = {
  5126. .inode = hdr->inode,
  5127. .state = hdr->args.context->state,
  5128. .stateid = &hdr->args.stateid,
  5129. .retrans = hdr->retrans,
  5130. };
  5131. task->tk_status = nfs4_async_handle_exception(task,
  5132. NFS_SERVER(inode), task->tk_status,
  5133. &exception);
  5134. hdr->retrans = exception.retrans;
  5135. if (exception.retry) {
  5136. rpc_restart_call_prepare(task);
  5137. return -EAGAIN;
  5138. }
  5139. }
  5140. if (task->tk_status >= 0) {
  5141. renew_lease(NFS_SERVER(inode), hdr->timestamp);
  5142. nfs_writeback_update_inode(hdr);
  5143. }
  5144. return 0;
  5145. }
  5146. static bool nfs4_write_stateid_changed(struct rpc_task *task,
  5147. struct nfs_pgio_args *args)
  5148. {
  5149. if (!nfs4_error_stateid_expired(task->tk_status) ||
  5150. nfs4_stateid_is_current(&args->stateid,
  5151. args->context,
  5152. args->lock_context,
  5153. FMODE_WRITE))
  5154. return false;
  5155. rpc_restart_call_prepare(task);
  5156. return true;
  5157. }
  5158. static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
  5159. {
  5160. if (!nfs4_sequence_done(task, &hdr->res.seq_res))
  5161. return -EAGAIN;
  5162. if (nfs4_write_stateid_changed(task, &hdr->args))
  5163. return -EAGAIN;
  5164. return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
  5165. nfs4_write_done_cb(task, hdr);
  5166. }
  5167. static
  5168. bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
  5169. {
  5170. /* Don't request attributes for pNFS or O_DIRECT writes */
  5171. if (hdr->ds_clp != NULL || hdr->dreq != NULL)
  5172. return false;
  5173. /* Otherwise, request attributes if and only if we don't hold
  5174. * a delegation
  5175. */
  5176. return nfs4_have_delegation(hdr->inode, FMODE_READ, 0) == 0;
  5177. }
  5178. void nfs4_bitmask_set(__u32 bitmask[], const __u32 src[],
  5179. struct inode *inode, unsigned long cache_validity)
  5180. {
  5181. struct nfs_server *server = NFS_SERVER(inode);
  5182. unsigned int i;
  5183. memcpy(bitmask, src, sizeof(*bitmask) * NFS4_BITMASK_SZ);
  5184. cache_validity |= READ_ONCE(NFS_I(inode)->cache_validity);
  5185. if (cache_validity & NFS_INO_INVALID_CHANGE)
  5186. bitmask[0] |= FATTR4_WORD0_CHANGE;
  5187. if (cache_validity & NFS_INO_INVALID_ATIME)
  5188. bitmask[1] |= FATTR4_WORD1_TIME_ACCESS;
  5189. if (cache_validity & NFS_INO_INVALID_MODE)
  5190. bitmask[1] |= FATTR4_WORD1_MODE;
  5191. if (cache_validity & NFS_INO_INVALID_OTHER)
  5192. bitmask[1] |= FATTR4_WORD1_OWNER | FATTR4_WORD1_OWNER_GROUP;
  5193. if (cache_validity & NFS_INO_INVALID_NLINK)
  5194. bitmask[1] |= FATTR4_WORD1_NUMLINKS;
  5195. if (cache_validity & NFS_INO_INVALID_CTIME)
  5196. bitmask[1] |= FATTR4_WORD1_TIME_METADATA;
  5197. if (cache_validity & NFS_INO_INVALID_MTIME)
  5198. bitmask[1] |= FATTR4_WORD1_TIME_MODIFY;
  5199. if (cache_validity & NFS_INO_INVALID_BLOCKS)
  5200. bitmask[1] |= FATTR4_WORD1_SPACE_USED;
  5201. if (cache_validity & NFS_INO_INVALID_BTIME)
  5202. bitmask[1] |= FATTR4_WORD1_TIME_CREATE;
  5203. if (cache_validity & NFS_INO_INVALID_SIZE)
  5204. bitmask[0] |= FATTR4_WORD0_SIZE;
  5205. for (i = 0; i < NFS4_BITMASK_SZ; i++)
  5206. bitmask[i] &= server->attr_bitmask[i];
  5207. }
  5208. static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
  5209. struct rpc_message *msg,
  5210. struct rpc_clnt **clnt)
  5211. {
  5212. struct nfs_server *server = NFS_SERVER(hdr->inode);
  5213. if (!nfs4_write_need_cache_consistency_data(hdr)) {
  5214. hdr->args.bitmask = NULL;
  5215. hdr->res.fattr = NULL;
  5216. } else {
  5217. nfs4_bitmask_set(hdr->args.bitmask_store,
  5218. server->cache_consistency_bitmask,
  5219. hdr->inode, NFS_INO_INVALID_BLOCKS);
  5220. hdr->args.bitmask = hdr->args.bitmask_store;
  5221. }
  5222. if (!hdr->pgio_done_cb)
  5223. hdr->pgio_done_cb = nfs4_write_done_cb;
  5224. hdr->res.server = server;
  5225. hdr->timestamp = jiffies;
  5226. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
  5227. nfs4_init_sequence(server->nfs_client, &hdr->args.seq_args,
  5228. &hdr->res.seq_res, 0, 0);
  5229. nfs4_state_protect_write(hdr->ds_clp ? hdr->ds_clp : server->nfs_client, clnt, msg, hdr);
  5230. }
  5231. static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
  5232. {
  5233. nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
  5234. &data->args.seq_args,
  5235. &data->res.seq_res,
  5236. task);
  5237. }
  5238. static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
  5239. {
  5240. struct inode *inode = data->inode;
  5241. trace_nfs4_commit(data, task->tk_status);
  5242. if (nfs4_async_handle_error(task, NFS_SERVER(inode),
  5243. NULL, NULL) == -EAGAIN) {
  5244. rpc_restart_call_prepare(task);
  5245. return -EAGAIN;
  5246. }
  5247. return 0;
  5248. }
  5249. static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
  5250. {
  5251. if (!nfs4_sequence_done(task, &data->res.seq_res))
  5252. return -EAGAIN;
  5253. return data->commit_done_cb(task, data);
  5254. }
  5255. static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
  5256. struct rpc_clnt **clnt)
  5257. {
  5258. struct nfs_server *server = NFS_SERVER(data->inode);
  5259. if (data->commit_done_cb == NULL)
  5260. data->commit_done_cb = nfs4_commit_done_cb;
  5261. data->res.server = server;
  5262. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
  5263. nfs4_init_sequence(server->nfs_client, &data->args.seq_args,
  5264. &data->res.seq_res, 1, 0);
  5265. nfs4_state_protect(data->ds_clp ? data->ds_clp : server->nfs_client,
  5266. NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
  5267. }
  5268. static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
  5269. struct nfs_commitres *res)
  5270. {
  5271. struct inode *dst_inode = file_inode(dst);
  5272. struct nfs_server *server = NFS_SERVER(dst_inode);
  5273. struct rpc_message msg = {
  5274. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
  5275. .rpc_argp = args,
  5276. .rpc_resp = res,
  5277. };
  5278. args->fh = NFS_FH(dst_inode);
  5279. return nfs4_call_sync(server->client, server, &msg,
  5280. &args->seq_args, &res->seq_res, 1);
  5281. }
  5282. int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
  5283. {
  5284. struct nfs_commitargs args = {
  5285. .offset = offset,
  5286. .count = count,
  5287. };
  5288. struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
  5289. struct nfs4_exception exception = { };
  5290. int status;
  5291. do {
  5292. status = _nfs4_proc_commit(dst, &args, res);
  5293. status = nfs4_handle_exception(dst_server, status, &exception);
  5294. } while (exception.retry);
  5295. return status;
  5296. }
  5297. static bool nfs4_server_supports_acls(const struct nfs_server *server,
  5298. enum nfs4_acl_type type)
  5299. {
  5300. switch (type) {
  5301. default:
  5302. return server->attr_bitmask[0] & FATTR4_WORD0_ACL;
  5303. case NFS4ACL_DACL:
  5304. return server->attr_bitmask[1] & FATTR4_WORD1_DACL;
  5305. case NFS4ACL_SACL:
  5306. return server->attr_bitmask[1] & FATTR4_WORD1_SACL;
  5307. }
  5308. }
  5309. /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
  5310. * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
  5311. * the stack.
  5312. */
  5313. #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
  5314. int nfs4_buf_to_pages_noslab(const void *buf, size_t buflen,
  5315. struct page **pages)
  5316. {
  5317. struct page *newpage, **spages;
  5318. int rc = 0;
  5319. size_t len;
  5320. spages = pages;
  5321. do {
  5322. len = min_t(size_t, PAGE_SIZE, buflen);
  5323. newpage = alloc_page(GFP_KERNEL);
  5324. if (newpage == NULL)
  5325. goto unwind;
  5326. memcpy(page_address(newpage), buf, len);
  5327. buf += len;
  5328. buflen -= len;
  5329. *pages++ = newpage;
  5330. rc++;
  5331. } while (buflen != 0);
  5332. return rc;
  5333. unwind:
  5334. for(; rc > 0; rc--)
  5335. __free_page(spages[rc-1]);
  5336. return -ENOMEM;
  5337. }
  5338. struct nfs4_cached_acl {
  5339. enum nfs4_acl_type type;
  5340. int cached;
  5341. size_t len;
  5342. char data[];
  5343. };
  5344. static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
  5345. {
  5346. struct nfs_inode *nfsi = NFS_I(inode);
  5347. spin_lock(&inode->i_lock);
  5348. kfree(nfsi->nfs4_acl);
  5349. nfsi->nfs4_acl = acl;
  5350. spin_unlock(&inode->i_lock);
  5351. }
  5352. static void nfs4_zap_acl_attr(struct inode *inode)
  5353. {
  5354. nfs4_set_cached_acl(inode, NULL);
  5355. }
  5356. static ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf,
  5357. size_t buflen, enum nfs4_acl_type type)
  5358. {
  5359. struct nfs_inode *nfsi = NFS_I(inode);
  5360. struct nfs4_cached_acl *acl;
  5361. int ret = -ENOENT;
  5362. spin_lock(&inode->i_lock);
  5363. acl = nfsi->nfs4_acl;
  5364. if (acl == NULL)
  5365. goto out;
  5366. if (acl->type != type)
  5367. goto out;
  5368. if (buf == NULL) /* user is just asking for length */
  5369. goto out_len;
  5370. if (acl->cached == 0)
  5371. goto out;
  5372. ret = -ERANGE; /* see getxattr(2) man page */
  5373. if (acl->len > buflen)
  5374. goto out;
  5375. memcpy(buf, acl->data, acl->len);
  5376. out_len:
  5377. ret = acl->len;
  5378. out:
  5379. spin_unlock(&inode->i_lock);
  5380. return ret;
  5381. }
  5382. static void nfs4_write_cached_acl(struct inode *inode, struct page **pages,
  5383. size_t pgbase, size_t acl_len,
  5384. enum nfs4_acl_type type)
  5385. {
  5386. struct nfs4_cached_acl *acl;
  5387. size_t buflen = sizeof(*acl) + acl_len;
  5388. if (buflen <= PAGE_SIZE) {
  5389. acl = kmalloc(buflen, GFP_KERNEL);
  5390. if (acl == NULL)
  5391. goto out;
  5392. acl->cached = 1;
  5393. _copy_from_pages(acl->data, pages, pgbase, acl_len);
  5394. } else {
  5395. acl = kmalloc_obj(*acl);
  5396. if (acl == NULL)
  5397. goto out;
  5398. acl->cached = 0;
  5399. }
  5400. acl->type = type;
  5401. acl->len = acl_len;
  5402. out:
  5403. nfs4_set_cached_acl(inode, acl);
  5404. }
  5405. /*
  5406. * The getxattr API returns the required buffer length when called with a
  5407. * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
  5408. * the required buf. On a NULL buf, we send a page of data to the server
  5409. * guessing that the ACL request can be serviced by a page. If so, we cache
  5410. * up to the page of ACL data, and the 2nd call to getxattr is serviced by
  5411. * the cache. If not so, we throw away the page, and cache the required
  5412. * length. The next getxattr call will then produce another round trip to
  5413. * the server, this time with the input buf of the required size.
  5414. */
  5415. static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf,
  5416. size_t buflen, enum nfs4_acl_type type)
  5417. {
  5418. struct page **pages;
  5419. struct nfs_getaclargs args = {
  5420. .fh = NFS_FH(inode),
  5421. .acl_type = type,
  5422. .acl_len = buflen,
  5423. };
  5424. struct nfs_getaclres res = {
  5425. .acl_type = type,
  5426. .acl_len = buflen,
  5427. };
  5428. struct rpc_message msg = {
  5429. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
  5430. .rpc_argp = &args,
  5431. .rpc_resp = &res,
  5432. };
  5433. unsigned int npages;
  5434. int ret = -ENOMEM, i;
  5435. struct nfs_server *server = NFS_SERVER(inode);
  5436. if (buflen == 0)
  5437. buflen = server->rsize;
  5438. npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
  5439. pages = kmalloc_objs(struct page *, npages);
  5440. if (!pages)
  5441. return -ENOMEM;
  5442. args.acl_pages = pages;
  5443. for (i = 0; i < npages; i++) {
  5444. pages[i] = alloc_page(GFP_KERNEL);
  5445. if (!pages[i])
  5446. goto out_free;
  5447. }
  5448. /* for decoding across pages */
  5449. res.acl_scratch = folio_alloc(GFP_KERNEL, 0);
  5450. if (!res.acl_scratch)
  5451. goto out_free;
  5452. args.acl_len = npages * PAGE_SIZE;
  5453. dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
  5454. __func__, buf, buflen, npages, args.acl_len);
  5455. ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
  5456. &msg, &args.seq_args, &res.seq_res, 0);
  5457. if (ret)
  5458. goto out_free;
  5459. /* Handle the case where the passed-in buffer is too short */
  5460. if (res.acl_flags & NFS4_ACL_TRUNC) {
  5461. /* Did the user only issue a request for the acl length? */
  5462. if (buf == NULL)
  5463. goto out_ok;
  5464. ret = -ERANGE;
  5465. goto out_free;
  5466. }
  5467. nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len,
  5468. type);
  5469. if (buf) {
  5470. if (res.acl_len > buflen) {
  5471. ret = -ERANGE;
  5472. goto out_free;
  5473. }
  5474. _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
  5475. }
  5476. out_ok:
  5477. ret = res.acl_len;
  5478. out_free:
  5479. while (--i >= 0)
  5480. __free_page(pages[i]);
  5481. if (res.acl_scratch)
  5482. folio_put(res.acl_scratch);
  5483. kfree(pages);
  5484. return ret;
  5485. }
  5486. static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf,
  5487. size_t buflen, enum nfs4_acl_type type)
  5488. {
  5489. struct nfs4_exception exception = {
  5490. .interruptible = true,
  5491. };
  5492. ssize_t ret;
  5493. do {
  5494. ret = __nfs4_get_acl_uncached(inode, buf, buflen, type);
  5495. trace_nfs4_get_acl(inode, ret);
  5496. if (ret >= 0)
  5497. break;
  5498. ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
  5499. } while (exception.retry);
  5500. return ret;
  5501. }
  5502. static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen,
  5503. enum nfs4_acl_type type)
  5504. {
  5505. struct nfs_server *server = NFS_SERVER(inode);
  5506. int ret;
  5507. if (unlikely(NFS_FH(inode)->size == 0))
  5508. return -ENODATA;
  5509. if (!nfs4_server_supports_acls(server, type))
  5510. return -EOPNOTSUPP;
  5511. ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
  5512. if (ret < 0)
  5513. return ret;
  5514. if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
  5515. nfs_zap_acl_cache(inode);
  5516. ret = nfs4_read_cached_acl(inode, buf, buflen, type);
  5517. if (ret != -ENOENT)
  5518. /* -ENOENT is returned if there is no ACL or if there is an ACL
  5519. * but no cached acl data, just the acl length */
  5520. return ret;
  5521. return nfs4_get_acl_uncached(inode, buf, buflen, type);
  5522. }
  5523. static int __nfs4_proc_set_acl(struct inode *inode, const void *buf,
  5524. size_t buflen, enum nfs4_acl_type type)
  5525. {
  5526. struct nfs_server *server = NFS_SERVER(inode);
  5527. struct page *pages[NFS4ACL_MAXPAGES];
  5528. struct nfs_setaclargs arg = {
  5529. .fh = NFS_FH(inode),
  5530. .acl_type = type,
  5531. .acl_len = buflen,
  5532. .acl_pages = pages,
  5533. };
  5534. struct nfs_setaclres res;
  5535. struct rpc_message msg = {
  5536. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
  5537. .rpc_argp = &arg,
  5538. .rpc_resp = &res,
  5539. };
  5540. unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
  5541. int ret, i;
  5542. /* You can't remove system.nfs4_acl: */
  5543. if (buflen == 0)
  5544. return -EINVAL;
  5545. if (!nfs4_server_supports_acls(server, type))
  5546. return -EOPNOTSUPP;
  5547. if (npages > ARRAY_SIZE(pages))
  5548. return -ERANGE;
  5549. i = nfs4_buf_to_pages_noslab(buf, buflen, arg.acl_pages);
  5550. if (i < 0)
  5551. return i;
  5552. nfs4_inode_make_writeable(inode);
  5553. ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
  5554. /*
  5555. * Free each page after tx, so the only ref left is
  5556. * held by the network stack
  5557. */
  5558. for (; i > 0; i--)
  5559. put_page(pages[i-1]);
  5560. /*
  5561. * Acl update can result in inode attribute update.
  5562. * so mark the attribute cache invalid.
  5563. */
  5564. spin_lock(&inode->i_lock);
  5565. nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
  5566. NFS_INO_INVALID_CTIME |
  5567. NFS_INO_REVAL_FORCED);
  5568. spin_unlock(&inode->i_lock);
  5569. nfs_access_zap_cache(inode);
  5570. nfs_zap_acl_cache(inode);
  5571. return ret;
  5572. }
  5573. static int nfs4_proc_set_acl(struct inode *inode, const void *buf,
  5574. size_t buflen, enum nfs4_acl_type type)
  5575. {
  5576. struct nfs4_exception exception = { };
  5577. int err;
  5578. if (unlikely(NFS_FH(inode)->size == 0))
  5579. return -ENODATA;
  5580. do {
  5581. err = __nfs4_proc_set_acl(inode, buf, buflen, type);
  5582. trace_nfs4_set_acl(inode, err);
  5583. if (err == -NFS4ERR_BADOWNER || err == -NFS4ERR_BADNAME) {
  5584. /*
  5585. * no need to retry since the kernel
  5586. * isn't involved in encoding the ACEs.
  5587. */
  5588. err = -EINVAL;
  5589. break;
  5590. }
  5591. err = nfs4_handle_exception(NFS_SERVER(inode), err,
  5592. &exception);
  5593. } while (exception.retry);
  5594. return err;
  5595. }
  5596. #ifdef CONFIG_NFS_V4_SECURITY_LABEL
  5597. static int _nfs4_get_security_label(struct inode *inode, void *buf,
  5598. size_t buflen)
  5599. {
  5600. struct nfs_server *server = NFS_SERVER(inode);
  5601. struct nfs4_label label = {0, 0, 0, buflen, buf};
  5602. u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
  5603. struct nfs_fattr fattr = {
  5604. .label = &label,
  5605. };
  5606. struct nfs4_getattr_arg arg = {
  5607. .fh = NFS_FH(inode),
  5608. .bitmask = bitmask,
  5609. };
  5610. struct nfs4_getattr_res res = {
  5611. .fattr = &fattr,
  5612. .server = server,
  5613. };
  5614. struct rpc_message msg = {
  5615. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
  5616. .rpc_argp = &arg,
  5617. .rpc_resp = &res,
  5618. };
  5619. int ret;
  5620. nfs_fattr_init(&fattr);
  5621. ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
  5622. if (ret)
  5623. return ret;
  5624. if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
  5625. return -ENOENT;
  5626. return label.len;
  5627. }
  5628. static int nfs4_get_security_label(struct inode *inode, void *buf,
  5629. size_t buflen)
  5630. {
  5631. struct nfs4_exception exception = {
  5632. .interruptible = true,
  5633. };
  5634. int err;
  5635. if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
  5636. return -EOPNOTSUPP;
  5637. do {
  5638. err = _nfs4_get_security_label(inode, buf, buflen);
  5639. trace_nfs4_get_security_label(inode, err);
  5640. err = nfs4_handle_exception(NFS_SERVER(inode), err,
  5641. &exception);
  5642. } while (exception.retry);
  5643. return err;
  5644. }
  5645. static int _nfs4_do_set_security_label(struct inode *inode,
  5646. struct nfs4_label *ilabel,
  5647. struct nfs_fattr *fattr)
  5648. {
  5649. struct iattr sattr = {0};
  5650. struct nfs_server *server = NFS_SERVER(inode);
  5651. const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
  5652. struct nfs_setattrargs arg = {
  5653. .fh = NFS_FH(inode),
  5654. .iap = &sattr,
  5655. .server = server,
  5656. .bitmask = bitmask,
  5657. .label = ilabel,
  5658. };
  5659. struct nfs_setattrres res = {
  5660. .fattr = fattr,
  5661. .server = server,
  5662. };
  5663. struct rpc_message msg = {
  5664. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
  5665. .rpc_argp = &arg,
  5666. .rpc_resp = &res,
  5667. };
  5668. int status;
  5669. nfs4_stateid_copy(&arg.stateid, &zero_stateid);
  5670. status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
  5671. if (status)
  5672. dprintk("%s failed: %d\n", __func__, status);
  5673. return status;
  5674. }
  5675. static int nfs4_do_set_security_label(struct inode *inode,
  5676. struct nfs4_label *ilabel,
  5677. struct nfs_fattr *fattr)
  5678. {
  5679. struct nfs4_exception exception = { };
  5680. int err;
  5681. do {
  5682. err = _nfs4_do_set_security_label(inode, ilabel, fattr);
  5683. trace_nfs4_set_security_label(inode, err);
  5684. err = nfs4_handle_exception(NFS_SERVER(inode), err,
  5685. &exception);
  5686. } while (exception.retry);
  5687. return err;
  5688. }
  5689. static int
  5690. nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
  5691. {
  5692. struct nfs4_label ilabel = {0, 0, 0, buflen, (char *)buf };
  5693. struct nfs_fattr *fattr;
  5694. int status;
  5695. if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
  5696. return -EOPNOTSUPP;
  5697. fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode));
  5698. if (fattr == NULL)
  5699. return -ENOMEM;
  5700. status = nfs4_do_set_security_label(inode, &ilabel, fattr);
  5701. if (status == 0)
  5702. nfs_setsecurity(inode, fattr);
  5703. nfs_free_fattr(fattr);
  5704. return status;
  5705. }
  5706. #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
  5707. static void nfs4_init_boot_verifier(const struct nfs_client *clp,
  5708. nfs4_verifier *bootverf)
  5709. {
  5710. __be32 verf[2];
  5711. if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
  5712. /* An impossible timestamp guarantees this value
  5713. * will never match a generated boot time. */
  5714. verf[0] = cpu_to_be32(U32_MAX);
  5715. verf[1] = cpu_to_be32(U32_MAX);
  5716. } else {
  5717. struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
  5718. u64 ns = ktime_to_ns(nn->boot_time);
  5719. verf[0] = cpu_to_be32(ns >> 32);
  5720. verf[1] = cpu_to_be32(ns);
  5721. }
  5722. memcpy(bootverf->data, verf, sizeof(bootverf->data));
  5723. }
  5724. static size_t
  5725. nfs4_get_uniquifier(struct nfs_client *clp, char *buf, size_t buflen)
  5726. {
  5727. struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
  5728. struct nfs_netns_client *nn_clp = nn->nfs_client;
  5729. const char *id;
  5730. buf[0] = '\0';
  5731. if (nn_clp) {
  5732. rcu_read_lock();
  5733. id = rcu_dereference(nn_clp->identifier);
  5734. if (id)
  5735. strscpy(buf, id, buflen);
  5736. rcu_read_unlock();
  5737. }
  5738. if (nfs4_client_id_uniquifier[0] != '\0' && buf[0] == '\0')
  5739. strscpy(buf, nfs4_client_id_uniquifier, buflen);
  5740. return strlen(buf);
  5741. }
  5742. static int
  5743. nfs4_init_nonuniform_client_string(struct nfs_client *clp)
  5744. {
  5745. char buf[NFS4_CLIENT_ID_UNIQ_LEN];
  5746. size_t buflen;
  5747. size_t len;
  5748. char *str;
  5749. if (clp->cl_owner_id != NULL)
  5750. return 0;
  5751. rcu_read_lock();
  5752. len = 14 +
  5753. strlen(clp->cl_rpcclient->cl_nodename) +
  5754. 1 +
  5755. strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
  5756. 1;
  5757. rcu_read_unlock();
  5758. buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
  5759. if (buflen)
  5760. len += buflen + 1;
  5761. if (len > NFS4_OPAQUE_LIMIT + 1)
  5762. return -EINVAL;
  5763. /*
  5764. * Since this string is allocated at mount time, and held until the
  5765. * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
  5766. * about a memory-reclaim deadlock.
  5767. */
  5768. str = kmalloc(len, GFP_KERNEL);
  5769. if (!str)
  5770. return -ENOMEM;
  5771. rcu_read_lock();
  5772. if (buflen)
  5773. scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
  5774. clp->cl_rpcclient->cl_nodename, buf,
  5775. rpc_peeraddr2str(clp->cl_rpcclient,
  5776. RPC_DISPLAY_ADDR));
  5777. else
  5778. scnprintf(str, len, "Linux NFSv4.0 %s/%s",
  5779. clp->cl_rpcclient->cl_nodename,
  5780. rpc_peeraddr2str(clp->cl_rpcclient,
  5781. RPC_DISPLAY_ADDR));
  5782. rcu_read_unlock();
  5783. clp->cl_owner_id = str;
  5784. return 0;
  5785. }
  5786. static int
  5787. nfs4_init_uniform_client_string(struct nfs_client *clp)
  5788. {
  5789. char buf[NFS4_CLIENT_ID_UNIQ_LEN];
  5790. size_t buflen;
  5791. size_t len;
  5792. char *str;
  5793. if (clp->cl_owner_id != NULL)
  5794. return 0;
  5795. len = 10 + 10 + 1 + 10 + 1 +
  5796. strlen(clp->cl_rpcclient->cl_nodename) + 1;
  5797. buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
  5798. if (buflen)
  5799. len += buflen + 1;
  5800. if (len > NFS4_OPAQUE_LIMIT + 1)
  5801. return -EINVAL;
  5802. /*
  5803. * Since this string is allocated at mount time, and held until the
  5804. * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
  5805. * about a memory-reclaim deadlock.
  5806. */
  5807. str = kmalloc(len, GFP_KERNEL);
  5808. if (!str)
  5809. return -ENOMEM;
  5810. if (buflen)
  5811. scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
  5812. clp->rpc_ops->version, clp->cl_minorversion,
  5813. buf, clp->cl_rpcclient->cl_nodename);
  5814. else
  5815. scnprintf(str, len, "Linux NFSv%u.%u %s",
  5816. clp->rpc_ops->version, clp->cl_minorversion,
  5817. clp->cl_rpcclient->cl_nodename);
  5818. clp->cl_owner_id = str;
  5819. return 0;
  5820. }
  5821. /*
  5822. * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
  5823. * services. Advertise one based on the address family of the
  5824. * clientaddr.
  5825. */
  5826. static unsigned int
  5827. nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
  5828. {
  5829. if (strchr(clp->cl_ipaddr, ':') != NULL)
  5830. return scnprintf(buf, len, "tcp6");
  5831. else
  5832. return scnprintf(buf, len, "tcp");
  5833. }
  5834. static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
  5835. {
  5836. struct nfs4_setclientid *sc = calldata;
  5837. if (task->tk_status == 0)
  5838. sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
  5839. }
  5840. static const struct rpc_call_ops nfs4_setclientid_ops = {
  5841. .rpc_call_done = nfs4_setclientid_done,
  5842. };
  5843. /**
  5844. * nfs4_proc_setclientid - Negotiate client ID
  5845. * @clp: state data structure
  5846. * @program: RPC program for NFSv4 callback service
  5847. * @port: IP port number for NFS4 callback service
  5848. * @cred: credential to use for this call
  5849. * @res: where to place the result
  5850. *
  5851. * Returns zero, a negative errno, or a negative NFS4ERR status code.
  5852. */
  5853. int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
  5854. unsigned short port, const struct cred *cred,
  5855. struct nfs4_setclientid_res *res)
  5856. {
  5857. nfs4_verifier sc_verifier;
  5858. struct nfs4_setclientid setclientid = {
  5859. .sc_verifier = &sc_verifier,
  5860. .sc_prog = program,
  5861. .sc_clnt = clp,
  5862. };
  5863. struct rpc_message msg = {
  5864. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
  5865. .rpc_argp = &setclientid,
  5866. .rpc_resp = res,
  5867. .rpc_cred = cred,
  5868. };
  5869. struct rpc_task_setup task_setup_data = {
  5870. .rpc_client = clp->cl_rpcclient,
  5871. .rpc_message = &msg,
  5872. .callback_ops = &nfs4_setclientid_ops,
  5873. .callback_data = &setclientid,
  5874. .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
  5875. };
  5876. unsigned long now = jiffies;
  5877. int status;
  5878. /* nfs_client_id4 */
  5879. nfs4_init_boot_verifier(clp, &sc_verifier);
  5880. if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
  5881. status = nfs4_init_uniform_client_string(clp);
  5882. else
  5883. status = nfs4_init_nonuniform_client_string(clp);
  5884. if (status)
  5885. goto out;
  5886. /* cb_client4 */
  5887. setclientid.sc_netid_len =
  5888. nfs4_init_callback_netid(clp,
  5889. setclientid.sc_netid,
  5890. sizeof(setclientid.sc_netid));
  5891. setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
  5892. sizeof(setclientid.sc_uaddr), "%s.%u.%u",
  5893. clp->cl_ipaddr, port >> 8, port & 255);
  5894. dprintk("NFS call setclientid auth=%s, '%s'\n",
  5895. clp->cl_rpcclient->cl_auth->au_ops->au_name,
  5896. clp->cl_owner_id);
  5897. status = nfs4_call_sync_custom(&task_setup_data);
  5898. if (setclientid.sc_cred) {
  5899. kfree(clp->cl_acceptor);
  5900. clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
  5901. put_rpccred(setclientid.sc_cred);
  5902. }
  5903. if (status == 0)
  5904. do_renew_lease(clp, now);
  5905. out:
  5906. trace_nfs4_setclientid(clp, status);
  5907. dprintk("NFS reply setclientid: %d\n", status);
  5908. return status;
  5909. }
  5910. /**
  5911. * nfs4_proc_setclientid_confirm - Confirm client ID
  5912. * @clp: state data structure
  5913. * @arg: result of a previous SETCLIENTID
  5914. * @cred: credential to use for this call
  5915. *
  5916. * Returns zero, a negative errno, or a negative NFS4ERR status code.
  5917. */
  5918. int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
  5919. struct nfs4_setclientid_res *arg,
  5920. const struct cred *cred)
  5921. {
  5922. struct rpc_message msg = {
  5923. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
  5924. .rpc_argp = arg,
  5925. .rpc_cred = cred,
  5926. };
  5927. int status;
  5928. dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
  5929. clp->cl_rpcclient->cl_auth->au_ops->au_name,
  5930. clp->cl_clientid);
  5931. status = rpc_call_sync(clp->cl_rpcclient, &msg,
  5932. RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
  5933. trace_nfs4_setclientid_confirm(clp, status);
  5934. dprintk("NFS reply setclientid_confirm: %d\n", status);
  5935. return status;
  5936. }
  5937. struct nfs4_delegreturndata {
  5938. struct nfs4_delegreturnargs args;
  5939. struct nfs4_delegreturnres res;
  5940. struct nfs_fh fh;
  5941. nfs4_stateid stateid;
  5942. unsigned long timestamp;
  5943. unsigned short retrans;
  5944. struct {
  5945. struct nfs4_layoutreturn_args arg;
  5946. struct nfs4_layoutreturn_res res;
  5947. struct nfs4_xdr_opaque_data ld_private;
  5948. u32 roc_barrier;
  5949. bool roc;
  5950. } lr;
  5951. struct nfs4_delegattr sattr;
  5952. struct nfs_fattr fattr;
  5953. int rpc_status;
  5954. struct inode *inode;
  5955. };
  5956. static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
  5957. {
  5958. struct nfs4_delegreturndata *data = calldata;
  5959. struct nfs4_exception exception = {
  5960. .inode = data->inode,
  5961. .stateid = &data->stateid,
  5962. .task_is_privileged = data->args.seq_args.sa_privileged,
  5963. .retrans = data->retrans,
  5964. };
  5965. if (!nfs4_sequence_done(task, &data->res.seq_res))
  5966. return;
  5967. trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
  5968. /* Handle Layoutreturn errors */
  5969. if (pnfs_roc_done(task, &data->args.lr_args, &data->res.lr_res,
  5970. &data->res.lr_ret) == -EAGAIN)
  5971. goto out_restart;
  5972. if (data->args.sattr_args && task->tk_status != 0) {
  5973. switch(data->res.sattr_ret) {
  5974. case 0:
  5975. data->args.sattr_args = NULL;
  5976. data->res.sattr_res = false;
  5977. break;
  5978. case -NFS4ERR_ADMIN_REVOKED:
  5979. case -NFS4ERR_DELEG_REVOKED:
  5980. case -NFS4ERR_EXPIRED:
  5981. case -NFS4ERR_BAD_STATEID:
  5982. /* Let the main handler below do stateid recovery */
  5983. break;
  5984. case -NFS4ERR_OLD_STATEID:
  5985. if (nfs4_refresh_delegation_stateid(&data->stateid,
  5986. data->inode))
  5987. goto out_restart;
  5988. fallthrough;
  5989. default:
  5990. data->args.sattr_args = NULL;
  5991. data->res.sattr_res = false;
  5992. goto out_restart;
  5993. }
  5994. }
  5995. switch (task->tk_status) {
  5996. case 0:
  5997. renew_lease(data->res.server, data->timestamp);
  5998. break;
  5999. case -NFS4ERR_ADMIN_REVOKED:
  6000. case -NFS4ERR_DELEG_REVOKED:
  6001. case -NFS4ERR_EXPIRED:
  6002. nfs4_free_revoked_stateid(data->res.server,
  6003. data->args.stateid,
  6004. task->tk_msg.rpc_cred);
  6005. fallthrough;
  6006. case -NFS4ERR_BAD_STATEID:
  6007. case -NFS4ERR_STALE_STATEID:
  6008. case -ETIMEDOUT:
  6009. task->tk_status = 0;
  6010. break;
  6011. case -NFS4ERR_OLD_STATEID:
  6012. if (!nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
  6013. nfs4_stateid_seqid_inc(&data->stateid);
  6014. if (data->args.bitmask) {
  6015. data->args.bitmask = NULL;
  6016. data->res.fattr = NULL;
  6017. }
  6018. goto out_restart;
  6019. case -NFS4ERR_ACCESS:
  6020. if (data->args.bitmask) {
  6021. data->args.bitmask = NULL;
  6022. data->res.fattr = NULL;
  6023. goto out_restart;
  6024. }
  6025. fallthrough;
  6026. default:
  6027. task->tk_status = nfs4_async_handle_exception(task,
  6028. data->res.server, task->tk_status,
  6029. &exception);
  6030. data->retrans = exception.retrans;
  6031. if (exception.retry)
  6032. goto out_restart;
  6033. }
  6034. nfs_delegation_mark_returned(data->inode, data->args.stateid);
  6035. data->rpc_status = task->tk_status;
  6036. return;
  6037. out_restart:
  6038. task->tk_status = 0;
  6039. rpc_restart_call_prepare(task);
  6040. }
  6041. static void nfs4_delegreturn_release(void *calldata)
  6042. {
  6043. struct nfs4_delegreturndata *data = calldata;
  6044. struct inode *inode = data->inode;
  6045. if (data->lr.roc)
  6046. pnfs_roc_release(&data->lr.arg, &data->lr.res,
  6047. data->res.lr_ret);
  6048. if (inode) {
  6049. nfs4_fattr_set_prechange(&data->fattr,
  6050. inode_peek_iversion_raw(inode));
  6051. nfs_refresh_inode(inode, &data->fattr);
  6052. nfs_iput_and_deactive(inode);
  6053. }
  6054. kfree(calldata);
  6055. }
  6056. static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
  6057. {
  6058. struct nfs4_delegreturndata *d_data;
  6059. struct pnfs_layout_hdr *lo;
  6060. d_data = data;
  6061. if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task)) {
  6062. nfs4_sequence_done(task, &d_data->res.seq_res);
  6063. return;
  6064. }
  6065. lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
  6066. if (lo && !pnfs_layout_is_valid(lo)) {
  6067. d_data->args.lr_args = NULL;
  6068. d_data->res.lr_res = NULL;
  6069. }
  6070. nfs4_setup_sequence(d_data->res.server->nfs_client,
  6071. &d_data->args.seq_args,
  6072. &d_data->res.seq_res,
  6073. task);
  6074. }
  6075. static const struct rpc_call_ops nfs4_delegreturn_ops = {
  6076. .rpc_call_prepare = nfs4_delegreturn_prepare,
  6077. .rpc_call_done = nfs4_delegreturn_done,
  6078. .rpc_release = nfs4_delegreturn_release,
  6079. };
  6080. static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred,
  6081. const nfs4_stateid *stateid,
  6082. struct nfs_delegation *delegation,
  6083. int issync)
  6084. {
  6085. struct nfs4_delegreturndata *data;
  6086. struct nfs_server *server = NFS_SERVER(inode);
  6087. struct rpc_task *task;
  6088. struct rpc_message msg = {
  6089. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
  6090. .rpc_cred = cred,
  6091. };
  6092. struct rpc_task_setup task_setup_data = {
  6093. .rpc_client = server->client,
  6094. .rpc_message = &msg,
  6095. .callback_ops = &nfs4_delegreturn_ops,
  6096. .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
  6097. };
  6098. int status = 0;
  6099. if (nfs_server_capable(inode, NFS_CAP_MOVEABLE))
  6100. task_setup_data.flags |= RPC_TASK_MOVEABLE;
  6101. data = kzalloc_obj(*data);
  6102. if (data == NULL)
  6103. return -ENOMEM;
  6104. nfs4_state_protect(server->nfs_client,
  6105. NFS_SP4_MACH_CRED_CLEANUP,
  6106. &task_setup_data.rpc_client, &msg);
  6107. data->args.fhandle = &data->fh;
  6108. data->args.stateid = &data->stateid;
  6109. nfs4_bitmask_set(data->args.bitmask_store,
  6110. server->cache_consistency_bitmask, inode, 0);
  6111. data->args.bitmask = data->args.bitmask_store;
  6112. nfs_copy_fh(&data->fh, NFS_FH(inode));
  6113. nfs4_stateid_copy(&data->stateid, stateid);
  6114. data->res.fattr = &data->fattr;
  6115. data->res.server = server;
  6116. data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
  6117. data->lr.arg.ld_private = &data->lr.ld_private;
  6118. nfs_fattr_init(data->res.fattr);
  6119. data->timestamp = jiffies;
  6120. data->rpc_status = 0;
  6121. data->inode = nfs_igrab_and_active(inode);
  6122. if (data->inode || issync) {
  6123. data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res,
  6124. cred, issync);
  6125. if (data->lr.roc) {
  6126. data->args.lr_args = &data->lr.arg;
  6127. data->res.lr_res = &data->lr.res;
  6128. }
  6129. }
  6130. if (delegation &&
  6131. test_bit(NFS_DELEGATION_DELEGTIME, &delegation->flags)) {
  6132. if (delegation->type & FMODE_READ) {
  6133. data->sattr.atime = inode_get_atime(inode);
  6134. data->sattr.atime_set = true;
  6135. }
  6136. if (delegation->type & FMODE_WRITE) {
  6137. data->sattr.mtime = inode_get_mtime(inode);
  6138. data->sattr.mtime_set = true;
  6139. }
  6140. data->args.sattr_args = &data->sattr;
  6141. data->res.sattr_res = true;
  6142. }
  6143. nfs4_init_sequence(server->nfs_client, &data->args.seq_args,
  6144. &data->res.seq_res, 1, !data->inode ? 1 : 0);
  6145. task_setup_data.callback_data = data;
  6146. msg.rpc_argp = &data->args;
  6147. msg.rpc_resp = &data->res;
  6148. task = rpc_run_task(&task_setup_data);
  6149. if (IS_ERR(task))
  6150. return PTR_ERR(task);
  6151. if (!issync)
  6152. goto out;
  6153. status = rpc_wait_for_completion_task(task);
  6154. if (status != 0)
  6155. goto out;
  6156. status = data->rpc_status;
  6157. out:
  6158. rpc_put_task(task);
  6159. return status;
  6160. }
  6161. int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred,
  6162. const nfs4_stateid *stateid,
  6163. struct nfs_delegation *delegation, int issync)
  6164. {
  6165. struct nfs_server *server = NFS_SERVER(inode);
  6166. struct nfs4_exception exception = { };
  6167. int err;
  6168. do {
  6169. err = _nfs4_proc_delegreturn(inode, cred, stateid,
  6170. delegation, issync);
  6171. trace_nfs4_delegreturn(inode, stateid, err);
  6172. switch (err) {
  6173. case -NFS4ERR_STALE_STATEID:
  6174. case -NFS4ERR_EXPIRED:
  6175. case 0:
  6176. return 0;
  6177. }
  6178. err = nfs4_handle_exception(server, err, &exception);
  6179. } while (exception.retry);
  6180. return err;
  6181. }
  6182. static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  6183. {
  6184. struct inode *inode = state->inode;
  6185. struct nfs_server *server = NFS_SERVER(inode);
  6186. struct nfs_client *clp = server->nfs_client;
  6187. struct nfs_lockt_args arg = {
  6188. .fh = NFS_FH(inode),
  6189. .fl = request,
  6190. };
  6191. struct nfs_lockt_res res = {
  6192. .denied = request,
  6193. };
  6194. struct rpc_message msg = {
  6195. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
  6196. .rpc_argp = &arg,
  6197. .rpc_resp = &res,
  6198. .rpc_cred = state->owner->so_cred,
  6199. };
  6200. struct nfs4_lock_state *lsp;
  6201. int status;
  6202. arg.lock_owner.clientid = clp->cl_clientid;
  6203. status = nfs4_set_lock_state(state, request);
  6204. if (status != 0)
  6205. goto out;
  6206. lsp = request->fl_u.nfs4_fl.owner;
  6207. arg.lock_owner.id = lsp->ls_seqid.owner_id;
  6208. arg.lock_owner.s_dev = server->s_dev;
  6209. status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
  6210. switch (status) {
  6211. case 0:
  6212. request->c.flc_type = F_UNLCK;
  6213. break;
  6214. case -NFS4ERR_DENIED:
  6215. status = 0;
  6216. }
  6217. request->fl_ops->fl_release_private(request);
  6218. request->fl_ops = NULL;
  6219. out:
  6220. return status;
  6221. }
  6222. static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  6223. {
  6224. struct nfs4_exception exception = {
  6225. .interruptible = true,
  6226. };
  6227. int err;
  6228. do {
  6229. err = _nfs4_proc_getlk(state, cmd, request);
  6230. trace_nfs4_get_lock(request, state, cmd, err);
  6231. err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
  6232. &exception);
  6233. } while (exception.retry);
  6234. return err;
  6235. }
  6236. /*
  6237. * Update the seqid of a lock stateid after receiving
  6238. * NFS4ERR_OLD_STATEID
  6239. */
  6240. static bool nfs4_refresh_lock_old_stateid(nfs4_stateid *dst,
  6241. struct nfs4_lock_state *lsp)
  6242. {
  6243. struct nfs4_state *state = lsp->ls_state;
  6244. bool ret = false;
  6245. spin_lock(&state->state_lock);
  6246. if (!nfs4_stateid_match_other(dst, &lsp->ls_stateid))
  6247. goto out;
  6248. if (!nfs4_stateid_is_newer(&lsp->ls_stateid, dst))
  6249. nfs4_stateid_seqid_inc(dst);
  6250. else
  6251. dst->seqid = lsp->ls_stateid.seqid;
  6252. ret = true;
  6253. out:
  6254. spin_unlock(&state->state_lock);
  6255. return ret;
  6256. }
  6257. static bool nfs4_sync_lock_stateid(nfs4_stateid *dst,
  6258. struct nfs4_lock_state *lsp)
  6259. {
  6260. struct nfs4_state *state = lsp->ls_state;
  6261. bool ret;
  6262. spin_lock(&state->state_lock);
  6263. ret = !nfs4_stateid_match_other(dst, &lsp->ls_stateid);
  6264. nfs4_stateid_copy(dst, &lsp->ls_stateid);
  6265. spin_unlock(&state->state_lock);
  6266. return ret;
  6267. }
  6268. struct nfs4_unlockdata {
  6269. struct nfs_locku_args arg;
  6270. struct nfs_locku_res res;
  6271. struct nfs4_lock_state *lsp;
  6272. struct nfs_open_context *ctx;
  6273. struct nfs_lock_context *l_ctx;
  6274. struct file_lock fl;
  6275. struct nfs_server *server;
  6276. unsigned long timestamp;
  6277. unsigned short retrans;
  6278. };
  6279. static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
  6280. struct nfs_open_context *ctx,
  6281. struct nfs4_lock_state *lsp,
  6282. struct nfs_seqid *seqid)
  6283. {
  6284. struct nfs4_unlockdata *p;
  6285. struct nfs4_state *state = lsp->ls_state;
  6286. struct inode *inode = state->inode;
  6287. struct nfs_lock_context *l_ctx;
  6288. p = kzalloc_obj(*p);
  6289. if (p == NULL)
  6290. return NULL;
  6291. l_ctx = nfs_get_lock_context(ctx);
  6292. if (!IS_ERR(l_ctx)) {
  6293. p->l_ctx = l_ctx;
  6294. } else {
  6295. kfree(p);
  6296. return NULL;
  6297. }
  6298. p->arg.fh = NFS_FH(inode);
  6299. p->arg.fl = &p->fl;
  6300. p->arg.seqid = seqid;
  6301. p->res.seqid = seqid;
  6302. p->lsp = lsp;
  6303. /* Ensure we don't close file until we're done freeing locks! */
  6304. p->ctx = get_nfs_open_context(ctx);
  6305. locks_init_lock(&p->fl);
  6306. locks_copy_lock(&p->fl, fl);
  6307. p->server = NFS_SERVER(inode);
  6308. spin_lock(&state->state_lock);
  6309. nfs4_stateid_copy(&p->arg.stateid, &lsp->ls_stateid);
  6310. spin_unlock(&state->state_lock);
  6311. return p;
  6312. }
  6313. static void nfs4_locku_release_calldata(void *data)
  6314. {
  6315. struct nfs4_unlockdata *calldata = data;
  6316. nfs_free_seqid(calldata->arg.seqid);
  6317. nfs4_put_lock_state(calldata->lsp);
  6318. nfs_put_lock_context(calldata->l_ctx);
  6319. put_nfs_open_context(calldata->ctx);
  6320. kfree(calldata);
  6321. }
  6322. static void nfs4_locku_done(struct rpc_task *task, void *data)
  6323. {
  6324. struct nfs4_unlockdata *calldata = data;
  6325. struct nfs4_exception exception = {
  6326. .inode = calldata->lsp->ls_state->inode,
  6327. .stateid = &calldata->arg.stateid,
  6328. .retrans = calldata->retrans,
  6329. };
  6330. if (!nfs4_sequence_done(task, &calldata->res.seq_res))
  6331. return;
  6332. switch (task->tk_status) {
  6333. case 0:
  6334. renew_lease(calldata->server, calldata->timestamp);
  6335. locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
  6336. if (nfs4_update_lock_stateid(calldata->lsp,
  6337. &calldata->res.stateid))
  6338. break;
  6339. fallthrough;
  6340. case -NFS4ERR_ADMIN_REVOKED:
  6341. case -NFS4ERR_EXPIRED:
  6342. nfs4_free_revoked_stateid(calldata->server,
  6343. &calldata->arg.stateid,
  6344. task->tk_msg.rpc_cred);
  6345. fallthrough;
  6346. case -NFS4ERR_BAD_STATEID:
  6347. case -NFS4ERR_STALE_STATEID:
  6348. if (nfs4_sync_lock_stateid(&calldata->arg.stateid,
  6349. calldata->lsp))
  6350. rpc_restart_call_prepare(task);
  6351. break;
  6352. case -NFS4ERR_OLD_STATEID:
  6353. if (nfs4_refresh_lock_old_stateid(&calldata->arg.stateid,
  6354. calldata->lsp))
  6355. rpc_restart_call_prepare(task);
  6356. break;
  6357. default:
  6358. task->tk_status = nfs4_async_handle_exception(task,
  6359. calldata->server, task->tk_status,
  6360. &exception);
  6361. calldata->retrans = exception.retrans;
  6362. if (exception.retry)
  6363. rpc_restart_call_prepare(task);
  6364. }
  6365. nfs_release_seqid(calldata->arg.seqid);
  6366. }
  6367. static void nfs4_locku_prepare(struct rpc_task *task, void *data)
  6368. {
  6369. struct nfs4_unlockdata *calldata = data;
  6370. if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
  6371. nfs_async_iocounter_wait(task, calldata->l_ctx))
  6372. return;
  6373. if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
  6374. goto out_wait;
  6375. if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
  6376. /* Note: exit _without_ running nfs4_locku_done */
  6377. goto out_no_action;
  6378. }
  6379. calldata->timestamp = jiffies;
  6380. if (nfs4_setup_sequence(calldata->server->nfs_client,
  6381. &calldata->arg.seq_args,
  6382. &calldata->res.seq_res,
  6383. task) != 0)
  6384. nfs_release_seqid(calldata->arg.seqid);
  6385. return;
  6386. out_no_action:
  6387. task->tk_action = NULL;
  6388. out_wait:
  6389. nfs4_sequence_done(task, &calldata->res.seq_res);
  6390. }
  6391. static const struct rpc_call_ops nfs4_locku_ops = {
  6392. .rpc_call_prepare = nfs4_locku_prepare,
  6393. .rpc_call_done = nfs4_locku_done,
  6394. .rpc_release = nfs4_locku_release_calldata,
  6395. };
  6396. static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
  6397. struct nfs_open_context *ctx,
  6398. struct nfs4_lock_state *lsp,
  6399. struct nfs_seqid *seqid)
  6400. {
  6401. struct nfs4_unlockdata *data;
  6402. struct nfs_client *clp = NFS_SERVER(lsp->ls_state->inode)->nfs_client;
  6403. struct rpc_message msg = {
  6404. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
  6405. .rpc_cred = ctx->cred,
  6406. };
  6407. struct rpc_task_setup task_setup_data = {
  6408. .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
  6409. .rpc_message = &msg,
  6410. .callback_ops = &nfs4_locku_ops,
  6411. .workqueue = nfsiod_workqueue,
  6412. .flags = RPC_TASK_ASYNC,
  6413. };
  6414. if (nfs_server_capable(lsp->ls_state->inode, NFS_CAP_MOVEABLE))
  6415. task_setup_data.flags |= RPC_TASK_MOVEABLE;
  6416. nfs4_state_protect(clp, NFS_SP4_MACH_CRED_CLEANUP,
  6417. &task_setup_data.rpc_client, &msg);
  6418. /* Ensure this is an unlock - when canceling a lock, the
  6419. * canceled lock is passed in, and it won't be an unlock.
  6420. */
  6421. fl->c.flc_type = F_UNLCK;
  6422. if (fl->c.flc_flags & FL_CLOSE)
  6423. set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
  6424. data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
  6425. if (data == NULL) {
  6426. nfs_free_seqid(seqid);
  6427. return ERR_PTR(-ENOMEM);
  6428. }
  6429. nfs4_init_sequence(clp, &data->arg.seq_args, &data->res.seq_res, 1, 0);
  6430. msg.rpc_argp = &data->arg;
  6431. msg.rpc_resp = &data->res;
  6432. task_setup_data.callback_data = data;
  6433. return rpc_run_task(&task_setup_data);
  6434. }
  6435. static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
  6436. {
  6437. struct inode *inode = state->inode;
  6438. struct nfs4_state_owner *sp = state->owner;
  6439. struct nfs_inode *nfsi = NFS_I(inode);
  6440. struct nfs_seqid *seqid;
  6441. struct nfs4_lock_state *lsp;
  6442. struct rpc_task *task;
  6443. struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
  6444. int status = 0;
  6445. unsigned char saved_flags = request->c.flc_flags;
  6446. status = nfs4_set_lock_state(state, request);
  6447. /* Unlock _before_ we do the RPC call */
  6448. request->c.flc_flags |= FL_EXISTS;
  6449. /* Exclude nfs_delegation_claim_locks() */
  6450. mutex_lock(&sp->so_delegreturn_mutex);
  6451. /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
  6452. down_read(&nfsi->rwsem);
  6453. if (locks_lock_inode_wait(inode, request) == -ENOENT) {
  6454. up_read(&nfsi->rwsem);
  6455. mutex_unlock(&sp->so_delegreturn_mutex);
  6456. goto out;
  6457. }
  6458. lsp = request->fl_u.nfs4_fl.owner;
  6459. set_bit(NFS_LOCK_UNLOCKING, &lsp->ls_flags);
  6460. up_read(&nfsi->rwsem);
  6461. mutex_unlock(&sp->so_delegreturn_mutex);
  6462. if (status != 0)
  6463. goto out;
  6464. /* Is this a delegated lock? */
  6465. if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
  6466. goto out;
  6467. alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
  6468. seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
  6469. status = -ENOMEM;
  6470. if (IS_ERR(seqid))
  6471. goto out;
  6472. task = nfs4_do_unlck(request,
  6473. nfs_file_open_context(request->c.flc_file),
  6474. lsp, seqid);
  6475. status = PTR_ERR(task);
  6476. if (IS_ERR(task))
  6477. goto out;
  6478. status = rpc_wait_for_completion_task(task);
  6479. rpc_put_task(task);
  6480. out:
  6481. request->c.flc_flags = saved_flags;
  6482. trace_nfs4_unlock(request, state, F_SETLK, status);
  6483. return status;
  6484. }
  6485. struct nfs4_lockdata {
  6486. struct nfs_lock_args arg;
  6487. struct nfs_lock_res res;
  6488. struct nfs4_lock_state *lsp;
  6489. struct nfs_open_context *ctx;
  6490. struct file_lock fl;
  6491. unsigned long timestamp;
  6492. int rpc_status;
  6493. int cancelled;
  6494. struct nfs_server *server;
  6495. };
  6496. static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
  6497. struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
  6498. gfp_t gfp_mask)
  6499. {
  6500. struct nfs4_lockdata *p;
  6501. struct inode *inode = lsp->ls_state->inode;
  6502. struct nfs_server *server = NFS_SERVER(inode);
  6503. struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
  6504. p = kzalloc_obj(*p, gfp_mask);
  6505. if (p == NULL)
  6506. return NULL;
  6507. p->arg.fh = NFS_FH(inode);
  6508. p->arg.fl = &p->fl;
  6509. p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
  6510. if (IS_ERR(p->arg.open_seqid))
  6511. goto out_free;
  6512. alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
  6513. p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
  6514. if (IS_ERR(p->arg.lock_seqid))
  6515. goto out_free_seqid;
  6516. p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
  6517. p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
  6518. p->arg.lock_owner.s_dev = server->s_dev;
  6519. p->res.lock_seqid = p->arg.lock_seqid;
  6520. p->lsp = lsp;
  6521. p->server = server;
  6522. p->ctx = get_nfs_open_context(ctx);
  6523. locks_init_lock(&p->fl);
  6524. locks_copy_lock(&p->fl, fl);
  6525. return p;
  6526. out_free_seqid:
  6527. nfs_free_seqid(p->arg.open_seqid);
  6528. out_free:
  6529. kfree(p);
  6530. return NULL;
  6531. }
  6532. static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
  6533. {
  6534. struct nfs4_lockdata *data = calldata;
  6535. struct nfs4_state *state = data->lsp->ls_state;
  6536. if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
  6537. goto out_wait;
  6538. /* Do we need to do an open_to_lock_owner? */
  6539. if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
  6540. if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
  6541. goto out_release_lock_seqid;
  6542. }
  6543. nfs4_stateid_copy(&data->arg.open_stateid,
  6544. &state->open_stateid);
  6545. data->arg.new_lock_owner = 1;
  6546. data->res.open_seqid = data->arg.open_seqid;
  6547. } else {
  6548. data->arg.new_lock_owner = 0;
  6549. nfs4_stateid_copy(&data->arg.lock_stateid,
  6550. &data->lsp->ls_stateid);
  6551. }
  6552. if (!nfs4_valid_open_stateid(state)) {
  6553. data->rpc_status = -EBADF;
  6554. task->tk_action = NULL;
  6555. goto out_release_open_seqid;
  6556. }
  6557. data->timestamp = jiffies;
  6558. if (nfs4_setup_sequence(data->server->nfs_client,
  6559. &data->arg.seq_args,
  6560. &data->res.seq_res,
  6561. task) == 0)
  6562. return;
  6563. out_release_open_seqid:
  6564. nfs_release_seqid(data->arg.open_seqid);
  6565. out_release_lock_seqid:
  6566. nfs_release_seqid(data->arg.lock_seqid);
  6567. out_wait:
  6568. nfs4_sequence_done(task, &data->res.seq_res);
  6569. dprintk("%s: ret = %d\n", __func__, data->rpc_status);
  6570. }
  6571. static void nfs4_lock_done(struct rpc_task *task, void *calldata)
  6572. {
  6573. struct nfs4_lockdata *data = calldata;
  6574. struct nfs4_lock_state *lsp = data->lsp;
  6575. if (!nfs4_sequence_done(task, &data->res.seq_res))
  6576. return;
  6577. data->rpc_status = task->tk_status;
  6578. switch (task->tk_status) {
  6579. case 0:
  6580. renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
  6581. data->timestamp);
  6582. if (data->arg.new_lock && !data->cancelled) {
  6583. data->fl.c.flc_flags &= ~(FL_SLEEP | FL_ACCESS);
  6584. if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
  6585. goto out_restart;
  6586. }
  6587. if (data->arg.new_lock_owner != 0) {
  6588. nfs_confirm_seqid(&lsp->ls_seqid, 0);
  6589. nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
  6590. set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
  6591. } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
  6592. goto out_restart;
  6593. break;
  6594. case -NFS4ERR_OLD_STATEID:
  6595. if (data->arg.new_lock_owner != 0 &&
  6596. nfs4_refresh_open_old_stateid(&data->arg.open_stateid,
  6597. lsp->ls_state))
  6598. goto out_restart;
  6599. if (nfs4_refresh_lock_old_stateid(&data->arg.lock_stateid, lsp))
  6600. goto out_restart;
  6601. fallthrough;
  6602. case -NFS4ERR_BAD_STATEID:
  6603. case -NFS4ERR_STALE_STATEID:
  6604. case -NFS4ERR_EXPIRED:
  6605. if (data->arg.new_lock_owner != 0) {
  6606. if (!nfs4_stateid_match(&data->arg.open_stateid,
  6607. &lsp->ls_state->open_stateid))
  6608. goto out_restart;
  6609. } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
  6610. &lsp->ls_stateid))
  6611. goto out_restart;
  6612. }
  6613. out_done:
  6614. dprintk("%s: ret = %d!\n", __func__, data->rpc_status);
  6615. return;
  6616. out_restart:
  6617. if (!data->cancelled)
  6618. rpc_restart_call_prepare(task);
  6619. goto out_done;
  6620. }
  6621. static void nfs4_lock_release(void *calldata)
  6622. {
  6623. struct nfs4_lockdata *data = calldata;
  6624. nfs_free_seqid(data->arg.open_seqid);
  6625. if (data->cancelled && data->rpc_status == 0) {
  6626. struct rpc_task *task;
  6627. task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
  6628. data->arg.lock_seqid);
  6629. if (!IS_ERR(task))
  6630. rpc_put_task_async(task);
  6631. dprintk("%s: cancelling lock!\n", __func__);
  6632. } else
  6633. nfs_free_seqid(data->arg.lock_seqid);
  6634. nfs4_put_lock_state(data->lsp);
  6635. put_nfs_open_context(data->ctx);
  6636. kfree(data);
  6637. }
  6638. static const struct rpc_call_ops nfs4_lock_ops = {
  6639. .rpc_call_prepare = nfs4_lock_prepare,
  6640. .rpc_call_done = nfs4_lock_done,
  6641. .rpc_release = nfs4_lock_release,
  6642. };
  6643. static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
  6644. {
  6645. switch (error) {
  6646. case -NFS4ERR_ADMIN_REVOKED:
  6647. case -NFS4ERR_EXPIRED:
  6648. case -NFS4ERR_BAD_STATEID:
  6649. lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
  6650. if (new_lock_owner != 0 ||
  6651. test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
  6652. nfs4_schedule_stateid_recovery(server, lsp->ls_state);
  6653. break;
  6654. case -NFS4ERR_STALE_STATEID:
  6655. lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
  6656. nfs4_schedule_lease_recovery(server->nfs_client);
  6657. }
  6658. }
  6659. static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
  6660. {
  6661. struct nfs4_lockdata *data;
  6662. struct rpc_task *task;
  6663. struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
  6664. struct rpc_message msg = {
  6665. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
  6666. .rpc_cred = state->owner->so_cred,
  6667. };
  6668. struct rpc_task_setup task_setup_data = {
  6669. .rpc_client = NFS_CLIENT(state->inode),
  6670. .rpc_message = &msg,
  6671. .callback_ops = &nfs4_lock_ops,
  6672. .workqueue = nfsiod_workqueue,
  6673. .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
  6674. };
  6675. int ret;
  6676. if (nfs_server_capable(state->inode, NFS_CAP_MOVEABLE))
  6677. task_setup_data.flags |= RPC_TASK_MOVEABLE;
  6678. data = nfs4_alloc_lockdata(fl,
  6679. nfs_file_open_context(fl->c.flc_file),
  6680. fl->fl_u.nfs4_fl.owner, GFP_KERNEL);
  6681. if (data == NULL)
  6682. return -ENOMEM;
  6683. if (IS_SETLKW(cmd))
  6684. data->arg.block = 1;
  6685. nfs4_init_sequence(clp, &data->arg.seq_args, &data->res.seq_res, 1,
  6686. recovery_type > NFS_LOCK_NEW);
  6687. msg.rpc_argp = &data->arg;
  6688. msg.rpc_resp = &data->res;
  6689. task_setup_data.callback_data = data;
  6690. if (recovery_type > NFS_LOCK_NEW) {
  6691. if (recovery_type == NFS_LOCK_RECLAIM)
  6692. data->arg.reclaim = NFS_LOCK_RECLAIM;
  6693. } else
  6694. data->arg.new_lock = 1;
  6695. task = rpc_run_task(&task_setup_data);
  6696. if (IS_ERR(task))
  6697. return PTR_ERR(task);
  6698. ret = rpc_wait_for_completion_task(task);
  6699. if (ret == 0) {
  6700. ret = data->rpc_status;
  6701. if (ret)
  6702. nfs4_handle_setlk_error(data->server, data->lsp,
  6703. data->arg.new_lock_owner, ret);
  6704. } else
  6705. data->cancelled = true;
  6706. trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
  6707. rpc_put_task(task);
  6708. dprintk("%s: ret = %d\n", __func__, ret);
  6709. return ret;
  6710. }
  6711. int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
  6712. {
  6713. struct nfs_server *server = NFS_SERVER(state->inode);
  6714. struct nfs4_exception exception = {
  6715. .inode = state->inode,
  6716. };
  6717. int err;
  6718. do {
  6719. /* Cache the lock if possible... */
  6720. if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
  6721. return 0;
  6722. err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
  6723. if (err != -NFS4ERR_DELAY)
  6724. break;
  6725. nfs4_handle_exception(server, err, &exception);
  6726. } while (exception.retry);
  6727. return err;
  6728. }
  6729. int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
  6730. {
  6731. struct nfs_server *server = NFS_SERVER(state->inode);
  6732. struct nfs4_exception exception = {
  6733. .inode = state->inode,
  6734. };
  6735. int err;
  6736. err = nfs4_set_lock_state(state, request);
  6737. if (err != 0)
  6738. return err;
  6739. if (!recover_lost_locks) {
  6740. set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
  6741. return 0;
  6742. }
  6743. do {
  6744. if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
  6745. return 0;
  6746. err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
  6747. switch (err) {
  6748. default:
  6749. goto out;
  6750. case -NFS4ERR_GRACE:
  6751. case -NFS4ERR_DELAY:
  6752. nfs4_handle_exception(server, err, &exception);
  6753. err = 0;
  6754. }
  6755. } while (exception.retry);
  6756. out:
  6757. return err;
  6758. }
  6759. static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
  6760. {
  6761. struct nfs4_lock_state *lsp;
  6762. int status;
  6763. status = nfs4_set_lock_state(state, request);
  6764. if (status != 0)
  6765. return status;
  6766. lsp = request->fl_u.nfs4_fl.owner;
  6767. if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
  6768. test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
  6769. return 0;
  6770. return nfs4_lock_expired(state, request);
  6771. }
  6772. static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  6773. {
  6774. struct nfs_inode *nfsi = NFS_I(state->inode);
  6775. struct nfs4_state_owner *sp = state->owner;
  6776. unsigned char flags = request->c.flc_flags;
  6777. int status;
  6778. request->c.flc_flags |= FL_ACCESS;
  6779. status = locks_lock_inode_wait(state->inode, request);
  6780. if (status < 0)
  6781. goto out;
  6782. mutex_lock(&sp->so_delegreturn_mutex);
  6783. down_read(&nfsi->rwsem);
  6784. if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
  6785. /* Yes: cache locks! */
  6786. /* ...but avoid races with delegation recall... */
  6787. request->c.flc_flags = flags & ~FL_SLEEP;
  6788. status = locks_lock_inode_wait(state->inode, request);
  6789. up_read(&nfsi->rwsem);
  6790. mutex_unlock(&sp->so_delegreturn_mutex);
  6791. goto out;
  6792. }
  6793. up_read(&nfsi->rwsem);
  6794. mutex_unlock(&sp->so_delegreturn_mutex);
  6795. status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
  6796. out:
  6797. request->c.flc_flags = flags;
  6798. return status;
  6799. }
  6800. static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  6801. {
  6802. struct nfs4_exception exception = {
  6803. .state = state,
  6804. .inode = state->inode,
  6805. .interruptible = true,
  6806. };
  6807. int err;
  6808. do {
  6809. err = _nfs4_proc_setlk(state, cmd, request);
  6810. if (err == -NFS4ERR_DENIED)
  6811. err = -EAGAIN;
  6812. err = nfs4_handle_exception(NFS_SERVER(state->inode),
  6813. err, &exception);
  6814. } while (exception.retry);
  6815. return err;
  6816. }
  6817. #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
  6818. #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
  6819. static int
  6820. nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
  6821. struct file_lock *request)
  6822. {
  6823. int status = -ERESTARTSYS;
  6824. unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
  6825. while(!signalled()) {
  6826. status = nfs4_proc_setlk(state, cmd, request);
  6827. if ((status != -EAGAIN) || IS_SETLK(cmd))
  6828. break;
  6829. __set_current_state(TASK_INTERRUPTIBLE|TASK_FREEZABLE);
  6830. schedule_timeout(timeout);
  6831. timeout *= 2;
  6832. timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
  6833. status = -ERESTARTSYS;
  6834. }
  6835. return status;
  6836. }
  6837. struct nfs4_lock_waiter {
  6838. struct inode *inode;
  6839. struct nfs_lowner owner;
  6840. wait_queue_entry_t wait;
  6841. };
  6842. static int
  6843. nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
  6844. {
  6845. struct nfs4_lock_waiter *waiter =
  6846. container_of(wait, struct nfs4_lock_waiter, wait);
  6847. /* NULL key means to wake up everyone */
  6848. if (key) {
  6849. struct cb_notify_lock_args *cbnl = key;
  6850. struct nfs_lowner *lowner = &cbnl->cbnl_owner,
  6851. *wowner = &waiter->owner;
  6852. /* Only wake if the callback was for the same owner. */
  6853. if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
  6854. return 0;
  6855. /* Make sure it's for the right inode */
  6856. if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
  6857. return 0;
  6858. }
  6859. return woken_wake_function(wait, mode, flags, key);
  6860. }
  6861. static int
  6862. nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  6863. {
  6864. struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
  6865. struct nfs_server *server = NFS_SERVER(state->inode);
  6866. struct nfs_client *clp = server->nfs_client;
  6867. wait_queue_head_t *q = &clp->cl_lock_waitq;
  6868. struct nfs4_lock_waiter waiter = {
  6869. .inode = state->inode,
  6870. .owner = { .clientid = clp->cl_clientid,
  6871. .id = lsp->ls_seqid.owner_id,
  6872. .s_dev = server->s_dev },
  6873. };
  6874. int status;
  6875. /* Don't bother with waitqueue if we don't expect a callback */
  6876. if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
  6877. return nfs4_retry_setlk_simple(state, cmd, request);
  6878. init_wait(&waiter.wait);
  6879. waiter.wait.func = nfs4_wake_lock_waiter;
  6880. add_wait_queue(q, &waiter.wait);
  6881. do {
  6882. status = nfs4_proc_setlk(state, cmd, request);
  6883. if (status != -EAGAIN || IS_SETLK(cmd))
  6884. break;
  6885. status = -ERESTARTSYS;
  6886. wait_woken(&waiter.wait, TASK_INTERRUPTIBLE|TASK_FREEZABLE,
  6887. NFS4_LOCK_MAXTIMEOUT);
  6888. } while (!signalled());
  6889. remove_wait_queue(q, &waiter.wait);
  6890. return status;
  6891. }
  6892. static int
  6893. nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
  6894. {
  6895. struct nfs_open_context *ctx;
  6896. struct nfs4_state *state;
  6897. int status;
  6898. /* verify open state */
  6899. ctx = nfs_file_open_context(filp);
  6900. state = ctx->state;
  6901. if (IS_GETLK(cmd)) {
  6902. if (state != NULL)
  6903. return nfs4_proc_getlk(state, F_GETLK, request);
  6904. return 0;
  6905. }
  6906. if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
  6907. return -EINVAL;
  6908. if (lock_is_unlock(request)) {
  6909. if (state != NULL)
  6910. return nfs4_proc_unlck(state, cmd, request);
  6911. return 0;
  6912. }
  6913. if (state == NULL)
  6914. return -ENOLCK;
  6915. if ((request->c.flc_flags & FL_POSIX) &&
  6916. !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
  6917. return -ENOLCK;
  6918. /*
  6919. * Don't rely on the VFS having checked the file open mode,
  6920. * since it won't do this for flock() locks.
  6921. */
  6922. switch (request->c.flc_type) {
  6923. case F_RDLCK:
  6924. if (!(filp->f_mode & FMODE_READ))
  6925. return -EBADF;
  6926. break;
  6927. case F_WRLCK:
  6928. if (!(filp->f_mode & FMODE_WRITE))
  6929. return -EBADF;
  6930. }
  6931. status = nfs4_set_lock_state(state, request);
  6932. if (status != 0)
  6933. return status;
  6934. return nfs4_retry_setlk(state, cmd, request);
  6935. }
  6936. static int nfs4_delete_lease(struct file *file, void **priv)
  6937. {
  6938. return generic_setlease(file, F_UNLCK, NULL, priv);
  6939. }
  6940. static int nfs4_add_lease(struct file *file, int arg, struct file_lease **lease,
  6941. void **priv)
  6942. {
  6943. struct inode *inode = file_inode(file);
  6944. fmode_t type = arg == F_RDLCK ? FMODE_READ : FMODE_WRITE;
  6945. int ret;
  6946. /* No delegation, no lease */
  6947. if (!nfs4_have_delegation(inode, type, 0))
  6948. return -EAGAIN;
  6949. ret = generic_setlease(file, arg, lease, priv);
  6950. if (ret || nfs4_have_delegation(inode, type, 0))
  6951. return ret;
  6952. /* We raced with a delegation return */
  6953. nfs4_delete_lease(file, priv);
  6954. return -EAGAIN;
  6955. }
  6956. int nfs4_proc_setlease(struct file *file, int arg, struct file_lease **lease,
  6957. void **priv)
  6958. {
  6959. switch (arg) {
  6960. case F_RDLCK:
  6961. case F_WRLCK:
  6962. return nfs4_add_lease(file, arg, lease, priv);
  6963. case F_UNLCK:
  6964. return nfs4_delete_lease(file, priv);
  6965. default:
  6966. return -EINVAL;
  6967. }
  6968. }
  6969. int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
  6970. {
  6971. struct nfs_server *server = NFS_SERVER(state->inode);
  6972. int err;
  6973. err = nfs4_set_lock_state(state, fl);
  6974. if (err != 0)
  6975. return err;
  6976. do {
  6977. err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
  6978. if (err != -NFS4ERR_DELAY && err != -NFS4ERR_GRACE)
  6979. break;
  6980. ssleep(1);
  6981. } while (err == -NFS4ERR_DELAY || err == -NFSERR_GRACE);
  6982. return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
  6983. }
  6984. #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
  6985. static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
  6986. struct mnt_idmap *idmap,
  6987. struct dentry *unused, struct inode *inode,
  6988. const char *key, const void *buf,
  6989. size_t buflen, int flags)
  6990. {
  6991. return nfs4_proc_set_acl(inode, buf, buflen, NFS4ACL_ACL);
  6992. }
  6993. static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
  6994. struct dentry *unused, struct inode *inode,
  6995. const char *key, void *buf, size_t buflen)
  6996. {
  6997. return nfs4_proc_get_acl(inode, buf, buflen, NFS4ACL_ACL);
  6998. }
  6999. static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
  7000. {
  7001. return nfs4_server_supports_acls(NFS_SB(dentry->d_sb), NFS4ACL_ACL);
  7002. }
  7003. #define XATTR_NAME_NFSV4_DACL "system.nfs4_dacl"
  7004. static int nfs4_xattr_set_nfs4_dacl(const struct xattr_handler *handler,
  7005. struct mnt_idmap *idmap,
  7006. struct dentry *unused, struct inode *inode,
  7007. const char *key, const void *buf,
  7008. size_t buflen, int flags)
  7009. {
  7010. return nfs4_proc_set_acl(inode, buf, buflen, NFS4ACL_DACL);
  7011. }
  7012. static int nfs4_xattr_get_nfs4_dacl(const struct xattr_handler *handler,
  7013. struct dentry *unused, struct inode *inode,
  7014. const char *key, void *buf, size_t buflen)
  7015. {
  7016. return nfs4_proc_get_acl(inode, buf, buflen, NFS4ACL_DACL);
  7017. }
  7018. static bool nfs4_xattr_list_nfs4_dacl(struct dentry *dentry)
  7019. {
  7020. return nfs4_server_supports_acls(NFS_SB(dentry->d_sb), NFS4ACL_DACL);
  7021. }
  7022. #define XATTR_NAME_NFSV4_SACL "system.nfs4_sacl"
  7023. static int nfs4_xattr_set_nfs4_sacl(const struct xattr_handler *handler,
  7024. struct mnt_idmap *idmap,
  7025. struct dentry *unused, struct inode *inode,
  7026. const char *key, const void *buf,
  7027. size_t buflen, int flags)
  7028. {
  7029. return nfs4_proc_set_acl(inode, buf, buflen, NFS4ACL_SACL);
  7030. }
  7031. static int nfs4_xattr_get_nfs4_sacl(const struct xattr_handler *handler,
  7032. struct dentry *unused, struct inode *inode,
  7033. const char *key, void *buf, size_t buflen)
  7034. {
  7035. return nfs4_proc_get_acl(inode, buf, buflen, NFS4ACL_SACL);
  7036. }
  7037. static bool nfs4_xattr_list_nfs4_sacl(struct dentry *dentry)
  7038. {
  7039. return nfs4_server_supports_acls(NFS_SB(dentry->d_sb), NFS4ACL_SACL);
  7040. }
  7041. #ifdef CONFIG_NFS_V4_SECURITY_LABEL
  7042. static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
  7043. struct mnt_idmap *idmap,
  7044. struct dentry *unused, struct inode *inode,
  7045. const char *key, const void *buf,
  7046. size_t buflen, int flags)
  7047. {
  7048. if (security_ismaclabel(key))
  7049. return nfs4_set_security_label(inode, buf, buflen);
  7050. return -EOPNOTSUPP;
  7051. }
  7052. static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
  7053. struct dentry *unused, struct inode *inode,
  7054. const char *key, void *buf, size_t buflen)
  7055. {
  7056. if (security_ismaclabel(key))
  7057. return nfs4_get_security_label(inode, buf, buflen);
  7058. return -EOPNOTSUPP;
  7059. }
  7060. static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
  7061. .prefix = XATTR_SECURITY_PREFIX,
  7062. .get = nfs4_xattr_get_nfs4_label,
  7063. .set = nfs4_xattr_set_nfs4_label,
  7064. };
  7065. #endif
  7066. #ifdef CONFIG_NFS_V4_2
  7067. static int nfs4_xattr_set_nfs4_user(const struct xattr_handler *handler,
  7068. struct mnt_idmap *idmap,
  7069. struct dentry *unused, struct inode *inode,
  7070. const char *key, const void *buf,
  7071. size_t buflen, int flags)
  7072. {
  7073. u32 mask;
  7074. int ret;
  7075. if (!nfs_server_capable(inode, NFS_CAP_XATTR))
  7076. return -EOPNOTSUPP;
  7077. /*
  7078. * There is no mapping from the MAY_* flags to the NFS_ACCESS_XA*
  7079. * flags right now. Handling of xattr operations use the normal
  7080. * file read/write permissions.
  7081. *
  7082. * Just in case the server has other ideas (which RFC 8276 allows),
  7083. * do a cached access check for the XA* flags to possibly avoid
  7084. * doing an RPC and getting EACCES back.
  7085. */
  7086. if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) {
  7087. if (!(mask & NFS_ACCESS_XAWRITE))
  7088. return -EACCES;
  7089. }
  7090. if (buf == NULL) {
  7091. ret = nfs42_proc_removexattr(inode, key);
  7092. if (!ret)
  7093. nfs4_xattr_cache_remove(inode, key);
  7094. } else {
  7095. ret = nfs42_proc_setxattr(inode, key, buf, buflen, flags);
  7096. if (!ret)
  7097. nfs4_xattr_cache_add(inode, key, buf, NULL, buflen);
  7098. }
  7099. return ret;
  7100. }
  7101. static int nfs4_xattr_get_nfs4_user(const struct xattr_handler *handler,
  7102. struct dentry *unused, struct inode *inode,
  7103. const char *key, void *buf, size_t buflen)
  7104. {
  7105. u32 mask;
  7106. ssize_t ret;
  7107. if (!nfs_server_capable(inode, NFS_CAP_XATTR))
  7108. return -EOPNOTSUPP;
  7109. if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) {
  7110. if (!(mask & NFS_ACCESS_XAREAD))
  7111. return -EACCES;
  7112. }
  7113. ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
  7114. if (ret)
  7115. return ret;
  7116. ret = nfs4_xattr_cache_get(inode, key, buf, buflen);
  7117. if (ret >= 0 || (ret < 0 && ret != -ENOENT))
  7118. return ret;
  7119. ret = nfs42_proc_getxattr(inode, key, buf, buflen);
  7120. return ret;
  7121. }
  7122. static ssize_t
  7123. nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
  7124. {
  7125. u64 cookie;
  7126. bool eof;
  7127. ssize_t ret, size;
  7128. char *buf;
  7129. size_t buflen;
  7130. u32 mask;
  7131. if (!nfs_server_capable(inode, NFS_CAP_XATTR))
  7132. return 0;
  7133. if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) {
  7134. if (!(mask & NFS_ACCESS_XALIST))
  7135. return 0;
  7136. }
  7137. ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
  7138. if (ret)
  7139. return ret;
  7140. ret = nfs4_xattr_cache_list(inode, list, list_len);
  7141. if (ret >= 0 || (ret < 0 && ret != -ENOENT))
  7142. return ret;
  7143. cookie = 0;
  7144. eof = false;
  7145. buflen = list_len ? list_len : XATTR_LIST_MAX;
  7146. buf = list_len ? list : NULL;
  7147. size = 0;
  7148. while (!eof) {
  7149. ret = nfs42_proc_listxattrs(inode, buf, buflen,
  7150. &cookie, &eof);
  7151. if (ret < 0)
  7152. return ret;
  7153. if (list_len) {
  7154. buf += ret;
  7155. buflen -= ret;
  7156. }
  7157. size += ret;
  7158. }
  7159. if (list_len)
  7160. nfs4_xattr_cache_set_list(inode, list, size);
  7161. return size;
  7162. }
  7163. #else
  7164. static ssize_t
  7165. nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
  7166. {
  7167. return 0;
  7168. }
  7169. #endif /* CONFIG_NFS_V4_2 */
  7170. /*
  7171. * nfs_fhget will use either the mounted_on_fileid or the fileid
  7172. */
  7173. static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
  7174. {
  7175. if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
  7176. (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
  7177. (fattr->valid & NFS_ATTR_FATTR_FSID) &&
  7178. (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
  7179. return;
  7180. fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
  7181. NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
  7182. fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
  7183. fattr->nlink = 2;
  7184. }
  7185. static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
  7186. const struct qstr *name,
  7187. struct nfs4_fs_locations *fs_locations,
  7188. struct page *page)
  7189. {
  7190. struct nfs_server *server = NFS_SERVER(dir);
  7191. u32 bitmask[3];
  7192. struct nfs4_fs_locations_arg args = {
  7193. .dir_fh = NFS_FH(dir),
  7194. .name = name,
  7195. .page = page,
  7196. .bitmask = bitmask,
  7197. };
  7198. struct nfs4_fs_locations_res res = {
  7199. .fs_locations = fs_locations,
  7200. };
  7201. struct rpc_message msg = {
  7202. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
  7203. .rpc_argp = &args,
  7204. .rpc_resp = &res,
  7205. };
  7206. int status;
  7207. dprintk("%s: start\n", __func__);
  7208. bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
  7209. bitmask[1] = nfs4_fattr_bitmap[1];
  7210. /* Ask for the fileid of the absent filesystem if mounted_on_fileid
  7211. * is not supported */
  7212. if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
  7213. bitmask[0] &= ~FATTR4_WORD0_FILEID;
  7214. else
  7215. bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
  7216. nfs_fattr_init(fs_locations->fattr);
  7217. fs_locations->server = server;
  7218. fs_locations->nlocations = 0;
  7219. status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
  7220. dprintk("%s: returned status = %d\n", __func__, status);
  7221. return status;
  7222. }
  7223. int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
  7224. const struct qstr *name,
  7225. struct nfs4_fs_locations *fs_locations,
  7226. struct page *page)
  7227. {
  7228. struct nfs4_exception exception = {
  7229. .interruptible = true,
  7230. };
  7231. int err;
  7232. do {
  7233. err = _nfs4_proc_fs_locations(client, dir, name,
  7234. fs_locations, page);
  7235. trace_nfs4_get_fs_locations(dir, name, err);
  7236. err = nfs4_handle_exception(NFS_SERVER(dir), err,
  7237. &exception);
  7238. } while (exception.retry);
  7239. return err;
  7240. }
  7241. /*
  7242. * This operation also signals the server that this client is
  7243. * performing migration recovery. The server can stop asserting
  7244. * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
  7245. * performing this operation is identified in the SEQUENCE
  7246. * operation in this compound.
  7247. *
  7248. * When the client supports GETATTR(fs_locations_info), it can
  7249. * be plumbed in here.
  7250. */
  7251. static int _nfs41_proc_get_locations(struct nfs_server *server,
  7252. struct nfs_fh *fhandle,
  7253. struct nfs4_fs_locations *locations,
  7254. struct page *page, const struct cred *cred)
  7255. {
  7256. struct rpc_clnt *clnt = server->client;
  7257. struct nfs_client *clp = server->nfs_client;
  7258. u32 bitmask[2] = {
  7259. [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
  7260. };
  7261. struct nfs4_fs_locations_arg args = {
  7262. .fh = fhandle,
  7263. .page = page,
  7264. .bitmask = bitmask,
  7265. .migration = 1, /* skip LOOKUP */
  7266. };
  7267. struct nfs4_fs_locations_res res = {
  7268. .fs_locations = locations,
  7269. .migration = 1,
  7270. };
  7271. struct rpc_message msg = {
  7272. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
  7273. .rpc_argp = &args,
  7274. .rpc_resp = &res,
  7275. .rpc_cred = cred,
  7276. };
  7277. struct nfs4_call_sync_data data = {
  7278. .seq_server = server,
  7279. .seq_args = &args.seq_args,
  7280. .seq_res = &res.seq_res,
  7281. };
  7282. struct rpc_task_setup task_setup_data = {
  7283. .rpc_client = clnt,
  7284. .rpc_message = &msg,
  7285. .callback_ops = clp->cl_mvops->call_sync_ops,
  7286. .callback_data = &data,
  7287. .flags = RPC_TASK_NO_ROUND_ROBIN,
  7288. };
  7289. int status;
  7290. nfs_fattr_init(locations->fattr);
  7291. locations->server = server;
  7292. locations->nlocations = 0;
  7293. nfs4_init_sequence(clp, &args.seq_args, &res.seq_res, 0, 1);
  7294. status = nfs4_call_sync_custom(&task_setup_data);
  7295. if (status == NFS4_OK &&
  7296. res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
  7297. status = -NFS4ERR_LEASE_MOVED;
  7298. return status;
  7299. }
  7300. /**
  7301. * nfs4_proc_get_locations - discover locations for a migrated FSID
  7302. * @server: pointer to nfs_server to process
  7303. * @fhandle: pointer to the kernel NFS client file handle
  7304. * @locations: result of query
  7305. * @page: buffer
  7306. * @cred: credential to use for this operation
  7307. *
  7308. * Returns NFS4_OK on success, a negative NFS4ERR status code if the
  7309. * operation failed, or a negative errno if a local error occurred.
  7310. *
  7311. * On success, "locations" is filled in, but if the server has
  7312. * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
  7313. * asserted.
  7314. *
  7315. * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
  7316. * from this client that require migration recovery.
  7317. */
  7318. int nfs4_proc_get_locations(struct nfs_server *server,
  7319. struct nfs_fh *fhandle,
  7320. struct nfs4_fs_locations *locations,
  7321. struct page *page, const struct cred *cred)
  7322. {
  7323. struct nfs_client *clp = server->nfs_client;
  7324. const struct nfs4_mig_recovery_ops *ops =
  7325. clp->cl_mvops->mig_recovery_ops;
  7326. struct nfs4_exception exception = {
  7327. .interruptible = true,
  7328. };
  7329. int status;
  7330. dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
  7331. (unsigned long long)server->fsid.major,
  7332. (unsigned long long)server->fsid.minor,
  7333. clp->cl_hostname);
  7334. nfs_display_fhandle(fhandle, __func__);
  7335. do {
  7336. status = ops->get_locations(server, fhandle, locations, page,
  7337. cred);
  7338. if (status != -NFS4ERR_DELAY)
  7339. break;
  7340. nfs4_handle_exception(server, status, &exception);
  7341. } while (exception.retry);
  7342. return status;
  7343. }
  7344. /*
  7345. * This operation also signals the server that this client is
  7346. * performing "lease moved" recovery. The server can stop asserting
  7347. * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
  7348. * this operation is identified in the SEQUENCE operation in this
  7349. * compound.
  7350. */
  7351. static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
  7352. {
  7353. struct nfs_server *server = NFS_SERVER(inode);
  7354. struct rpc_clnt *clnt = server->client;
  7355. struct nfs4_fsid_present_arg args = {
  7356. .fh = NFS_FH(inode),
  7357. };
  7358. struct nfs4_fsid_present_res res = {
  7359. };
  7360. struct rpc_message msg = {
  7361. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
  7362. .rpc_argp = &args,
  7363. .rpc_resp = &res,
  7364. .rpc_cred = cred,
  7365. };
  7366. int status;
  7367. res.fh = nfs_alloc_fhandle();
  7368. if (res.fh == NULL)
  7369. return -ENOMEM;
  7370. nfs4_init_sequence(server->nfs_client, &args.seq_args, &res.seq_res, 0, 1);
  7371. status = nfs4_call_sync_sequence(clnt, server, &msg,
  7372. &args.seq_args, &res.seq_res);
  7373. nfs_free_fhandle(res.fh);
  7374. if (status == NFS4_OK &&
  7375. res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
  7376. status = -NFS4ERR_LEASE_MOVED;
  7377. return status;
  7378. }
  7379. /**
  7380. * nfs4_proc_fsid_present - Is this FSID present or absent on server?
  7381. * @inode: inode on FSID to check
  7382. * @cred: credential to use for this operation
  7383. *
  7384. * Server indicates whether the FSID is present, moved, or not
  7385. * recognized. This operation is necessary to clear a LEASE_MOVED
  7386. * condition for this client ID.
  7387. *
  7388. * Returns NFS4_OK if the FSID is present on this server,
  7389. * -NFS4ERR_MOVED if the FSID is no longer present, a negative
  7390. * NFS4ERR code if some error occurred on the server, or a
  7391. * negative errno if a local failure occurred.
  7392. */
  7393. int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
  7394. {
  7395. struct nfs_server *server = NFS_SERVER(inode);
  7396. struct nfs_client *clp = server->nfs_client;
  7397. const struct nfs4_mig_recovery_ops *ops =
  7398. clp->cl_mvops->mig_recovery_ops;
  7399. struct nfs4_exception exception = {
  7400. .interruptible = true,
  7401. };
  7402. int status;
  7403. dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
  7404. (unsigned long long)server->fsid.major,
  7405. (unsigned long long)server->fsid.minor,
  7406. clp->cl_hostname);
  7407. nfs_display_fhandle(NFS_FH(inode), __func__);
  7408. do {
  7409. status = ops->fsid_present(inode, cred);
  7410. if (status != -NFS4ERR_DELAY)
  7411. break;
  7412. nfs4_handle_exception(server, status, &exception);
  7413. } while (exception.retry);
  7414. return status;
  7415. }
  7416. /*
  7417. * If 'use_integrity' is true and the state managment nfs_client
  7418. * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
  7419. * and the machine credential as per RFC3530bis and RFC5661 Security
  7420. * Considerations sections. Otherwise, just use the user cred with the
  7421. * filesystem's rpc_client.
  7422. */
  7423. static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
  7424. {
  7425. int status;
  7426. struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
  7427. struct nfs_client *clp = NFS_SERVER(dir)->nfs_client;
  7428. struct nfs4_secinfo_arg args = {
  7429. .dir_fh = NFS_FH(dir),
  7430. .name = name,
  7431. };
  7432. struct nfs4_secinfo_res res = {
  7433. .flavors = flavors,
  7434. };
  7435. struct rpc_message msg = {
  7436. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
  7437. .rpc_argp = &args,
  7438. .rpc_resp = &res,
  7439. };
  7440. struct nfs4_call_sync_data data = {
  7441. .seq_server = NFS_SERVER(dir),
  7442. .seq_args = &args.seq_args,
  7443. .seq_res = &res.seq_res,
  7444. };
  7445. struct rpc_task_setup task_setup = {
  7446. .rpc_client = clnt,
  7447. .rpc_message = &msg,
  7448. .callback_ops = clp->cl_mvops->call_sync_ops,
  7449. .callback_data = &data,
  7450. .flags = RPC_TASK_NO_ROUND_ROBIN,
  7451. };
  7452. const struct cred *cred = NULL;
  7453. if (use_integrity) {
  7454. clnt = clp->cl_rpcclient;
  7455. task_setup.rpc_client = clnt;
  7456. cred = nfs4_get_clid_cred(clp);
  7457. msg.rpc_cred = cred;
  7458. }
  7459. dprintk("NFS call secinfo %s\n", name->name);
  7460. nfs4_state_protect(clp, NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
  7461. nfs4_init_sequence(clp, &args.seq_args, &res.seq_res, 0, 0);
  7462. status = nfs4_call_sync_custom(&task_setup);
  7463. dprintk("NFS reply secinfo: %d\n", status);
  7464. put_cred(cred);
  7465. return status;
  7466. }
  7467. int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
  7468. struct nfs4_secinfo_flavors *flavors)
  7469. {
  7470. struct nfs4_exception exception = {
  7471. .interruptible = true,
  7472. };
  7473. int err;
  7474. do {
  7475. err = -NFS4ERR_WRONGSEC;
  7476. /* try to use integrity protection with machine cred */
  7477. if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
  7478. err = _nfs4_proc_secinfo(dir, name, flavors, true);
  7479. /*
  7480. * if unable to use integrity protection, or SECINFO with
  7481. * integrity protection returns NFS4ERR_WRONGSEC (which is
  7482. * disallowed by spec, but exists in deployed servers) use
  7483. * the current filesystem's rpc_client and the user cred.
  7484. */
  7485. if (err == -NFS4ERR_WRONGSEC)
  7486. err = _nfs4_proc_secinfo(dir, name, flavors, false);
  7487. trace_nfs4_secinfo(dir, name, err);
  7488. err = nfs4_handle_exception(NFS_SERVER(dir), err,
  7489. &exception);
  7490. } while (exception.retry);
  7491. return err;
  7492. }
  7493. /*
  7494. * Check the exchange flags returned by the server for invalid flags, having
  7495. * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
  7496. * DS flags set.
  7497. */
  7498. static int nfs4_check_cl_exchange_flags(u32 flags, u32 version)
  7499. {
  7500. if (version >= 2 && (flags & ~EXCHGID4_2_FLAG_MASK_R))
  7501. goto out_inval;
  7502. else if (version < 2 && (flags & ~EXCHGID4_FLAG_MASK_R))
  7503. goto out_inval;
  7504. if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
  7505. (flags & EXCHGID4_FLAG_USE_NON_PNFS))
  7506. goto out_inval;
  7507. if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
  7508. goto out_inval;
  7509. return NFS_OK;
  7510. out_inval:
  7511. return -NFS4ERR_INVAL;
  7512. }
  7513. static bool
  7514. nfs41_same_server_scope(struct nfs41_server_scope *a,
  7515. struct nfs41_server_scope *b)
  7516. {
  7517. if (a->server_scope_sz != b->server_scope_sz)
  7518. return false;
  7519. return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
  7520. }
  7521. static void
  7522. nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
  7523. {
  7524. struct nfs41_bind_conn_to_session_args *args = task->tk_msg.rpc_argp;
  7525. struct nfs41_bind_conn_to_session_res *res = task->tk_msg.rpc_resp;
  7526. struct nfs_client *clp = args->client;
  7527. switch (task->tk_status) {
  7528. case -NFS4ERR_BADSESSION:
  7529. case -NFS4ERR_DEADSESSION:
  7530. nfs4_schedule_session_recovery(clp->cl_session,
  7531. task->tk_status);
  7532. return;
  7533. }
  7534. if (args->dir == NFS4_CDFC4_FORE_OR_BOTH &&
  7535. res->dir != NFS4_CDFS4_BOTH) {
  7536. rpc_task_close_connection(task);
  7537. if (args->retries++ < MAX_BIND_CONN_TO_SESSION_RETRIES)
  7538. rpc_restart_call(task);
  7539. }
  7540. }
  7541. static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
  7542. .rpc_call_done = nfs4_bind_one_conn_to_session_done,
  7543. };
  7544. /*
  7545. * nfs4_proc_bind_one_conn_to_session()
  7546. *
  7547. * The 4.1 client currently uses the same TCP connection for the
  7548. * fore and backchannel.
  7549. */
  7550. static
  7551. int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
  7552. struct rpc_xprt *xprt,
  7553. struct nfs_client *clp,
  7554. const struct cred *cred)
  7555. {
  7556. int status;
  7557. struct nfs41_bind_conn_to_session_args args = {
  7558. .client = clp,
  7559. .dir = NFS4_CDFC4_FORE_OR_BOTH,
  7560. .retries = 0,
  7561. };
  7562. struct nfs41_bind_conn_to_session_res res;
  7563. struct rpc_message msg = {
  7564. .rpc_proc =
  7565. &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
  7566. .rpc_argp = &args,
  7567. .rpc_resp = &res,
  7568. .rpc_cred = cred,
  7569. };
  7570. struct rpc_task_setup task_setup_data = {
  7571. .rpc_client = clnt,
  7572. .rpc_xprt = xprt,
  7573. .callback_ops = &nfs4_bind_one_conn_to_session_ops,
  7574. .rpc_message = &msg,
  7575. .flags = RPC_TASK_TIMEOUT,
  7576. };
  7577. struct rpc_task *task;
  7578. nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
  7579. if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
  7580. args.dir = NFS4_CDFC4_FORE;
  7581. /* Do not set the backchannel flag unless this is clnt->cl_xprt */
  7582. if (xprt != rcu_access_pointer(clnt->cl_xprt))
  7583. args.dir = NFS4_CDFC4_FORE;
  7584. task = rpc_run_task(&task_setup_data);
  7585. if (!IS_ERR(task)) {
  7586. status = task->tk_status;
  7587. rpc_put_task(task);
  7588. } else
  7589. status = PTR_ERR(task);
  7590. trace_nfs4_bind_conn_to_session(clp, status);
  7591. if (status == 0) {
  7592. if (memcmp(res.sessionid.data,
  7593. clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
  7594. dprintk("NFS: %s: Session ID mismatch\n", __func__);
  7595. return -EIO;
  7596. }
  7597. if ((res.dir & args.dir) != res.dir || res.dir == 0) {
  7598. dprintk("NFS: %s: Unexpected direction from server\n",
  7599. __func__);
  7600. return -EIO;
  7601. }
  7602. if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
  7603. dprintk("NFS: %s: Server returned RDMA mode = true\n",
  7604. __func__);
  7605. return -EIO;
  7606. }
  7607. }
  7608. return status;
  7609. }
  7610. struct rpc_bind_conn_calldata {
  7611. struct nfs_client *clp;
  7612. const struct cred *cred;
  7613. };
  7614. static int
  7615. nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
  7616. struct rpc_xprt *xprt,
  7617. void *calldata)
  7618. {
  7619. struct rpc_bind_conn_calldata *p = calldata;
  7620. return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
  7621. }
  7622. int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
  7623. {
  7624. struct rpc_bind_conn_calldata data = {
  7625. .clp = clp,
  7626. .cred = cred,
  7627. };
  7628. return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
  7629. nfs4_proc_bind_conn_to_session_callback, &data);
  7630. }
  7631. /*
  7632. * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
  7633. * and operations we'd like to see to enable certain features in the allow map
  7634. */
  7635. static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
  7636. .how = SP4_MACH_CRED,
  7637. .enforce.u.words = {
  7638. [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
  7639. 1 << (OP_EXCHANGE_ID - 32) |
  7640. 1 << (OP_CREATE_SESSION - 32) |
  7641. 1 << (OP_DESTROY_SESSION - 32) |
  7642. 1 << (OP_DESTROY_CLIENTID - 32)
  7643. },
  7644. .allow.u.words = {
  7645. [0] = 1 << (OP_CLOSE) |
  7646. 1 << (OP_OPEN_DOWNGRADE) |
  7647. 1 << (OP_LOCKU) |
  7648. 1 << (OP_DELEGRETURN) |
  7649. 1 << (OP_COMMIT),
  7650. [1] = 1 << (OP_SECINFO - 32) |
  7651. 1 << (OP_SECINFO_NO_NAME - 32) |
  7652. 1 << (OP_LAYOUTRETURN - 32) |
  7653. 1 << (OP_TEST_STATEID - 32) |
  7654. 1 << (OP_FREE_STATEID - 32) |
  7655. 1 << (OP_WRITE - 32)
  7656. }
  7657. };
  7658. /*
  7659. * Select the state protection mode for client `clp' given the server results
  7660. * from exchange_id in `sp'.
  7661. *
  7662. * Returns 0 on success, negative errno otherwise.
  7663. */
  7664. static int nfs4_sp4_select_mode(struct nfs_client *clp,
  7665. struct nfs41_state_protection *sp)
  7666. {
  7667. static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
  7668. [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
  7669. 1 << (OP_EXCHANGE_ID - 32) |
  7670. 1 << (OP_CREATE_SESSION - 32) |
  7671. 1 << (OP_DESTROY_SESSION - 32) |
  7672. 1 << (OP_DESTROY_CLIENTID - 32)
  7673. };
  7674. unsigned long flags = 0;
  7675. unsigned int i;
  7676. int ret = 0;
  7677. if (sp->how == SP4_MACH_CRED) {
  7678. /* Print state protect result */
  7679. dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
  7680. for (i = 0; i <= LAST_NFS4_OP; i++) {
  7681. if (test_bit(i, sp->enforce.u.longs))
  7682. dfprintk(MOUNT, " enforce op %d\n", i);
  7683. if (test_bit(i, sp->allow.u.longs))
  7684. dfprintk(MOUNT, " allow op %d\n", i);
  7685. }
  7686. /* make sure nothing is on enforce list that isn't supported */
  7687. for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
  7688. if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
  7689. dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
  7690. ret = -EINVAL;
  7691. goto out;
  7692. }
  7693. }
  7694. /*
  7695. * Minimal mode - state operations are allowed to use machine
  7696. * credential. Note this already happens by default, so the
  7697. * client doesn't have to do anything more than the negotiation.
  7698. *
  7699. * NOTE: we don't care if EXCHANGE_ID is in the list -
  7700. * we're already using the machine cred for exchange_id
  7701. * and will never use a different cred.
  7702. */
  7703. if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
  7704. test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
  7705. test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
  7706. test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
  7707. dfprintk(MOUNT, "sp4_mach_cred:\n");
  7708. dfprintk(MOUNT, " minimal mode enabled\n");
  7709. __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
  7710. } else {
  7711. dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
  7712. ret = -EINVAL;
  7713. goto out;
  7714. }
  7715. if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
  7716. test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
  7717. test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
  7718. test_bit(OP_LOCKU, sp->allow.u.longs)) {
  7719. dfprintk(MOUNT, " cleanup mode enabled\n");
  7720. __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
  7721. }
  7722. if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
  7723. dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
  7724. __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
  7725. }
  7726. if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
  7727. test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
  7728. dfprintk(MOUNT, " secinfo mode enabled\n");
  7729. __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
  7730. }
  7731. if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
  7732. test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
  7733. dfprintk(MOUNT, " stateid mode enabled\n");
  7734. __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
  7735. }
  7736. if (test_bit(OP_WRITE, sp->allow.u.longs)) {
  7737. dfprintk(MOUNT, " write mode enabled\n");
  7738. __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
  7739. }
  7740. if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
  7741. dfprintk(MOUNT, " commit mode enabled\n");
  7742. __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
  7743. }
  7744. }
  7745. out:
  7746. clp->cl_sp4_flags = flags;
  7747. return ret;
  7748. }
  7749. struct nfs41_exchange_id_data {
  7750. struct nfs41_exchange_id_res res;
  7751. struct nfs41_exchange_id_args args;
  7752. };
  7753. static void nfs4_exchange_id_release(void *data)
  7754. {
  7755. struct nfs41_exchange_id_data *cdata =
  7756. (struct nfs41_exchange_id_data *)data;
  7757. nfs_put_client(cdata->args.client);
  7758. kfree(cdata->res.impl_id);
  7759. kfree(cdata->res.server_scope);
  7760. kfree(cdata->res.server_owner);
  7761. kfree(cdata);
  7762. }
  7763. static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
  7764. .rpc_release = nfs4_exchange_id_release,
  7765. };
  7766. /*
  7767. * _nfs4_proc_exchange_id()
  7768. *
  7769. * Wrapper for EXCHANGE_ID operation.
  7770. */
  7771. static struct rpc_task *
  7772. nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
  7773. u32 sp4_how, struct rpc_xprt *xprt)
  7774. {
  7775. struct rpc_message msg = {
  7776. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
  7777. .rpc_cred = cred,
  7778. };
  7779. struct rpc_task_setup task_setup_data = {
  7780. .rpc_client = clp->cl_rpcclient,
  7781. .callback_ops = &nfs4_exchange_id_call_ops,
  7782. .rpc_message = &msg,
  7783. .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
  7784. };
  7785. struct nfs41_exchange_id_data *calldata;
  7786. int status;
  7787. if (!refcount_inc_not_zero(&clp->cl_count))
  7788. return ERR_PTR(-EIO);
  7789. status = -ENOMEM;
  7790. calldata = kzalloc_obj(*calldata, GFP_NOFS);
  7791. if (!calldata)
  7792. goto out;
  7793. nfs4_init_boot_verifier(clp, &calldata->args.verifier);
  7794. status = nfs4_init_uniform_client_string(clp);
  7795. if (status)
  7796. goto out_calldata;
  7797. calldata->res.server_owner = kzalloc_obj(struct nfs41_server_owner,
  7798. GFP_NOFS);
  7799. status = -ENOMEM;
  7800. if (unlikely(calldata->res.server_owner == NULL))
  7801. goto out_calldata;
  7802. calldata->res.server_scope = kzalloc_obj(struct nfs41_server_scope,
  7803. GFP_NOFS);
  7804. if (unlikely(calldata->res.server_scope == NULL))
  7805. goto out_server_owner;
  7806. calldata->res.impl_id = kzalloc_obj(struct nfs41_impl_id, GFP_NOFS);
  7807. if (unlikely(calldata->res.impl_id == NULL))
  7808. goto out_server_scope;
  7809. switch (sp4_how) {
  7810. case SP4_NONE:
  7811. calldata->args.state_protect.how = SP4_NONE;
  7812. break;
  7813. case SP4_MACH_CRED:
  7814. calldata->args.state_protect = nfs4_sp4_mach_cred_request;
  7815. break;
  7816. default:
  7817. /* unsupported! */
  7818. WARN_ON_ONCE(1);
  7819. status = -EINVAL;
  7820. goto out_impl_id;
  7821. }
  7822. if (xprt) {
  7823. task_setup_data.rpc_xprt = xprt;
  7824. task_setup_data.flags |= RPC_TASK_SOFTCONN;
  7825. memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
  7826. sizeof(calldata->args.verifier.data));
  7827. }
  7828. calldata->args.client = clp;
  7829. calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
  7830. EXCHGID4_FLAG_BIND_PRINC_STATEID;
  7831. #ifdef CONFIG_NFS_V4_1_MIGRATION
  7832. calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
  7833. #endif
  7834. if (test_bit(NFS_CS_PNFS, &clp->cl_flags))
  7835. calldata->args.flags |= EXCHGID4_FLAG_USE_PNFS_DS;
  7836. msg.rpc_argp = &calldata->args;
  7837. msg.rpc_resp = &calldata->res;
  7838. task_setup_data.callback_data = calldata;
  7839. return rpc_run_task(&task_setup_data);
  7840. out_impl_id:
  7841. kfree(calldata->res.impl_id);
  7842. out_server_scope:
  7843. kfree(calldata->res.server_scope);
  7844. out_server_owner:
  7845. kfree(calldata->res.server_owner);
  7846. out_calldata:
  7847. kfree(calldata);
  7848. out:
  7849. nfs_put_client(clp);
  7850. return ERR_PTR(status);
  7851. }
  7852. /*
  7853. * _nfs4_proc_exchange_id()
  7854. *
  7855. * Wrapper for EXCHANGE_ID operation.
  7856. */
  7857. static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
  7858. u32 sp4_how)
  7859. {
  7860. struct rpc_task *task;
  7861. struct nfs41_exchange_id_args *argp;
  7862. struct nfs41_exchange_id_res *resp;
  7863. unsigned long now = jiffies;
  7864. int status;
  7865. task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
  7866. if (IS_ERR(task))
  7867. return PTR_ERR(task);
  7868. argp = task->tk_msg.rpc_argp;
  7869. resp = task->tk_msg.rpc_resp;
  7870. status = task->tk_status;
  7871. if (status != 0)
  7872. goto out;
  7873. status = nfs4_check_cl_exchange_flags(resp->flags,
  7874. clp->cl_mvops->minor_version);
  7875. if (status != 0)
  7876. goto out;
  7877. status = nfs4_sp4_select_mode(clp, &resp->state_protect);
  7878. if (status != 0)
  7879. goto out;
  7880. do_renew_lease(clp, now);
  7881. clp->cl_clientid = resp->clientid;
  7882. clp->cl_exchange_flags = resp->flags;
  7883. clp->cl_seqid = resp->seqid;
  7884. /* Client ID is not confirmed */
  7885. if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
  7886. clear_bit(NFS4_SESSION_ESTABLISHED,
  7887. &clp->cl_session->session_state);
  7888. if (clp->cl_serverscope != NULL &&
  7889. !nfs41_same_server_scope(clp->cl_serverscope,
  7890. resp->server_scope)) {
  7891. dprintk("%s: server_scope mismatch detected\n",
  7892. __func__);
  7893. set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
  7894. }
  7895. swap(clp->cl_serverowner, resp->server_owner);
  7896. swap(clp->cl_serverscope, resp->server_scope);
  7897. swap(clp->cl_implid, resp->impl_id);
  7898. /* Save the EXCHANGE_ID verifier session trunk tests */
  7899. memcpy(clp->cl_confirm.data, argp->verifier.data,
  7900. sizeof(clp->cl_confirm.data));
  7901. out:
  7902. trace_nfs4_exchange_id(clp, status);
  7903. rpc_put_task(task);
  7904. return status;
  7905. }
  7906. /*
  7907. * nfs4_proc_exchange_id()
  7908. *
  7909. * Returns zero, a negative errno, or a negative NFS4ERR status code.
  7910. *
  7911. * Since the clientid has expired, all compounds using sessions
  7912. * associated with the stale clientid will be returning
  7913. * NFS4ERR_BADSESSION in the sequence operation, and will therefore
  7914. * be in some phase of session reset.
  7915. *
  7916. * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
  7917. */
  7918. int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
  7919. {
  7920. rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
  7921. int status;
  7922. /* try SP4_MACH_CRED if krb5i/p */
  7923. if (authflavor == RPC_AUTH_GSS_KRB5I ||
  7924. authflavor == RPC_AUTH_GSS_KRB5P) {
  7925. status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
  7926. if (!status)
  7927. return 0;
  7928. }
  7929. /* try SP4_NONE */
  7930. return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
  7931. }
  7932. /**
  7933. * nfs4_test_session_trunk
  7934. *
  7935. * This is an add_xprt_test() test function called from
  7936. * rpc_clnt_setup_test_and_add_xprt.
  7937. *
  7938. * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
  7939. * and is dereferrenced in nfs4_exchange_id_release
  7940. *
  7941. * Upon success, add the new transport to the rpc_clnt
  7942. *
  7943. * @clnt: struct rpc_clnt to get new transport
  7944. * @xprt: the rpc_xprt to test
  7945. * @data: call data for _nfs4_proc_exchange_id.
  7946. */
  7947. void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
  7948. void *data)
  7949. {
  7950. struct nfs4_add_xprt_data *adata = data;
  7951. struct rpc_task *task;
  7952. int status;
  7953. u32 sp4_how;
  7954. dprintk("--> %s try %s\n", __func__,
  7955. xprt->address_strings[RPC_DISPLAY_ADDR]);
  7956. sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
  7957. try_again:
  7958. /* Test connection for session trunking. Async exchange_id call */
  7959. task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
  7960. if (IS_ERR(task))
  7961. return;
  7962. status = task->tk_status;
  7963. if (status == 0) {
  7964. status = nfs4_detect_session_trunking(adata->clp,
  7965. task->tk_msg.rpc_resp, xprt);
  7966. trace_nfs4_trunked_exchange_id(adata->clp,
  7967. xprt->address_strings[RPC_DISPLAY_ADDR], status);
  7968. }
  7969. if (status == 0)
  7970. rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
  7971. else if (status != -NFS4ERR_DELAY && rpc_clnt_xprt_switch_has_addr(clnt,
  7972. (struct sockaddr *)&xprt->addr))
  7973. rpc_clnt_xprt_switch_remove_xprt(clnt, xprt);
  7974. rpc_put_task(task);
  7975. if (status == -NFS4ERR_DELAY) {
  7976. ssleep(1);
  7977. goto try_again;
  7978. }
  7979. }
  7980. EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
  7981. static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
  7982. const struct cred *cred)
  7983. {
  7984. struct rpc_message msg = {
  7985. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
  7986. .rpc_argp = clp,
  7987. .rpc_cred = cred,
  7988. };
  7989. int status;
  7990. status = rpc_call_sync(clp->cl_rpcclient, &msg,
  7991. RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
  7992. trace_nfs4_destroy_clientid(clp, status);
  7993. if (status)
  7994. dprintk("NFS: Got error %d from the server %s on "
  7995. "DESTROY_CLIENTID.", status, clp->cl_hostname);
  7996. return status;
  7997. }
  7998. static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
  7999. const struct cred *cred)
  8000. {
  8001. unsigned int loop;
  8002. int ret;
  8003. for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
  8004. ret = _nfs4_proc_destroy_clientid(clp, cred);
  8005. switch (ret) {
  8006. case -NFS4ERR_DELAY:
  8007. case -NFS4ERR_CLIENTID_BUSY:
  8008. ssleep(1);
  8009. break;
  8010. default:
  8011. return ret;
  8012. }
  8013. }
  8014. return 0;
  8015. }
  8016. int nfs4_destroy_clientid(struct nfs_client *clp)
  8017. {
  8018. const struct cred *cred;
  8019. int ret = 0;
  8020. if (clp->cl_mvops->minor_version < 1)
  8021. goto out;
  8022. if (clp->cl_exchange_flags == 0)
  8023. goto out;
  8024. if (clp->cl_preserve_clid)
  8025. goto out;
  8026. cred = nfs4_get_clid_cred(clp);
  8027. ret = nfs4_proc_destroy_clientid(clp, cred);
  8028. put_cred(cred);
  8029. switch (ret) {
  8030. case 0:
  8031. case -NFS4ERR_STALE_CLIENTID:
  8032. clp->cl_exchange_flags = 0;
  8033. }
  8034. out:
  8035. return ret;
  8036. }
  8037. struct nfs4_get_lease_time_data {
  8038. struct nfs4_get_lease_time_args *args;
  8039. struct nfs4_get_lease_time_res *res;
  8040. struct nfs_client *clp;
  8041. };
  8042. static void nfs4_get_lease_time_prepare(struct rpc_task *task,
  8043. void *calldata)
  8044. {
  8045. struct nfs4_get_lease_time_data *data =
  8046. (struct nfs4_get_lease_time_data *)calldata;
  8047. /* just setup sequence, do not trigger session recovery
  8048. since we're invoked within one */
  8049. nfs4_setup_sequence(data->clp,
  8050. &data->args->la_seq_args,
  8051. &data->res->lr_seq_res,
  8052. task);
  8053. }
  8054. /*
  8055. * Called from nfs4_state_manager thread for session setup, so don't recover
  8056. * from sequence operation or clientid errors.
  8057. */
  8058. static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
  8059. {
  8060. struct nfs4_get_lease_time_data *data =
  8061. (struct nfs4_get_lease_time_data *)calldata;
  8062. if (!nfs4_sequence_done(task, &data->res->lr_seq_res))
  8063. return;
  8064. switch (task->tk_status) {
  8065. case -NFS4ERR_DELAY:
  8066. case -NFS4ERR_GRACE:
  8067. rpc_delay(task, NFS4_POLL_RETRY_MIN);
  8068. task->tk_status = 0;
  8069. fallthrough;
  8070. case -NFS4ERR_RETRY_UNCACHED_REP:
  8071. rpc_restart_call_prepare(task);
  8072. return;
  8073. }
  8074. }
  8075. static const struct rpc_call_ops nfs4_get_lease_time_ops = {
  8076. .rpc_call_prepare = nfs4_get_lease_time_prepare,
  8077. .rpc_call_done = nfs4_get_lease_time_done,
  8078. };
  8079. int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
  8080. {
  8081. struct nfs4_get_lease_time_args args;
  8082. struct nfs4_get_lease_time_res res = {
  8083. .lr_fsinfo = fsinfo,
  8084. };
  8085. struct nfs4_get_lease_time_data data = {
  8086. .args = &args,
  8087. .res = &res,
  8088. .clp = clp,
  8089. };
  8090. struct rpc_message msg = {
  8091. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
  8092. .rpc_argp = &args,
  8093. .rpc_resp = &res,
  8094. };
  8095. struct rpc_task_setup task_setup = {
  8096. .rpc_client = clp->cl_rpcclient,
  8097. .rpc_message = &msg,
  8098. .callback_ops = &nfs4_get_lease_time_ops,
  8099. .callback_data = &data,
  8100. .flags = RPC_TASK_TIMEOUT,
  8101. };
  8102. nfs4_init_sequence(clp, &args.la_seq_args, &res.lr_seq_res, 0, 1);
  8103. return nfs4_call_sync_custom(&task_setup);
  8104. }
  8105. /*
  8106. * Initialize the values to be used by the client in CREATE_SESSION
  8107. * If nfs4_init_session set the fore channel request and response sizes,
  8108. * use them.
  8109. *
  8110. * Set the back channel max_resp_sz_cached to zero to force the client to
  8111. * always set csa_cachethis to FALSE because the current implementation
  8112. * of the back channel DRC only supports caching the CB_SEQUENCE operation.
  8113. */
  8114. static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
  8115. struct rpc_clnt *clnt)
  8116. {
  8117. unsigned int max_rqst_sz, max_resp_sz;
  8118. unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
  8119. unsigned int max_bc_slots = rpc_num_bc_slots(clnt);
  8120. max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
  8121. max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
  8122. /* Fore channel attributes */
  8123. args->fc_attrs.max_rqst_sz = max_rqst_sz;
  8124. args->fc_attrs.max_resp_sz = max_resp_sz;
  8125. args->fc_attrs.max_ops = NFS4_MAX_OPS;
  8126. args->fc_attrs.max_reqs = max_session_slots;
  8127. dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
  8128. "max_ops=%u max_reqs=%u\n",
  8129. __func__,
  8130. args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
  8131. args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
  8132. /* Back channel attributes */
  8133. args->bc_attrs.max_rqst_sz = max_bc_payload;
  8134. args->bc_attrs.max_resp_sz = max_bc_payload;
  8135. args->bc_attrs.max_resp_sz_cached = 0;
  8136. args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
  8137. args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
  8138. if (args->bc_attrs.max_reqs > max_bc_slots)
  8139. args->bc_attrs.max_reqs = max_bc_slots;
  8140. dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
  8141. "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
  8142. __func__,
  8143. args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
  8144. args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
  8145. args->bc_attrs.max_reqs);
  8146. }
  8147. static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
  8148. struct nfs41_create_session_res *res)
  8149. {
  8150. struct nfs4_channel_attrs *sent = &args->fc_attrs;
  8151. struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
  8152. if (rcvd->max_resp_sz > sent->max_resp_sz)
  8153. return -EINVAL;
  8154. /*
  8155. * Our requested max_ops is the minimum we need; we're not
  8156. * prepared to break up compounds into smaller pieces than that.
  8157. * So, no point even trying to continue if the server won't
  8158. * cooperate:
  8159. */
  8160. if (rcvd->max_ops < sent->max_ops)
  8161. return -EINVAL;
  8162. if (rcvd->max_reqs == 0)
  8163. return -EINVAL;
  8164. if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
  8165. rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
  8166. return 0;
  8167. }
  8168. static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
  8169. struct nfs41_create_session_res *res)
  8170. {
  8171. struct nfs4_channel_attrs *sent = &args->bc_attrs;
  8172. struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
  8173. if (!(res->flags & SESSION4_BACK_CHAN))
  8174. goto out;
  8175. if (rcvd->max_rqst_sz > sent->max_rqst_sz)
  8176. return -EINVAL;
  8177. if (rcvd->max_resp_sz > sent->max_resp_sz)
  8178. return -EINVAL;
  8179. if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
  8180. return -EINVAL;
  8181. if (rcvd->max_ops > sent->max_ops)
  8182. return -EINVAL;
  8183. if (rcvd->max_reqs > sent->max_reqs)
  8184. return -EINVAL;
  8185. out:
  8186. return 0;
  8187. }
  8188. static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
  8189. struct nfs41_create_session_res *res)
  8190. {
  8191. int ret;
  8192. ret = nfs4_verify_fore_channel_attrs(args, res);
  8193. if (ret)
  8194. return ret;
  8195. return nfs4_verify_back_channel_attrs(args, res);
  8196. }
  8197. static void nfs4_update_session(struct nfs4_session *session,
  8198. struct nfs41_create_session_res *res)
  8199. {
  8200. nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
  8201. /* Mark client id and session as being confirmed */
  8202. session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
  8203. set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
  8204. session->flags = res->flags;
  8205. memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
  8206. if (res->flags & SESSION4_BACK_CHAN)
  8207. memcpy(&session->bc_attrs, &res->bc_attrs,
  8208. sizeof(session->bc_attrs));
  8209. }
  8210. static int _nfs4_proc_create_session(struct nfs_client *clp,
  8211. const struct cred *cred)
  8212. {
  8213. struct nfs4_session *session = clp->cl_session;
  8214. struct nfs41_create_session_args args = {
  8215. .client = clp,
  8216. .clientid = clp->cl_clientid,
  8217. .seqid = clp->cl_seqid,
  8218. .cb_program = NFS4_CALLBACK,
  8219. };
  8220. struct nfs41_create_session_res res;
  8221. struct rpc_message msg = {
  8222. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
  8223. .rpc_argp = &args,
  8224. .rpc_resp = &res,
  8225. .rpc_cred = cred,
  8226. };
  8227. int status;
  8228. nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
  8229. args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
  8230. status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
  8231. RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
  8232. trace_nfs4_create_session(clp, status);
  8233. switch (status) {
  8234. case -NFS4ERR_STALE_CLIENTID:
  8235. case -NFS4ERR_DELAY:
  8236. case -ETIMEDOUT:
  8237. case -EACCES:
  8238. case -EAGAIN:
  8239. goto out;
  8240. }
  8241. clp->cl_seqid++;
  8242. if (!status) {
  8243. /* Verify the session's negotiated channel_attrs values */
  8244. status = nfs4_verify_channel_attrs(&args, &res);
  8245. /* Increment the clientid slot sequence id */
  8246. if (status)
  8247. goto out;
  8248. nfs4_update_session(session, &res);
  8249. }
  8250. out:
  8251. return status;
  8252. }
  8253. /*
  8254. * Issues a CREATE_SESSION operation to the server.
  8255. * It is the responsibility of the caller to verify the session is
  8256. * expired before calling this routine.
  8257. */
  8258. int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
  8259. {
  8260. int status;
  8261. unsigned *ptr;
  8262. struct nfs4_session *session = clp->cl_session;
  8263. struct nfs4_add_xprt_data xprtdata = {
  8264. .clp = clp,
  8265. };
  8266. struct rpc_add_xprt_test rpcdata = {
  8267. .add_xprt_test = clp->cl_mvops->session_trunk,
  8268. .data = &xprtdata,
  8269. };
  8270. dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
  8271. status = _nfs4_proc_create_session(clp, cred);
  8272. if (status)
  8273. goto out;
  8274. /* Init or reset the session slot tables */
  8275. status = nfs4_setup_session_slot_tables(session);
  8276. dprintk("slot table setup returned %d\n", status);
  8277. if (status)
  8278. goto out;
  8279. ptr = (unsigned *)&session->sess_id.data[0];
  8280. dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
  8281. clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
  8282. rpc_clnt_probe_trunked_xprts(clp->cl_rpcclient, &rpcdata);
  8283. out:
  8284. return status;
  8285. }
  8286. /*
  8287. * Issue the over-the-wire RPC DESTROY_SESSION.
  8288. * The caller must serialize access to this routine.
  8289. */
  8290. int nfs4_proc_destroy_session(struct nfs4_session *session,
  8291. const struct cred *cred)
  8292. {
  8293. struct rpc_message msg = {
  8294. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
  8295. .rpc_argp = session,
  8296. .rpc_cred = cred,
  8297. };
  8298. int status = 0;
  8299. /* session is still being setup */
  8300. if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
  8301. return 0;
  8302. status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
  8303. RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
  8304. trace_nfs4_destroy_session(session->clp, status);
  8305. if (status)
  8306. dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
  8307. "Session has been destroyed regardless...\n", status);
  8308. rpc_clnt_manage_trunked_xprts(session->clp->cl_rpcclient);
  8309. return status;
  8310. }
  8311. /*
  8312. * Renew the cl_session lease.
  8313. */
  8314. struct nfs4_sequence_data {
  8315. struct nfs_client *clp;
  8316. struct nfs4_sequence_args args;
  8317. struct nfs4_sequence_res res;
  8318. };
  8319. static void nfs41_sequence_release(void *data)
  8320. {
  8321. struct nfs4_sequence_data *calldata = data;
  8322. struct nfs_client *clp = calldata->clp;
  8323. if (refcount_read(&clp->cl_count) > 1)
  8324. nfs4_schedule_state_renewal(clp);
  8325. nfs_put_client(clp);
  8326. kfree(calldata);
  8327. }
  8328. static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
  8329. {
  8330. switch(task->tk_status) {
  8331. case -NFS4ERR_DELAY:
  8332. rpc_delay(task, NFS4_POLL_RETRY_MAX);
  8333. return -EAGAIN;
  8334. default:
  8335. nfs4_schedule_lease_recovery(clp);
  8336. }
  8337. return 0;
  8338. }
  8339. static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
  8340. {
  8341. struct nfs4_sequence_data *calldata = data;
  8342. struct nfs_client *clp = calldata->clp;
  8343. if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
  8344. return;
  8345. trace_nfs4_sequence(clp, task->tk_status);
  8346. if (task->tk_status < 0 && clp->cl_cons_state >= 0) {
  8347. dprintk("%s ERROR %d\n", __func__, task->tk_status);
  8348. if (refcount_read(&clp->cl_count) == 1)
  8349. return;
  8350. if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
  8351. rpc_restart_call_prepare(task);
  8352. return;
  8353. }
  8354. }
  8355. dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
  8356. }
  8357. static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
  8358. {
  8359. struct nfs4_sequence_data *calldata = data;
  8360. struct nfs_client *clp = calldata->clp;
  8361. struct nfs4_sequence_args *args;
  8362. struct nfs4_sequence_res *res;
  8363. args = task->tk_msg.rpc_argp;
  8364. res = task->tk_msg.rpc_resp;
  8365. nfs4_setup_sequence(clp, args, res, task);
  8366. }
  8367. static const struct rpc_call_ops nfs41_sequence_ops = {
  8368. .rpc_call_done = nfs41_sequence_call_done,
  8369. .rpc_call_prepare = nfs41_sequence_prepare,
  8370. .rpc_release = nfs41_sequence_release,
  8371. };
  8372. static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
  8373. const struct cred *cred,
  8374. struct nfs4_slot *slot,
  8375. bool is_privileged)
  8376. {
  8377. struct nfs4_sequence_data *calldata;
  8378. struct rpc_message msg = {
  8379. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
  8380. .rpc_cred = cred,
  8381. };
  8382. struct rpc_task_setup task_setup_data = {
  8383. .rpc_client = clp->cl_rpcclient,
  8384. .rpc_message = &msg,
  8385. .callback_ops = &nfs41_sequence_ops,
  8386. .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT | RPC_TASK_MOVEABLE,
  8387. };
  8388. struct rpc_task *ret;
  8389. ret = ERR_PTR(-EIO);
  8390. if (!refcount_inc_not_zero(&clp->cl_count))
  8391. goto out_err;
  8392. ret = ERR_PTR(-ENOMEM);
  8393. calldata = kzalloc_obj(*calldata);
  8394. if (calldata == NULL)
  8395. goto out_put_clp;
  8396. nfs4_init_sequence(clp, &calldata->args, &calldata->res, 0, is_privileged);
  8397. nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
  8398. msg.rpc_argp = &calldata->args;
  8399. msg.rpc_resp = &calldata->res;
  8400. calldata->clp = clp;
  8401. task_setup_data.callback_data = calldata;
  8402. ret = rpc_run_task(&task_setup_data);
  8403. if (IS_ERR(ret))
  8404. goto out_err;
  8405. return ret;
  8406. out_put_clp:
  8407. nfs_put_client(clp);
  8408. out_err:
  8409. nfs41_release_slot(slot);
  8410. return ret;
  8411. }
  8412. static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
  8413. {
  8414. struct rpc_task *task;
  8415. int ret = 0;
  8416. if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
  8417. return -EAGAIN;
  8418. task = _nfs41_proc_sequence(clp, cred, NULL, false);
  8419. if (IS_ERR(task))
  8420. ret = PTR_ERR(task);
  8421. else
  8422. rpc_put_task_async(task);
  8423. dprintk("<-- %s status=%d\n", __func__, ret);
  8424. return ret;
  8425. }
  8426. static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
  8427. {
  8428. struct rpc_task *task;
  8429. int ret;
  8430. task = _nfs41_proc_sequence(clp, cred, NULL, true);
  8431. if (IS_ERR(task)) {
  8432. ret = PTR_ERR(task);
  8433. goto out;
  8434. }
  8435. ret = rpc_wait_for_completion_task(task);
  8436. if (!ret)
  8437. ret = task->tk_status;
  8438. rpc_put_task(task);
  8439. out:
  8440. dprintk("<-- %s status=%d\n", __func__, ret);
  8441. return ret;
  8442. }
  8443. struct nfs4_reclaim_complete_data {
  8444. struct nfs_client *clp;
  8445. struct nfs41_reclaim_complete_args arg;
  8446. struct nfs41_reclaim_complete_res res;
  8447. };
  8448. static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
  8449. {
  8450. struct nfs4_reclaim_complete_data *calldata = data;
  8451. nfs4_setup_sequence(calldata->clp,
  8452. &calldata->arg.seq_args,
  8453. &calldata->res.seq_res,
  8454. task);
  8455. }
  8456. static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
  8457. {
  8458. switch(task->tk_status) {
  8459. case 0:
  8460. wake_up_all(&clp->cl_lock_waitq);
  8461. fallthrough;
  8462. case -NFS4ERR_COMPLETE_ALREADY:
  8463. case -NFS4ERR_WRONG_CRED: /* What to do here? */
  8464. break;
  8465. case -NFS4ERR_DELAY:
  8466. rpc_delay(task, NFS4_POLL_RETRY_MAX);
  8467. fallthrough;
  8468. case -NFS4ERR_RETRY_UNCACHED_REP:
  8469. case -EACCES:
  8470. dprintk("%s: failed to reclaim complete error %d for server %s, retrying\n",
  8471. __func__, task->tk_status, clp->cl_hostname);
  8472. return -EAGAIN;
  8473. case -NFS4ERR_BADSESSION:
  8474. case -NFS4ERR_DEADSESSION:
  8475. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  8476. break;
  8477. default:
  8478. nfs4_schedule_lease_recovery(clp);
  8479. }
  8480. return 0;
  8481. }
  8482. static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
  8483. {
  8484. struct nfs4_reclaim_complete_data *calldata = data;
  8485. struct nfs_client *clp = calldata->clp;
  8486. struct nfs4_sequence_res *res = &calldata->res.seq_res;
  8487. if (!nfs41_sequence_done(task, res))
  8488. return;
  8489. trace_nfs4_reclaim_complete(clp, task->tk_status);
  8490. if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
  8491. rpc_restart_call_prepare(task);
  8492. return;
  8493. }
  8494. }
  8495. static void nfs4_free_reclaim_complete_data(void *data)
  8496. {
  8497. struct nfs4_reclaim_complete_data *calldata = data;
  8498. kfree(calldata);
  8499. }
  8500. static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
  8501. .rpc_call_prepare = nfs4_reclaim_complete_prepare,
  8502. .rpc_call_done = nfs4_reclaim_complete_done,
  8503. .rpc_release = nfs4_free_reclaim_complete_data,
  8504. };
  8505. /*
  8506. * Issue a global reclaim complete.
  8507. */
  8508. static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
  8509. const struct cred *cred)
  8510. {
  8511. struct nfs4_reclaim_complete_data *calldata;
  8512. struct rpc_message msg = {
  8513. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
  8514. .rpc_cred = cred,
  8515. };
  8516. struct rpc_task_setup task_setup_data = {
  8517. .rpc_client = clp->cl_rpcclient,
  8518. .rpc_message = &msg,
  8519. .callback_ops = &nfs4_reclaim_complete_call_ops,
  8520. .flags = RPC_TASK_NO_ROUND_ROBIN,
  8521. };
  8522. int status = -ENOMEM;
  8523. calldata = kzalloc_obj(*calldata, GFP_NOFS);
  8524. if (calldata == NULL)
  8525. goto out;
  8526. calldata->clp = clp;
  8527. calldata->arg.one_fs = 0;
  8528. nfs4_init_sequence(clp, &calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
  8529. msg.rpc_argp = &calldata->arg;
  8530. msg.rpc_resp = &calldata->res;
  8531. task_setup_data.callback_data = calldata;
  8532. status = nfs4_call_sync_custom(&task_setup_data);
  8533. out:
  8534. dprintk("<-- %s status=%d\n", __func__, status);
  8535. return status;
  8536. }
  8537. static void
  8538. nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
  8539. {
  8540. struct nfs4_layoutget *lgp = calldata;
  8541. struct nfs_server *server = NFS_SERVER(lgp->args.inode);
  8542. nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
  8543. &lgp->res.seq_res, task);
  8544. }
  8545. static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
  8546. {
  8547. struct nfs4_layoutget *lgp = calldata;
  8548. nfs41_sequence_process(task, &lgp->res.seq_res);
  8549. }
  8550. static int
  8551. nfs4_layoutget_handle_exception(struct rpc_task *task,
  8552. struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
  8553. {
  8554. struct inode *inode = lgp->args.inode;
  8555. struct nfs_server *server = NFS_SERVER(inode);
  8556. struct pnfs_layout_hdr *lo = lgp->lo;
  8557. int nfs4err = task->tk_status;
  8558. int err, status = 0;
  8559. LIST_HEAD(head);
  8560. dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
  8561. nfs4_sequence_free_slot(&lgp->res.seq_res);
  8562. exception->state = NULL;
  8563. exception->stateid = NULL;
  8564. switch (nfs4err) {
  8565. case 0:
  8566. goto out;
  8567. /*
  8568. * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
  8569. * on the file. set tk_status to -ENODATA to tell upper layer to
  8570. * retry go inband.
  8571. */
  8572. case -NFS4ERR_LAYOUTUNAVAILABLE:
  8573. status = -ENODATA;
  8574. goto out;
  8575. /*
  8576. * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
  8577. * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
  8578. */
  8579. case -NFS4ERR_BADLAYOUT:
  8580. status = -EOVERFLOW;
  8581. goto out;
  8582. /*
  8583. * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
  8584. * (or clients) writing to the same RAID stripe except when
  8585. * the minlength argument is 0 (see RFC5661 section 18.43.3).
  8586. *
  8587. * Treat it like we would RECALLCONFLICT -- we retry for a little
  8588. * while, and then eventually give up.
  8589. */
  8590. case -NFS4ERR_LAYOUTTRYLATER:
  8591. if (lgp->args.minlength == 0) {
  8592. status = -EOVERFLOW;
  8593. goto out;
  8594. }
  8595. status = -EBUSY;
  8596. break;
  8597. case -NFS4ERR_RECALLCONFLICT:
  8598. case -NFS4ERR_RETURNCONFLICT:
  8599. status = -ERECALLCONFLICT;
  8600. break;
  8601. case -NFS4ERR_DELEG_REVOKED:
  8602. case -NFS4ERR_ADMIN_REVOKED:
  8603. case -NFS4ERR_EXPIRED:
  8604. case -NFS4ERR_BAD_STATEID:
  8605. exception->timeout = 0;
  8606. spin_lock(&inode->i_lock);
  8607. /* If the open stateid was bad, then recover it. */
  8608. if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
  8609. !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
  8610. spin_unlock(&inode->i_lock);
  8611. exception->state = lgp->args.ctx->state;
  8612. exception->stateid = &lgp->args.stateid;
  8613. break;
  8614. }
  8615. /*
  8616. * Mark the bad layout state as invalid, then retry
  8617. */
  8618. pnfs_mark_layout_stateid_invalid(lo, &head);
  8619. spin_unlock(&inode->i_lock);
  8620. nfs_commit_inode(inode, 0);
  8621. pnfs_free_lseg_list(&head);
  8622. status = -EAGAIN;
  8623. goto out;
  8624. }
  8625. err = nfs4_handle_exception(server, nfs4err, exception);
  8626. if (!status) {
  8627. if (exception->retry)
  8628. status = -EAGAIN;
  8629. else
  8630. status = err;
  8631. }
  8632. out:
  8633. return status;
  8634. }
  8635. size_t max_response_pages(struct nfs_server *server)
  8636. {
  8637. u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
  8638. return nfs_page_array_len(0, max_resp_sz);
  8639. }
  8640. static void nfs4_layoutget_release(void *calldata)
  8641. {
  8642. struct nfs4_layoutget *lgp = calldata;
  8643. nfs4_sequence_free_slot(&lgp->res.seq_res);
  8644. pnfs_layoutget_free(lgp);
  8645. }
  8646. static const struct rpc_call_ops nfs4_layoutget_call_ops = {
  8647. .rpc_call_prepare = nfs4_layoutget_prepare,
  8648. .rpc_call_done = nfs4_layoutget_done,
  8649. .rpc_release = nfs4_layoutget_release,
  8650. };
  8651. struct pnfs_layout_segment *
  8652. nfs4_proc_layoutget(struct nfs4_layoutget *lgp,
  8653. struct nfs4_exception *exception)
  8654. {
  8655. struct inode *inode = lgp->args.inode;
  8656. struct nfs_server *server = NFS_SERVER(inode);
  8657. struct rpc_task *task;
  8658. struct rpc_message msg = {
  8659. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
  8660. .rpc_argp = &lgp->args,
  8661. .rpc_resp = &lgp->res,
  8662. .rpc_cred = lgp->cred,
  8663. };
  8664. struct rpc_task_setup task_setup_data = {
  8665. .rpc_client = server->client,
  8666. .rpc_message = &msg,
  8667. .callback_ops = &nfs4_layoutget_call_ops,
  8668. .callback_data = lgp,
  8669. .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF |
  8670. RPC_TASK_MOVEABLE,
  8671. };
  8672. struct pnfs_layout_segment *lseg = NULL;
  8673. int status = 0;
  8674. nfs4_init_sequence(server->nfs_client, &lgp->args.seq_args,
  8675. &lgp->res.seq_res, 0, 0);
  8676. exception->retry = 0;
  8677. task = rpc_run_task(&task_setup_data);
  8678. if (IS_ERR(task))
  8679. return ERR_CAST(task);
  8680. status = rpc_wait_for_completion_task(task);
  8681. if (status != 0)
  8682. goto out;
  8683. if (task->tk_status < 0) {
  8684. exception->retry = 1;
  8685. status = nfs4_layoutget_handle_exception(task, lgp, exception);
  8686. } else if (lgp->res.layoutp->len == 0) {
  8687. exception->retry = 1;
  8688. status = -EAGAIN;
  8689. nfs4_update_delay(&exception->timeout);
  8690. } else
  8691. lseg = pnfs_layout_process(lgp);
  8692. out:
  8693. trace_nfs4_layoutget(lgp->args.ctx,
  8694. &lgp->args.range,
  8695. &lgp->res.range,
  8696. &lgp->res.stateid,
  8697. status);
  8698. rpc_put_task(task);
  8699. dprintk("<-- %s status=%d\n", __func__, status);
  8700. if (status)
  8701. return ERR_PTR(status);
  8702. return lseg;
  8703. }
  8704. static void
  8705. nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
  8706. {
  8707. struct nfs4_layoutreturn *lrp = calldata;
  8708. nfs4_setup_sequence(lrp->clp,
  8709. &lrp->args.seq_args,
  8710. &lrp->res.seq_res,
  8711. task);
  8712. if (!pnfs_layout_is_valid(lrp->args.layout))
  8713. rpc_exit(task, 0);
  8714. }
  8715. static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
  8716. {
  8717. struct nfs4_layoutreturn *lrp = calldata;
  8718. struct nfs_server *server;
  8719. if (!nfs41_sequence_process(task, &lrp->res.seq_res))
  8720. return;
  8721. if (task->tk_rpc_status == -ETIMEDOUT) {
  8722. lrp->rpc_status = -EAGAIN;
  8723. lrp->res.lrs_present = 0;
  8724. return;
  8725. }
  8726. /*
  8727. * Was there an RPC level error? Assume the call succeeded,
  8728. * and that we need to release the layout
  8729. */
  8730. if (task->tk_rpc_status != 0 && RPC_WAS_SENT(task)) {
  8731. lrp->res.lrs_present = 0;
  8732. return;
  8733. }
  8734. server = NFS_SERVER(lrp->args.inode);
  8735. switch (task->tk_status) {
  8736. case -NFS4ERR_OLD_STATEID:
  8737. if (nfs4_layout_refresh_old_stateid(&lrp->args.stateid,
  8738. &lrp->args.range,
  8739. lrp->args.inode))
  8740. goto out_restart;
  8741. fallthrough;
  8742. default:
  8743. task->tk_status = 0;
  8744. lrp->res.lrs_present = 0;
  8745. fallthrough;
  8746. case 0:
  8747. break;
  8748. case -NFS4ERR_BADSESSION:
  8749. case -NFS4ERR_DEADSESSION:
  8750. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  8751. nfs4_schedule_session_recovery(server->nfs_client->cl_session,
  8752. task->tk_status);
  8753. lrp->res.lrs_present = 0;
  8754. lrp->rpc_status = -EAGAIN;
  8755. task->tk_status = 0;
  8756. break;
  8757. case -NFS4ERR_DELAY:
  8758. if (nfs4_async_handle_error(task, server, NULL, NULL) ==
  8759. -EAGAIN)
  8760. goto out_restart;
  8761. lrp->res.lrs_present = 0;
  8762. break;
  8763. }
  8764. return;
  8765. out_restart:
  8766. task->tk_status = 0;
  8767. nfs4_sequence_free_slot(&lrp->res.seq_res);
  8768. rpc_restart_call_prepare(task);
  8769. }
  8770. static void nfs4_layoutreturn_release(void *calldata)
  8771. {
  8772. struct nfs4_layoutreturn *lrp = calldata;
  8773. struct pnfs_layout_hdr *lo = lrp->args.layout;
  8774. if (lrp->rpc_status == 0 || !lrp->inode)
  8775. pnfs_layoutreturn_free_lsegs(
  8776. lo, &lrp->args.stateid, &lrp->args.range,
  8777. lrp->res.lrs_present ? &lrp->res.stateid : NULL);
  8778. else
  8779. pnfs_layoutreturn_retry_later(lo, &lrp->args.stateid,
  8780. &lrp->args.range);
  8781. nfs4_sequence_free_slot(&lrp->res.seq_res);
  8782. if (lrp->ld_private.ops && lrp->ld_private.ops->free)
  8783. lrp->ld_private.ops->free(&lrp->ld_private);
  8784. pnfs_put_layout_hdr(lrp->args.layout);
  8785. nfs_iput_and_deactive(lrp->inode);
  8786. put_cred(lrp->cred);
  8787. kfree(calldata);
  8788. }
  8789. static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
  8790. .rpc_call_prepare = nfs4_layoutreturn_prepare,
  8791. .rpc_call_done = nfs4_layoutreturn_done,
  8792. .rpc_release = nfs4_layoutreturn_release,
  8793. };
  8794. int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, unsigned int flags)
  8795. {
  8796. struct nfs_client *clp = NFS_SERVER(lrp->args.inode)->nfs_client;
  8797. struct rpc_task *task;
  8798. struct rpc_message msg = {
  8799. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
  8800. .rpc_argp = &lrp->args,
  8801. .rpc_resp = &lrp->res,
  8802. .rpc_cred = lrp->cred,
  8803. };
  8804. struct rpc_task_setup task_setup_data = {
  8805. .rpc_client = NFS_SERVER(lrp->args.inode)->client,
  8806. .rpc_message = &msg,
  8807. .callback_ops = &nfs4_layoutreturn_call_ops,
  8808. .callback_data = lrp,
  8809. .flags = RPC_TASK_MOVEABLE,
  8810. };
  8811. int status = 0;
  8812. nfs4_state_protect(clp, NFS_SP4_MACH_CRED_PNFS_CLEANUP,
  8813. &task_setup_data.rpc_client, &msg);
  8814. lrp->inode = nfs_igrab_and_active(lrp->args.inode);
  8815. if (flags & PNFS_FL_LAYOUTRETURN_ASYNC) {
  8816. if (!lrp->inode) {
  8817. nfs4_layoutreturn_release(lrp);
  8818. return -EAGAIN;
  8819. }
  8820. task_setup_data.flags |= RPC_TASK_ASYNC;
  8821. }
  8822. if (!lrp->inode)
  8823. flags |= PNFS_FL_LAYOUTRETURN_PRIVILEGED;
  8824. nfs4_init_sequence(clp, &lrp->args.seq_args, &lrp->res.seq_res, 1,
  8825. flags & PNFS_FL_LAYOUTRETURN_PRIVILEGED ? 1 : 0);
  8826. task = rpc_run_task(&task_setup_data);
  8827. if (IS_ERR(task))
  8828. return PTR_ERR(task);
  8829. if (!(flags & PNFS_FL_LAYOUTRETURN_ASYNC))
  8830. status = task->tk_status;
  8831. trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
  8832. dprintk("<-- %s status=%d\n", __func__, status);
  8833. rpc_put_task(task);
  8834. return status;
  8835. }
  8836. static int
  8837. _nfs4_proc_getdeviceinfo(struct nfs_server *server,
  8838. struct pnfs_device *pdev,
  8839. const struct cred *cred)
  8840. {
  8841. struct nfs4_getdeviceinfo_args args = {
  8842. .pdev = pdev,
  8843. .notify_types = NOTIFY_DEVICEID4_CHANGE |
  8844. NOTIFY_DEVICEID4_DELETE,
  8845. };
  8846. struct nfs4_getdeviceinfo_res res = {
  8847. .pdev = pdev,
  8848. };
  8849. struct rpc_message msg = {
  8850. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
  8851. .rpc_argp = &args,
  8852. .rpc_resp = &res,
  8853. .rpc_cred = cred,
  8854. };
  8855. int status;
  8856. status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  8857. if (res.notification & ~args.notify_types)
  8858. dprintk("%s: unsupported notification\n", __func__);
  8859. if (res.notification != args.notify_types)
  8860. pdev->nocache = 1;
  8861. trace_nfs4_getdeviceinfo(server, &pdev->dev_id, status);
  8862. dprintk("<-- %s status=%d\n", __func__, status);
  8863. return status;
  8864. }
  8865. int nfs4_proc_getdeviceinfo(struct nfs_server *server,
  8866. struct pnfs_device *pdev,
  8867. const struct cred *cred)
  8868. {
  8869. struct nfs4_exception exception = { };
  8870. int err;
  8871. do {
  8872. err = nfs4_handle_exception(server,
  8873. _nfs4_proc_getdeviceinfo(server, pdev, cred),
  8874. &exception);
  8875. } while (exception.retry);
  8876. return err;
  8877. }
  8878. EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
  8879. static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
  8880. {
  8881. struct nfs4_layoutcommit_data *data = calldata;
  8882. struct nfs_server *server = NFS_SERVER(data->args.inode);
  8883. nfs4_setup_sequence(server->nfs_client,
  8884. &data->args.seq_args,
  8885. &data->res.seq_res,
  8886. task);
  8887. }
  8888. static void
  8889. nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
  8890. {
  8891. struct nfs4_layoutcommit_data *data = calldata;
  8892. struct nfs_server *server = NFS_SERVER(data->args.inode);
  8893. if (!nfs41_sequence_done(task, &data->res.seq_res))
  8894. return;
  8895. switch (task->tk_status) { /* Just ignore these failures */
  8896. case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
  8897. case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
  8898. case -NFS4ERR_BADLAYOUT: /* no layout */
  8899. case -NFS4ERR_GRACE: /* loca_recalim always false */
  8900. task->tk_status = 0;
  8901. break;
  8902. case 0:
  8903. break;
  8904. default:
  8905. if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
  8906. rpc_restart_call_prepare(task);
  8907. return;
  8908. }
  8909. }
  8910. }
  8911. static void nfs4_layoutcommit_release(void *calldata)
  8912. {
  8913. struct nfs4_layoutcommit_data *data = calldata;
  8914. pnfs_cleanup_layoutcommit(data);
  8915. nfs_post_op_update_inode_force_wcc(data->args.inode,
  8916. data->res.fattr);
  8917. put_cred(data->cred);
  8918. nfs_iput_and_deactive(data->inode);
  8919. kfree(data);
  8920. }
  8921. static const struct rpc_call_ops nfs4_layoutcommit_ops = {
  8922. .rpc_call_prepare = nfs4_layoutcommit_prepare,
  8923. .rpc_call_done = nfs4_layoutcommit_done,
  8924. .rpc_release = nfs4_layoutcommit_release,
  8925. };
  8926. int
  8927. nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
  8928. {
  8929. struct rpc_message msg = {
  8930. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
  8931. .rpc_argp = &data->args,
  8932. .rpc_resp = &data->res,
  8933. .rpc_cred = data->cred,
  8934. };
  8935. struct rpc_task_setup task_setup_data = {
  8936. .task = &data->task,
  8937. .rpc_client = NFS_CLIENT(data->args.inode),
  8938. .rpc_message = &msg,
  8939. .callback_ops = &nfs4_layoutcommit_ops,
  8940. .callback_data = data,
  8941. .flags = RPC_TASK_MOVEABLE,
  8942. };
  8943. struct rpc_task *task;
  8944. int status = 0;
  8945. dprintk("NFS: initiating layoutcommit call. sync %d "
  8946. "lbw: %llu inode %lu\n", sync,
  8947. data->args.lastbytewritten,
  8948. data->args.inode->i_ino);
  8949. if (!sync) {
  8950. data->inode = nfs_igrab_and_active(data->args.inode);
  8951. if (data->inode == NULL) {
  8952. nfs4_layoutcommit_release(data);
  8953. return -EAGAIN;
  8954. }
  8955. task_setup_data.flags = RPC_TASK_ASYNC;
  8956. }
  8957. nfs4_init_sequence(NFS_SERVER(data->args.inode)->nfs_client,
  8958. &data->args.seq_args, &data->res.seq_res, 1, 0);
  8959. task = rpc_run_task(&task_setup_data);
  8960. if (IS_ERR(task))
  8961. return PTR_ERR(task);
  8962. if (sync)
  8963. status = task->tk_status;
  8964. trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
  8965. dprintk("%s: status %d\n", __func__, status);
  8966. rpc_put_task(task);
  8967. return status;
  8968. }
  8969. /*
  8970. * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
  8971. * possible) as per RFC3530bis and RFC5661 Security Considerations sections
  8972. */
  8973. static int _nfs41_proc_secinfo_no_name(struct nfs_server *server,
  8974. struct nfs_fh *fhandle,
  8975. struct nfs4_secinfo_flavors *flavors,
  8976. bool use_integrity)
  8977. {
  8978. struct nfs_client *clp = server->nfs_client;
  8979. struct nfs41_secinfo_no_name_args args = {
  8980. .style = SECINFO_STYLE_CURRENT_FH,
  8981. };
  8982. struct nfs4_secinfo_res res = {
  8983. .flavors = flavors,
  8984. };
  8985. struct rpc_message msg = {
  8986. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
  8987. .rpc_argp = &args,
  8988. .rpc_resp = &res,
  8989. };
  8990. struct nfs4_call_sync_data data = {
  8991. .seq_server = server,
  8992. .seq_args = &args.seq_args,
  8993. .seq_res = &res.seq_res,
  8994. };
  8995. struct rpc_task_setup task_setup = {
  8996. .rpc_client = server->client,
  8997. .rpc_message = &msg,
  8998. .callback_ops = clp->cl_mvops->call_sync_ops,
  8999. .callback_data = &data,
  9000. .flags = RPC_TASK_NO_ROUND_ROBIN,
  9001. };
  9002. const struct cred *cred = NULL;
  9003. int status;
  9004. if (use_integrity) {
  9005. task_setup.rpc_client = clp->cl_rpcclient;
  9006. cred = nfs4_get_clid_cred(clp);
  9007. msg.rpc_cred = cred;
  9008. }
  9009. nfs4_init_sequence(clp, &args.seq_args, &res.seq_res, 0, 0);
  9010. status = nfs4_call_sync_custom(&task_setup);
  9011. dprintk("<-- %s status=%d\n", __func__, status);
  9012. put_cred(cred);
  9013. return status;
  9014. }
  9015. static int nfs41_proc_secinfo_no_name(struct nfs_server *server,
  9016. struct nfs_fh *fhandle,
  9017. struct nfs4_secinfo_flavors *flavors)
  9018. {
  9019. struct nfs4_exception exception = {
  9020. .interruptible = true,
  9021. };
  9022. int err;
  9023. do {
  9024. /* first try using integrity protection */
  9025. err = -NFS4ERR_WRONGSEC;
  9026. /* try to use integrity protection with machine cred */
  9027. if (_nfs4_is_integrity_protected(server->nfs_client))
  9028. err = _nfs41_proc_secinfo_no_name(server, fhandle,
  9029. flavors, true);
  9030. /*
  9031. * if unable to use integrity protection, or SECINFO with
  9032. * integrity protection returns NFS4ERR_WRONGSEC (which is
  9033. * disallowed by spec, but exists in deployed servers) use
  9034. * the current filesystem's rpc_client and the user cred.
  9035. */
  9036. if (err == -NFS4ERR_WRONGSEC)
  9037. err = _nfs41_proc_secinfo_no_name(server, fhandle,
  9038. flavors, false);
  9039. switch (err) {
  9040. case 0:
  9041. case -NFS4ERR_WRONGSEC:
  9042. case -ENOTSUPP:
  9043. goto out;
  9044. default:
  9045. err = nfs4_handle_exception(server, err, &exception);
  9046. }
  9047. } while (exception.retry);
  9048. out:
  9049. return err;
  9050. }
  9051. static int nfs41_find_root_sec(struct nfs_server *server,
  9052. struct nfs_fh *fhandle, struct nfs_fattr *fattr)
  9053. {
  9054. int err;
  9055. struct page *page;
  9056. rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
  9057. struct nfs4_secinfo_flavors *flavors;
  9058. struct nfs4_secinfo4 *secinfo;
  9059. int i;
  9060. page = alloc_page(GFP_KERNEL);
  9061. if (!page) {
  9062. err = -ENOMEM;
  9063. goto out;
  9064. }
  9065. flavors = page_address(page);
  9066. err = nfs41_proc_secinfo_no_name(server, fhandle, flavors);
  9067. /*
  9068. * Fall back on "guess and check" method if
  9069. * the server doesn't support SECINFO_NO_NAME
  9070. */
  9071. if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
  9072. err = nfs4_find_root_sec(server, fhandle, fattr);
  9073. goto out_freepage;
  9074. }
  9075. if (err)
  9076. goto out_freepage;
  9077. for (i = 0; i < flavors->num_flavors; i++) {
  9078. secinfo = &flavors->flavors[i];
  9079. switch (secinfo->flavor) {
  9080. case RPC_AUTH_NULL:
  9081. case RPC_AUTH_UNIX:
  9082. case RPC_AUTH_GSS:
  9083. flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
  9084. &secinfo->flavor_info);
  9085. break;
  9086. default:
  9087. flavor = RPC_AUTH_MAXFLAVOR;
  9088. break;
  9089. }
  9090. if (!nfs_auth_info_match(&server->auth_info, flavor))
  9091. flavor = RPC_AUTH_MAXFLAVOR;
  9092. if (flavor != RPC_AUTH_MAXFLAVOR) {
  9093. err = nfs4_lookup_root_sec(server, fhandle, fattr,
  9094. flavor);
  9095. if (!err)
  9096. break;
  9097. }
  9098. }
  9099. if (flavor == RPC_AUTH_MAXFLAVOR)
  9100. err = -EPERM;
  9101. out_freepage:
  9102. put_page(page);
  9103. if (err == -EACCES)
  9104. return -EPERM;
  9105. out:
  9106. return err;
  9107. }
  9108. static int _nfs41_test_stateid(struct nfs_server *server,
  9109. const nfs4_stateid *stateid,
  9110. const struct cred *cred)
  9111. {
  9112. int status;
  9113. struct nfs41_test_stateid_args args = {
  9114. .stateid = *stateid,
  9115. };
  9116. struct nfs41_test_stateid_res res;
  9117. struct rpc_message msg = {
  9118. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
  9119. .rpc_argp = &args,
  9120. .rpc_resp = &res,
  9121. .rpc_cred = cred,
  9122. };
  9123. struct rpc_clnt *rpc_client = server->client;
  9124. struct nfs_client *clp = server->nfs_client;
  9125. nfs4_state_protect(clp, NFS_SP4_MACH_CRED_STATEID, &rpc_client, &msg);
  9126. dprintk("NFS call test_stateid %p\n", stateid);
  9127. nfs4_init_sequence(clp, &args.seq_args, &res.seq_res, 0, 1);
  9128. status = nfs4_call_sync_sequence(rpc_client, server, &msg,
  9129. &args.seq_args, &res.seq_res);
  9130. if (status != NFS_OK) {
  9131. dprintk("NFS reply test_stateid: failed, %d\n", status);
  9132. return status;
  9133. }
  9134. dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
  9135. return -res.status;
  9136. }
  9137. static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
  9138. int err, struct nfs4_exception *exception)
  9139. {
  9140. exception->retry = 0;
  9141. switch(err) {
  9142. case -NFS4ERR_DELAY:
  9143. case -NFS4ERR_RETRY_UNCACHED_REP:
  9144. nfs4_handle_exception(server, err, exception);
  9145. break;
  9146. case -NFS4ERR_BADSESSION:
  9147. case -NFS4ERR_BADSLOT:
  9148. case -NFS4ERR_BAD_HIGH_SLOT:
  9149. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  9150. case -NFS4ERR_DEADSESSION:
  9151. nfs4_do_handle_exception(server, err, exception);
  9152. }
  9153. }
  9154. /**
  9155. * nfs41_test_stateid - perform a TEST_STATEID operation
  9156. *
  9157. * @server: server / transport on which to perform the operation
  9158. * @stateid: state ID to test
  9159. * @cred: credential
  9160. *
  9161. * Returns NFS_OK if the server recognizes that "stateid" is valid.
  9162. * Otherwise a negative NFS4ERR value is returned if the operation
  9163. * failed or the state ID is not currently valid.
  9164. */
  9165. static int nfs41_test_stateid(struct nfs_server *server,
  9166. const nfs4_stateid *stateid,
  9167. const struct cred *cred)
  9168. {
  9169. struct nfs4_exception exception = {
  9170. .interruptible = true,
  9171. };
  9172. int err;
  9173. do {
  9174. err = _nfs41_test_stateid(server, stateid, cred);
  9175. nfs4_handle_delay_or_session_error(server, err, &exception);
  9176. } while (exception.retry);
  9177. return err;
  9178. }
  9179. struct nfs_free_stateid_data {
  9180. struct nfs_server *server;
  9181. struct nfs41_free_stateid_args args;
  9182. struct nfs41_free_stateid_res res;
  9183. };
  9184. static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
  9185. {
  9186. struct nfs_free_stateid_data *data = calldata;
  9187. nfs4_setup_sequence(data->server->nfs_client,
  9188. &data->args.seq_args,
  9189. &data->res.seq_res,
  9190. task);
  9191. }
  9192. static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
  9193. {
  9194. struct nfs_free_stateid_data *data = calldata;
  9195. nfs41_sequence_done(task, &data->res.seq_res);
  9196. switch (task->tk_status) {
  9197. case -NFS4ERR_DELAY:
  9198. if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
  9199. rpc_restart_call_prepare(task);
  9200. }
  9201. }
  9202. static void nfs41_free_stateid_release(void *calldata)
  9203. {
  9204. struct nfs_free_stateid_data *data = calldata;
  9205. struct nfs_client *clp = data->server->nfs_client;
  9206. nfs_put_client(clp);
  9207. kfree(calldata);
  9208. }
  9209. static const struct rpc_call_ops nfs41_free_stateid_ops = {
  9210. .rpc_call_prepare = nfs41_free_stateid_prepare,
  9211. .rpc_call_done = nfs41_free_stateid_done,
  9212. .rpc_release = nfs41_free_stateid_release,
  9213. };
  9214. /**
  9215. * nfs41_free_stateid - perform a FREE_STATEID operation
  9216. *
  9217. * @server: server / transport on which to perform the operation
  9218. * @stateid: state ID to release
  9219. * @cred: credential
  9220. * @privileged: set to true if this call needs to be privileged
  9221. *
  9222. * Note: this function is always asynchronous.
  9223. */
  9224. static int nfs41_free_stateid(struct nfs_server *server,
  9225. nfs4_stateid *stateid,
  9226. const struct cred *cred,
  9227. bool privileged)
  9228. {
  9229. struct rpc_message msg = {
  9230. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
  9231. .rpc_cred = cred,
  9232. };
  9233. struct rpc_task_setup task_setup = {
  9234. .rpc_client = server->client,
  9235. .rpc_message = &msg,
  9236. .callback_ops = &nfs41_free_stateid_ops,
  9237. .flags = RPC_TASK_ASYNC | RPC_TASK_MOVEABLE,
  9238. };
  9239. struct nfs_free_stateid_data *data;
  9240. struct rpc_task *task;
  9241. struct nfs_client *clp = server->nfs_client;
  9242. if (!refcount_inc_not_zero(&clp->cl_count))
  9243. return -EIO;
  9244. nfs4_state_protect(clp, NFS_SP4_MACH_CRED_STATEID,
  9245. &task_setup.rpc_client, &msg);
  9246. dprintk("NFS call free_stateid %p\n", stateid);
  9247. data = kmalloc_obj(*data);
  9248. if (!data)
  9249. return -ENOMEM;
  9250. data->server = server;
  9251. nfs4_stateid_copy(&data->args.stateid, stateid);
  9252. task_setup.callback_data = data;
  9253. msg.rpc_argp = &data->args;
  9254. msg.rpc_resp = &data->res;
  9255. nfs4_init_sequence(clp, &data->args.seq_args, &data->res.seq_res, 1,
  9256. privileged);
  9257. task = rpc_run_task(&task_setup);
  9258. if (IS_ERR(task))
  9259. return PTR_ERR(task);
  9260. rpc_put_task(task);
  9261. stateid->type = NFS4_FREED_STATEID_TYPE;
  9262. return 0;
  9263. }
  9264. static void
  9265. nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
  9266. {
  9267. const struct cred *cred = lsp->ls_state->owner->so_cred;
  9268. nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
  9269. nfs4_free_lock_state(server, lsp);
  9270. }
  9271. static bool nfs41_match_stateid(const nfs4_stateid *s1,
  9272. const nfs4_stateid *s2)
  9273. {
  9274. trace_nfs41_match_stateid(s1, s2);
  9275. if (s1->type != s2->type)
  9276. return false;
  9277. if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
  9278. return false;
  9279. if (s1->seqid == s2->seqid)
  9280. return true;
  9281. return s1->seqid == 0 || s2->seqid == 0;
  9282. }
  9283. bool nfs4_match_stateid(const nfs4_stateid *s1,
  9284. const nfs4_stateid *s2)
  9285. {
  9286. trace_nfs4_match_stateid(s1, s2);
  9287. return nfs4_stateid_match(s1, s2);
  9288. }
  9289. static const struct nfs4_sequence_slot_ops nfs41_sequence_slot_ops = {
  9290. .process = nfs41_sequence_process,
  9291. .done = nfs41_sequence_done,
  9292. .free_slot = nfs41_sequence_free_slot,
  9293. };
  9294. static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
  9295. .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
  9296. .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
  9297. .recover_open = nfs4_open_reclaim,
  9298. .recover_lock = nfs4_lock_reclaim,
  9299. .establish_clid = nfs41_init_clientid,
  9300. .reclaim_complete = nfs41_proc_reclaim_complete,
  9301. .detect_trunking = nfs41_discover_server_trunking,
  9302. };
  9303. static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
  9304. .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
  9305. .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
  9306. .recover_open = nfs41_open_expired,
  9307. .recover_lock = nfs41_lock_expired,
  9308. .establish_clid = nfs41_init_clientid,
  9309. };
  9310. static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
  9311. .sched_state_renewal = nfs41_proc_async_sequence,
  9312. .get_state_renewal_cred = nfs4_get_machine_cred,
  9313. .renew_lease = nfs4_proc_sequence,
  9314. };
  9315. static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
  9316. .get_locations = _nfs41_proc_get_locations,
  9317. .fsid_present = _nfs41_proc_fsid_present,
  9318. };
  9319. static struct nfs_seqid *
  9320. nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
  9321. {
  9322. return NULL;
  9323. }
  9324. static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
  9325. .minor_version = 1,
  9326. .init_caps = NFS_CAP_READDIRPLUS
  9327. | NFS_CAP_ATOMIC_OPEN
  9328. | NFS_CAP_DIR_DELEG
  9329. | NFS_CAP_POSIX_LOCK
  9330. | NFS_CAP_STATEID_NFSV41
  9331. | NFS_CAP_ATOMIC_OPEN_V1
  9332. | NFS_CAP_LGOPEN
  9333. | NFS_CAP_MOVEABLE,
  9334. .init_client = nfs41_init_client,
  9335. .shutdown_client = nfs41_shutdown_client,
  9336. .match_stateid = nfs41_match_stateid,
  9337. .find_root_sec = nfs41_find_root_sec,
  9338. .free_lock_state = nfs41_free_lock_state,
  9339. .test_and_free_expired = nfs41_test_and_free_expired_stateid,
  9340. .alloc_seqid = nfs_alloc_no_seqid,
  9341. .session_trunk = nfs4_test_session_trunk,
  9342. .call_sync_ops = &nfs41_call_sync_ops,
  9343. .sequence_slot_ops = &nfs41_sequence_slot_ops,
  9344. .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
  9345. .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
  9346. .state_renewal_ops = &nfs41_state_renewal_ops,
  9347. .mig_recovery_ops = &nfs41_mig_recovery_ops,
  9348. };
  9349. #if defined(CONFIG_NFS_V4_2)
  9350. static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
  9351. .minor_version = 2,
  9352. .init_caps = NFS_CAP_READDIRPLUS
  9353. | NFS_CAP_ATOMIC_OPEN
  9354. | NFS_CAP_DIR_DELEG
  9355. | NFS_CAP_POSIX_LOCK
  9356. | NFS_CAP_STATEID_NFSV41
  9357. | NFS_CAP_ATOMIC_OPEN_V1
  9358. | NFS_CAP_LGOPEN
  9359. | NFS_CAP_ALLOCATE
  9360. | NFS_CAP_COPY
  9361. | NFS_CAP_OFFLOAD_CANCEL
  9362. | NFS_CAP_COPY_NOTIFY
  9363. | NFS_CAP_DEALLOCATE
  9364. | NFS_CAP_ZERO_RANGE
  9365. | NFS_CAP_SEEK
  9366. | NFS_CAP_LAYOUTSTATS
  9367. | NFS_CAP_CLONE
  9368. | NFS_CAP_LAYOUTERROR
  9369. | NFS_CAP_READ_PLUS
  9370. | NFS_CAP_MOVEABLE
  9371. | NFS_CAP_OFFLOAD_STATUS,
  9372. .init_client = nfs41_init_client,
  9373. .shutdown_client = nfs41_shutdown_client,
  9374. .match_stateid = nfs41_match_stateid,
  9375. .find_root_sec = nfs41_find_root_sec,
  9376. .free_lock_state = nfs41_free_lock_state,
  9377. .call_sync_ops = &nfs41_call_sync_ops,
  9378. .sequence_slot_ops = &nfs41_sequence_slot_ops,
  9379. .test_and_free_expired = nfs41_test_and_free_expired_stateid,
  9380. .alloc_seqid = nfs_alloc_no_seqid,
  9381. .session_trunk = nfs4_test_session_trunk,
  9382. .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
  9383. .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
  9384. .state_renewal_ops = &nfs41_state_renewal_ops,
  9385. .mig_recovery_ops = &nfs41_mig_recovery_ops,
  9386. };
  9387. #endif
  9388. const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
  9389. #if defined(CONFIG_NFS_V4_0)
  9390. [0] = &nfs_v4_0_minor_ops,
  9391. #endif /* CONFIG_NFS_V4_0 */
  9392. [1] = &nfs_v4_1_minor_ops,
  9393. #if defined(CONFIG_NFS_V4_2)
  9394. [2] = &nfs_v4_2_minor_ops,
  9395. #endif
  9396. };
  9397. static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
  9398. {
  9399. ssize_t error, error2, error3;
  9400. size_t left = size;
  9401. error = generic_listxattr(dentry, list, left);
  9402. if (error < 0)
  9403. return error;
  9404. if (list) {
  9405. list += error;
  9406. left -= error;
  9407. }
  9408. error2 = security_inode_listsecurity(d_inode(dentry), list, left);
  9409. if (error2 < 0)
  9410. return error2;
  9411. if (list) {
  9412. list += error2;
  9413. left -= error2;
  9414. }
  9415. error3 = nfs4_listxattr_nfs4_user(d_inode(dentry), list, left);
  9416. if (error3 < 0)
  9417. return error3;
  9418. error += error2 + error3;
  9419. if (size && error > size)
  9420. return -ERANGE;
  9421. return error;
  9422. }
  9423. static void nfs4_enable_swap(struct inode *inode)
  9424. {
  9425. /* The state manager thread must always be running.
  9426. * It will notice the client is a swapper, and stay put.
  9427. */
  9428. struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
  9429. nfs4_schedule_state_manager(clp);
  9430. }
  9431. static void nfs4_disable_swap(struct inode *inode)
  9432. {
  9433. /* The state manager thread will now exit once it is
  9434. * woken.
  9435. */
  9436. struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
  9437. set_bit(NFS4CLNT_RUN_MANAGER, &clp->cl_state);
  9438. clear_bit(NFS4CLNT_MANAGER_AVAILABLE, &clp->cl_state);
  9439. wake_up_var(&clp->cl_state);
  9440. }
  9441. static const struct inode_operations nfs4_dir_inode_operations = {
  9442. .create = nfs_create,
  9443. .lookup = nfs_lookup,
  9444. .atomic_open = nfs_atomic_open,
  9445. .link = nfs_link,
  9446. .unlink = nfs_unlink,
  9447. .symlink = nfs_symlink,
  9448. .mkdir = nfs_mkdir,
  9449. .rmdir = nfs_rmdir,
  9450. .mknod = nfs_mknod,
  9451. .rename = nfs_rename,
  9452. .permission = nfs_permission,
  9453. .getattr = nfs_getattr,
  9454. .setattr = nfs_setattr,
  9455. .listxattr = nfs4_listxattr,
  9456. };
  9457. static const struct inode_operations nfs4_file_inode_operations = {
  9458. .permission = nfs_permission,
  9459. .getattr = nfs_getattr,
  9460. .setattr = nfs_setattr,
  9461. .listxattr = nfs4_listxattr,
  9462. };
  9463. static struct nfs_server *nfs4_clone_server(struct nfs_server *source,
  9464. struct nfs_fh *fh, struct nfs_fattr *fattr,
  9465. rpc_authflavor_t flavor)
  9466. {
  9467. struct nfs_server *server;
  9468. int error;
  9469. server = nfs_clone_server(source, fh, fattr, flavor);
  9470. if (IS_ERR(server))
  9471. return server;
  9472. error = nfs4_delegation_hash_alloc(server);
  9473. if (error) {
  9474. nfs_free_server(server);
  9475. return ERR_PTR(error);
  9476. }
  9477. return server;
  9478. }
  9479. const struct nfs_rpc_ops nfs_v4_clientops = {
  9480. .version = 4, /* protocol version */
  9481. .dentry_ops = &nfs4_dentry_operations,
  9482. .dir_inode_ops = &nfs4_dir_inode_operations,
  9483. .file_inode_ops = &nfs4_file_inode_operations,
  9484. .file_ops = &nfs4_file_operations,
  9485. .getroot = nfs4_proc_get_root,
  9486. .submount = nfs4_submount,
  9487. .try_get_tree = nfs4_try_get_tree,
  9488. .getattr = nfs4_proc_getattr,
  9489. .setattr = nfs4_proc_setattr,
  9490. .lookup = nfs4_proc_lookup,
  9491. .lookupp = nfs4_proc_lookupp,
  9492. .access = nfs4_proc_access,
  9493. .readlink = nfs4_proc_readlink,
  9494. .create = nfs4_proc_create,
  9495. .remove = nfs4_proc_remove,
  9496. .unlink_setup = nfs4_proc_unlink_setup,
  9497. .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
  9498. .unlink_done = nfs4_proc_unlink_done,
  9499. .rename_setup = nfs4_proc_rename_setup,
  9500. .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
  9501. .rename_done = nfs4_proc_rename_done,
  9502. .link = nfs4_proc_link,
  9503. .symlink = nfs4_proc_symlink,
  9504. .mkdir = nfs4_proc_mkdir,
  9505. .rmdir = nfs4_proc_rmdir,
  9506. .readdir = nfs4_proc_readdir,
  9507. .mknod = nfs4_proc_mknod,
  9508. .statfs = nfs4_proc_statfs,
  9509. .fsinfo = nfs4_proc_fsinfo,
  9510. .pathconf = nfs4_proc_pathconf,
  9511. .set_capabilities = nfs4_server_capabilities,
  9512. .decode_dirent = nfs4_decode_dirent,
  9513. .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
  9514. .read_setup = nfs4_proc_read_setup,
  9515. .read_done = nfs4_read_done,
  9516. .write_setup = nfs4_proc_write_setup,
  9517. .write_done = nfs4_write_done,
  9518. .commit_setup = nfs4_proc_commit_setup,
  9519. .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
  9520. .commit_done = nfs4_commit_done,
  9521. .lock = nfs4_proc_lock,
  9522. .clear_acl_cache = nfs4_zap_acl_attr,
  9523. .close_context = nfs4_close_context,
  9524. .open_context = nfs4_atomic_open,
  9525. .have_delegation = nfs4_have_delegation,
  9526. .return_delegation = nfs4_inode_return_delegation,
  9527. .alloc_client = nfs4_alloc_client,
  9528. .init_client = nfs4_init_client,
  9529. .free_client = nfs4_free_client,
  9530. .create_server = nfs4_create_server,
  9531. .clone_server = nfs4_clone_server,
  9532. .discover_trunking = nfs4_discover_trunking,
  9533. .enable_swap = nfs4_enable_swap,
  9534. .disable_swap = nfs4_disable_swap,
  9535. };
  9536. static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
  9537. .name = XATTR_NAME_NFSV4_ACL,
  9538. .list = nfs4_xattr_list_nfs4_acl,
  9539. .get = nfs4_xattr_get_nfs4_acl,
  9540. .set = nfs4_xattr_set_nfs4_acl,
  9541. };
  9542. static const struct xattr_handler nfs4_xattr_nfs4_dacl_handler = {
  9543. .name = XATTR_NAME_NFSV4_DACL,
  9544. .list = nfs4_xattr_list_nfs4_dacl,
  9545. .get = nfs4_xattr_get_nfs4_dacl,
  9546. .set = nfs4_xattr_set_nfs4_dacl,
  9547. };
  9548. static const struct xattr_handler nfs4_xattr_nfs4_sacl_handler = {
  9549. .name = XATTR_NAME_NFSV4_SACL,
  9550. .list = nfs4_xattr_list_nfs4_sacl,
  9551. .get = nfs4_xattr_get_nfs4_sacl,
  9552. .set = nfs4_xattr_set_nfs4_sacl,
  9553. };
  9554. #ifdef CONFIG_NFS_V4_2
  9555. static const struct xattr_handler nfs4_xattr_nfs4_user_handler = {
  9556. .prefix = XATTR_USER_PREFIX,
  9557. .get = nfs4_xattr_get_nfs4_user,
  9558. .set = nfs4_xattr_set_nfs4_user,
  9559. };
  9560. #endif
  9561. const struct xattr_handler * const nfs4_xattr_handlers[] = {
  9562. &nfs4_xattr_nfs4_acl_handler,
  9563. &nfs4_xattr_nfs4_dacl_handler,
  9564. &nfs4_xattr_nfs4_sacl_handler,
  9565. #ifdef CONFIG_NFS_V4_SECURITY_LABEL
  9566. &nfs4_xattr_nfs4_label_handler,
  9567. #endif
  9568. #ifdef CONFIG_NFS_V4_2
  9569. &nfs4_xattr_nfs4_user_handler,
  9570. #endif
  9571. NULL
  9572. };