nfs4proc.c 244 KB

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