nfs4proc.c 263 KB

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