nfs4proc.c 236 KB

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