nfs4proc.c 236 KB

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