nfs4proc.c 187 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023
  1. /*
  2. * fs/nfs/nfs4proc.c
  3. *
  4. * Client-side procedure declarations for NFSv4.
  5. *
  6. * Copyright (c) 2002 The Regents of the University of Michigan.
  7. * All rights reserved.
  8. *
  9. * Kendrick Smith <kmsmith@umich.edu>
  10. * Andy Adamson <andros@umich.edu>
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. *
  16. * 1. Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. * 2. Redistributions in binary form must reproduce the above copyright
  19. * notice, this list of conditions and the following disclaimer in the
  20. * documentation and/or other materials provided with the distribution.
  21. * 3. Neither the name of the University nor the names of its
  22. * contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  26. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  27. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  28. * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  29. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  30. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  31. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  32. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  33. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  34. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. #include <linux/mm.h>
  38. #include <linux/delay.h>
  39. #include <linux/errno.h>
  40. #include <linux/string.h>
  41. #include <linux/ratelimit.h>
  42. #include <linux/printk.h>
  43. #include <linux/slab.h>
  44. #include <linux/sunrpc/clnt.h>
  45. #include <linux/nfs.h>
  46. #include <linux/nfs4.h>
  47. #include <linux/nfs_fs.h>
  48. #include <linux/nfs_page.h>
  49. #include <linux/nfs_mount.h>
  50. #include <linux/namei.h>
  51. #include <linux/mount.h>
  52. #include <linux/module.h>
  53. #include <linux/nfs_idmap.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 "nfs4session.h"
  65. #include "fscache.h"
  66. #define NFSDBG_FACILITY NFSDBG_PROC
  67. #define NFS4_POLL_RETRY_MIN (HZ/10)
  68. #define NFS4_POLL_RETRY_MAX (15*HZ)
  69. struct nfs4_opendata;
  70. static int _nfs4_proc_open(struct nfs4_opendata *data);
  71. static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
  72. static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
  73. static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
  74. static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
  75. static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *);
  76. static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
  77. static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
  78. struct nfs_fattr *fattr, struct iattr *sattr,
  79. struct nfs4_state *state);
  80. #ifdef CONFIG_NFS_V4_1
  81. static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
  82. static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
  83. #endif
  84. /* Prevent leaks of NFSv4 errors into userland */
  85. static int nfs4_map_errors(int err)
  86. {
  87. if (err >= -1000)
  88. return err;
  89. switch (err) {
  90. case -NFS4ERR_RESOURCE:
  91. case -NFS4ERR_LAYOUTTRYLATER:
  92. case -NFS4ERR_RECALLCONFLICT:
  93. return -EREMOTEIO;
  94. case -NFS4ERR_WRONGSEC:
  95. return -EPERM;
  96. case -NFS4ERR_BADOWNER:
  97. case -NFS4ERR_BADNAME:
  98. return -EINVAL;
  99. case -NFS4ERR_SHARE_DENIED:
  100. return -EACCES;
  101. case -NFS4ERR_MINOR_VERS_MISMATCH:
  102. return -EPROTONOSUPPORT;
  103. case -NFS4ERR_ACCESS:
  104. return -EACCES;
  105. case -NFS4ERR_FILE_OPEN:
  106. return -EBUSY;
  107. default:
  108. dprintk("%s could not handle NFSv4 error %d\n",
  109. __func__, -err);
  110. break;
  111. }
  112. return -EIO;
  113. }
  114. /*
  115. * This is our standard bitmap for GETATTR requests.
  116. */
  117. const u32 nfs4_fattr_bitmap[3] = {
  118. FATTR4_WORD0_TYPE
  119. | FATTR4_WORD0_CHANGE
  120. | FATTR4_WORD0_SIZE
  121. | FATTR4_WORD0_FSID
  122. | FATTR4_WORD0_FILEID,
  123. FATTR4_WORD1_MODE
  124. | FATTR4_WORD1_NUMLINKS
  125. | FATTR4_WORD1_OWNER
  126. | FATTR4_WORD1_OWNER_GROUP
  127. | FATTR4_WORD1_RAWDEV
  128. | FATTR4_WORD1_SPACE_USED
  129. | FATTR4_WORD1_TIME_ACCESS
  130. | FATTR4_WORD1_TIME_METADATA
  131. | FATTR4_WORD1_TIME_MODIFY
  132. };
  133. static const u32 nfs4_pnfs_open_bitmap[3] = {
  134. FATTR4_WORD0_TYPE
  135. | FATTR4_WORD0_CHANGE
  136. | FATTR4_WORD0_SIZE
  137. | FATTR4_WORD0_FSID
  138. | FATTR4_WORD0_FILEID,
  139. FATTR4_WORD1_MODE
  140. | FATTR4_WORD1_NUMLINKS
  141. | FATTR4_WORD1_OWNER
  142. | FATTR4_WORD1_OWNER_GROUP
  143. | FATTR4_WORD1_RAWDEV
  144. | FATTR4_WORD1_SPACE_USED
  145. | FATTR4_WORD1_TIME_ACCESS
  146. | FATTR4_WORD1_TIME_METADATA
  147. | FATTR4_WORD1_TIME_MODIFY,
  148. FATTR4_WORD2_MDSTHRESHOLD
  149. };
  150. static const u32 nfs4_open_noattr_bitmap[3] = {
  151. FATTR4_WORD0_TYPE
  152. | FATTR4_WORD0_CHANGE
  153. | FATTR4_WORD0_FILEID,
  154. };
  155. const u32 nfs4_statfs_bitmap[2] = {
  156. FATTR4_WORD0_FILES_AVAIL
  157. | FATTR4_WORD0_FILES_FREE
  158. | FATTR4_WORD0_FILES_TOTAL,
  159. FATTR4_WORD1_SPACE_AVAIL
  160. | FATTR4_WORD1_SPACE_FREE
  161. | FATTR4_WORD1_SPACE_TOTAL
  162. };
  163. const u32 nfs4_pathconf_bitmap[2] = {
  164. FATTR4_WORD0_MAXLINK
  165. | FATTR4_WORD0_MAXNAME,
  166. 0
  167. };
  168. const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
  169. | FATTR4_WORD0_MAXREAD
  170. | FATTR4_WORD0_MAXWRITE
  171. | FATTR4_WORD0_LEASE_TIME,
  172. FATTR4_WORD1_TIME_DELTA
  173. | FATTR4_WORD1_FS_LAYOUT_TYPES,
  174. FATTR4_WORD2_LAYOUT_BLKSIZE
  175. };
  176. const u32 nfs4_fs_locations_bitmap[2] = {
  177. FATTR4_WORD0_TYPE
  178. | FATTR4_WORD0_CHANGE
  179. | FATTR4_WORD0_SIZE
  180. | FATTR4_WORD0_FSID
  181. | FATTR4_WORD0_FILEID
  182. | FATTR4_WORD0_FS_LOCATIONS,
  183. FATTR4_WORD1_MODE
  184. | FATTR4_WORD1_NUMLINKS
  185. | FATTR4_WORD1_OWNER
  186. | FATTR4_WORD1_OWNER_GROUP
  187. | FATTR4_WORD1_RAWDEV
  188. | FATTR4_WORD1_SPACE_USED
  189. | FATTR4_WORD1_TIME_ACCESS
  190. | FATTR4_WORD1_TIME_METADATA
  191. | FATTR4_WORD1_TIME_MODIFY
  192. | FATTR4_WORD1_MOUNTED_ON_FILEID
  193. };
  194. static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
  195. struct nfs4_readdir_arg *readdir)
  196. {
  197. __be32 *start, *p;
  198. if (cookie > 2) {
  199. readdir->cookie = cookie;
  200. memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
  201. return;
  202. }
  203. readdir->cookie = 0;
  204. memset(&readdir->verifier, 0, sizeof(readdir->verifier));
  205. if (cookie == 2)
  206. return;
  207. /*
  208. * NFSv4 servers do not return entries for '.' and '..'
  209. * Therefore, we fake these entries here. We let '.'
  210. * have cookie 0 and '..' have cookie 1. Note that
  211. * when talking to the server, we always send cookie 0
  212. * instead of 1 or 2.
  213. */
  214. start = p = kmap_atomic(*readdir->pages);
  215. if (cookie == 0) {
  216. *p++ = xdr_one; /* next */
  217. *p++ = xdr_zero; /* cookie, first word */
  218. *p++ = xdr_one; /* cookie, second word */
  219. *p++ = xdr_one; /* entry len */
  220. memcpy(p, ".\0\0\0", 4); /* entry */
  221. p++;
  222. *p++ = xdr_one; /* bitmap length */
  223. *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
  224. *p++ = htonl(8); /* attribute buffer length */
  225. p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
  226. }
  227. *p++ = xdr_one; /* next */
  228. *p++ = xdr_zero; /* cookie, first word */
  229. *p++ = xdr_two; /* cookie, second word */
  230. *p++ = xdr_two; /* entry len */
  231. memcpy(p, "..\0\0", 4); /* entry */
  232. p++;
  233. *p++ = xdr_one; /* bitmap length */
  234. *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
  235. *p++ = htonl(8); /* attribute buffer length */
  236. p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
  237. readdir->pgbase = (char *)p - (char *)start;
  238. readdir->count -= readdir->pgbase;
  239. kunmap_atomic(start);
  240. }
  241. static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
  242. {
  243. int res = 0;
  244. might_sleep();
  245. if (*timeout <= 0)
  246. *timeout = NFS4_POLL_RETRY_MIN;
  247. if (*timeout > NFS4_POLL_RETRY_MAX)
  248. *timeout = NFS4_POLL_RETRY_MAX;
  249. freezable_schedule_timeout_killable(*timeout);
  250. if (fatal_signal_pending(current))
  251. res = -ERESTARTSYS;
  252. *timeout <<= 1;
  253. return res;
  254. }
  255. /* This is the error handling routine for processes that are allowed
  256. * to sleep.
  257. */
  258. static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
  259. {
  260. struct nfs_client *clp = server->nfs_client;
  261. struct nfs4_state *state = exception->state;
  262. struct inode *inode = exception->inode;
  263. int ret = errorcode;
  264. exception->retry = 0;
  265. switch(errorcode) {
  266. case 0:
  267. return 0;
  268. case -NFS4ERR_OPENMODE:
  269. if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
  270. nfs4_inode_return_delegation(inode);
  271. exception->retry = 1;
  272. return 0;
  273. }
  274. if (state == NULL)
  275. break;
  276. ret = nfs4_schedule_stateid_recovery(server, state);
  277. if (ret < 0)
  278. break;
  279. goto wait_on_recovery;
  280. case -NFS4ERR_DELEG_REVOKED:
  281. case -NFS4ERR_ADMIN_REVOKED:
  282. case -NFS4ERR_BAD_STATEID:
  283. if (state == NULL)
  284. break;
  285. nfs_remove_bad_delegation(state->inode);
  286. ret = nfs4_schedule_stateid_recovery(server, state);
  287. if (ret < 0)
  288. break;
  289. goto wait_on_recovery;
  290. case -NFS4ERR_EXPIRED:
  291. if (state != NULL) {
  292. ret = nfs4_schedule_stateid_recovery(server, state);
  293. if (ret < 0)
  294. break;
  295. }
  296. case -NFS4ERR_STALE_STATEID:
  297. case -NFS4ERR_STALE_CLIENTID:
  298. nfs4_schedule_lease_recovery(clp);
  299. goto wait_on_recovery;
  300. #if defined(CONFIG_NFS_V4_1)
  301. case -NFS4ERR_BADSESSION:
  302. case -NFS4ERR_BADSLOT:
  303. case -NFS4ERR_BAD_HIGH_SLOT:
  304. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  305. case -NFS4ERR_DEADSESSION:
  306. case -NFS4ERR_SEQ_FALSE_RETRY:
  307. case -NFS4ERR_SEQ_MISORDERED:
  308. dprintk("%s ERROR: %d Reset session\n", __func__,
  309. errorcode);
  310. nfs4_schedule_session_recovery(clp->cl_session, errorcode);
  311. goto wait_on_recovery;
  312. #endif /* defined(CONFIG_NFS_V4_1) */
  313. case -NFS4ERR_FILE_OPEN:
  314. if (exception->timeout > HZ) {
  315. /* We have retried a decent amount, time to
  316. * fail
  317. */
  318. ret = -EBUSY;
  319. break;
  320. }
  321. case -NFS4ERR_GRACE:
  322. case -NFS4ERR_DELAY:
  323. ret = nfs4_delay(server->client, &exception->timeout);
  324. if (ret != 0)
  325. break;
  326. case -NFS4ERR_RETRY_UNCACHED_REP:
  327. case -NFS4ERR_OLD_STATEID:
  328. exception->retry = 1;
  329. break;
  330. case -NFS4ERR_BADOWNER:
  331. /* The following works around a Linux server bug! */
  332. case -NFS4ERR_BADNAME:
  333. if (server->caps & NFS_CAP_UIDGID_NOMAP) {
  334. server->caps &= ~NFS_CAP_UIDGID_NOMAP;
  335. exception->retry = 1;
  336. printk(KERN_WARNING "NFS: v4 server %s "
  337. "does not accept raw "
  338. "uid/gids. "
  339. "Reenabling the idmapper.\n",
  340. server->nfs_client->cl_hostname);
  341. }
  342. }
  343. /* We failed to handle the error */
  344. return nfs4_map_errors(ret);
  345. wait_on_recovery:
  346. ret = nfs4_wait_clnt_recover(clp);
  347. if (ret == 0)
  348. exception->retry = 1;
  349. return ret;
  350. }
  351. static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
  352. {
  353. spin_lock(&clp->cl_lock);
  354. if (time_before(clp->cl_last_renewal,timestamp))
  355. clp->cl_last_renewal = timestamp;
  356. spin_unlock(&clp->cl_lock);
  357. }
  358. static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
  359. {
  360. do_renew_lease(server->nfs_client, timestamp);
  361. }
  362. #if defined(CONFIG_NFS_V4_1)
  363. static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
  364. {
  365. struct nfs4_session *session;
  366. struct nfs4_slot_table *tbl;
  367. bool send_new_highest_used_slotid = false;
  368. if (!res->sr_slot) {
  369. /* just wake up the next guy waiting since
  370. * we may have not consumed a slot after all */
  371. dprintk("%s: No slot\n", __func__);
  372. return;
  373. }
  374. tbl = res->sr_slot->table;
  375. session = tbl->session;
  376. spin_lock(&tbl->slot_tbl_lock);
  377. /* Be nice to the server: try to ensure that the last transmitted
  378. * value for highest_user_slotid <= target_highest_slotid
  379. */
  380. if (tbl->highest_used_slotid > tbl->target_highest_slotid)
  381. send_new_highest_used_slotid = true;
  382. if (nfs41_wake_and_assign_slot(tbl, res->sr_slot)) {
  383. send_new_highest_used_slotid = false;
  384. goto out_unlock;
  385. }
  386. nfs4_free_slot(tbl, res->sr_slot);
  387. if (tbl->highest_used_slotid != NFS4_NO_SLOT)
  388. send_new_highest_used_slotid = false;
  389. out_unlock:
  390. spin_unlock(&tbl->slot_tbl_lock);
  391. res->sr_slot = NULL;
  392. if (send_new_highest_used_slotid)
  393. nfs41_server_notify_highest_slotid_update(session->clp);
  394. }
  395. static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
  396. {
  397. struct nfs4_session *session;
  398. struct nfs4_slot *slot;
  399. struct nfs_client *clp;
  400. bool interrupted = false;
  401. int ret = 1;
  402. /* don't increment the sequence number if the task wasn't sent */
  403. if (!RPC_WAS_SENT(task))
  404. goto out;
  405. slot = res->sr_slot;
  406. session = slot->table->session;
  407. if (slot->interrupted) {
  408. slot->interrupted = 0;
  409. interrupted = true;
  410. }
  411. /* Check the SEQUENCE operation status */
  412. switch (res->sr_status) {
  413. case 0:
  414. /* Update the slot's sequence and clientid lease timer */
  415. ++slot->seq_nr;
  416. clp = session->clp;
  417. do_renew_lease(clp, res->sr_timestamp);
  418. /* Check sequence flags */
  419. if (res->sr_status_flags != 0)
  420. nfs4_schedule_lease_recovery(clp);
  421. nfs41_update_target_slotid(slot->table, slot, res);
  422. break;
  423. case 1:
  424. /*
  425. * sr_status remains 1 if an RPC level error occurred.
  426. * The server may or may not have processed the sequence
  427. * operation..
  428. * Mark the slot as having hosted an interrupted RPC call.
  429. */
  430. slot->interrupted = 1;
  431. goto out;
  432. case -NFS4ERR_DELAY:
  433. /* The server detected a resend of the RPC call and
  434. * returned NFS4ERR_DELAY as per Section 2.10.6.2
  435. * of RFC5661.
  436. */
  437. dprintk("%s: slot=%u seq=%u: Operation in progress\n",
  438. __func__,
  439. slot->slot_nr,
  440. slot->seq_nr);
  441. goto out_retry;
  442. case -NFS4ERR_BADSLOT:
  443. /*
  444. * The slot id we used was probably retired. Try again
  445. * using a different slot id.
  446. */
  447. goto retry_nowait;
  448. case -NFS4ERR_SEQ_MISORDERED:
  449. /*
  450. * Was the last operation on this sequence interrupted?
  451. * If so, retry after bumping the sequence number.
  452. */
  453. if (interrupted) {
  454. ++slot->seq_nr;
  455. goto retry_nowait;
  456. }
  457. /*
  458. * Could this slot have been previously retired?
  459. * If so, then the server may be expecting seq_nr = 1!
  460. */
  461. if (slot->seq_nr != 1) {
  462. slot->seq_nr = 1;
  463. goto retry_nowait;
  464. }
  465. break;
  466. case -NFS4ERR_SEQ_FALSE_RETRY:
  467. ++slot->seq_nr;
  468. goto retry_nowait;
  469. default:
  470. /* Just update the slot sequence no. */
  471. ++slot->seq_nr;
  472. }
  473. out:
  474. /* The session may be reset by one of the error handlers. */
  475. dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
  476. nfs41_sequence_free_slot(res);
  477. return ret;
  478. retry_nowait:
  479. if (rpc_restart_call_prepare(task)) {
  480. task->tk_status = 0;
  481. ret = 0;
  482. }
  483. goto out;
  484. out_retry:
  485. if (!rpc_restart_call(task))
  486. goto out;
  487. rpc_delay(task, NFS4_POLL_RETRY_MAX);
  488. return 0;
  489. }
  490. static int nfs4_sequence_done(struct rpc_task *task,
  491. struct nfs4_sequence_res *res)
  492. {
  493. if (res->sr_slot == NULL)
  494. return 1;
  495. return nfs41_sequence_done(task, res);
  496. }
  497. static void nfs41_init_sequence(struct nfs4_sequence_args *args,
  498. struct nfs4_sequence_res *res, int cache_reply)
  499. {
  500. args->sa_slot = NULL;
  501. args->sa_cache_this = 0;
  502. args->sa_privileged = 0;
  503. if (cache_reply)
  504. args->sa_cache_this = 1;
  505. res->sr_slot = NULL;
  506. }
  507. static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
  508. {
  509. args->sa_privileged = 1;
  510. }
  511. int nfs41_setup_sequence(struct nfs4_session *session,
  512. struct nfs4_sequence_args *args,
  513. struct nfs4_sequence_res *res,
  514. struct rpc_task *task)
  515. {
  516. struct nfs4_slot *slot;
  517. struct nfs4_slot_table *tbl;
  518. dprintk("--> %s\n", __func__);
  519. /* slot already allocated? */
  520. if (res->sr_slot != NULL)
  521. goto out_success;
  522. tbl = &session->fc_slot_table;
  523. task->tk_timeout = 0;
  524. spin_lock(&tbl->slot_tbl_lock);
  525. if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
  526. !args->sa_privileged) {
  527. /* The state manager will wait until the slot table is empty */
  528. dprintk("%s session is draining\n", __func__);
  529. goto out_sleep;
  530. }
  531. slot = nfs4_alloc_slot(tbl);
  532. if (IS_ERR(slot)) {
  533. /* If out of memory, try again in 1/4 second */
  534. if (slot == ERR_PTR(-ENOMEM))
  535. task->tk_timeout = HZ >> 2;
  536. dprintk("<-- %s: no free slots\n", __func__);
  537. goto out_sleep;
  538. }
  539. spin_unlock(&tbl->slot_tbl_lock);
  540. args->sa_slot = slot;
  541. dprintk("<-- %s slotid=%d seqid=%d\n", __func__,
  542. slot->slot_nr, slot->seq_nr);
  543. res->sr_slot = slot;
  544. res->sr_timestamp = jiffies;
  545. res->sr_status_flags = 0;
  546. /*
  547. * sr_status is only set in decode_sequence, and so will remain
  548. * set to 1 if an rpc level failure occurs.
  549. */
  550. res->sr_status = 1;
  551. out_success:
  552. rpc_call_start(task);
  553. return 0;
  554. out_sleep:
  555. /* Privileged tasks are queued with top priority */
  556. if (args->sa_privileged)
  557. rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
  558. NULL, RPC_PRIORITY_PRIVILEGED);
  559. else
  560. rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
  561. spin_unlock(&tbl->slot_tbl_lock);
  562. return -EAGAIN;
  563. }
  564. EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
  565. int nfs4_setup_sequence(const struct nfs_server *server,
  566. struct nfs4_sequence_args *args,
  567. struct nfs4_sequence_res *res,
  568. struct rpc_task *task)
  569. {
  570. struct nfs4_session *session = nfs4_get_session(server);
  571. int ret = 0;
  572. if (session == NULL) {
  573. rpc_call_start(task);
  574. goto out;
  575. }
  576. dprintk("--> %s clp %p session %p sr_slot %d\n",
  577. __func__, session->clp, session, res->sr_slot ?
  578. res->sr_slot->slot_nr : -1);
  579. ret = nfs41_setup_sequence(session, args, res, task);
  580. out:
  581. dprintk("<-- %s status=%d\n", __func__, ret);
  582. return ret;
  583. }
  584. struct nfs41_call_sync_data {
  585. const struct nfs_server *seq_server;
  586. struct nfs4_sequence_args *seq_args;
  587. struct nfs4_sequence_res *seq_res;
  588. };
  589. static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
  590. {
  591. struct nfs41_call_sync_data *data = calldata;
  592. struct nfs4_session *session = nfs4_get_session(data->seq_server);
  593. dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
  594. nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
  595. }
  596. static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
  597. {
  598. struct nfs41_call_sync_data *data = calldata;
  599. nfs41_sequence_done(task, data->seq_res);
  600. }
  601. static const struct rpc_call_ops nfs41_call_sync_ops = {
  602. .rpc_call_prepare = nfs41_call_sync_prepare,
  603. .rpc_call_done = nfs41_call_sync_done,
  604. };
  605. static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
  606. struct nfs_server *server,
  607. struct rpc_message *msg,
  608. struct nfs4_sequence_args *args,
  609. struct nfs4_sequence_res *res)
  610. {
  611. int ret;
  612. struct rpc_task *task;
  613. struct nfs41_call_sync_data data = {
  614. .seq_server = server,
  615. .seq_args = args,
  616. .seq_res = res,
  617. };
  618. struct rpc_task_setup task_setup = {
  619. .rpc_client = clnt,
  620. .rpc_message = msg,
  621. .callback_ops = &nfs41_call_sync_ops,
  622. .callback_data = &data
  623. };
  624. task = rpc_run_task(&task_setup);
  625. if (IS_ERR(task))
  626. ret = PTR_ERR(task);
  627. else {
  628. ret = task->tk_status;
  629. rpc_put_task(task);
  630. }
  631. return ret;
  632. }
  633. #else
  634. static
  635. void nfs41_init_sequence(struct nfs4_sequence_args *args,
  636. struct nfs4_sequence_res *res, int cache_reply)
  637. {
  638. }
  639. static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
  640. {
  641. }
  642. static int nfs4_sequence_done(struct rpc_task *task,
  643. struct nfs4_sequence_res *res)
  644. {
  645. return 1;
  646. }
  647. #endif /* CONFIG_NFS_V4_1 */
  648. static
  649. int _nfs4_call_sync(struct rpc_clnt *clnt,
  650. struct nfs_server *server,
  651. struct rpc_message *msg,
  652. struct nfs4_sequence_args *args,
  653. struct nfs4_sequence_res *res)
  654. {
  655. return rpc_call_sync(clnt, msg, 0);
  656. }
  657. static
  658. int nfs4_call_sync(struct rpc_clnt *clnt,
  659. struct nfs_server *server,
  660. struct rpc_message *msg,
  661. struct nfs4_sequence_args *args,
  662. struct nfs4_sequence_res *res,
  663. int cache_reply)
  664. {
  665. nfs41_init_sequence(args, res, cache_reply);
  666. return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
  667. args, res);
  668. }
  669. static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
  670. {
  671. struct nfs_inode *nfsi = NFS_I(dir);
  672. spin_lock(&dir->i_lock);
  673. nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
  674. if (!cinfo->atomic || cinfo->before != dir->i_version)
  675. nfs_force_lookup_revalidate(dir);
  676. dir->i_version = cinfo->after;
  677. nfs_fscache_invalidate(dir);
  678. spin_unlock(&dir->i_lock);
  679. }
  680. struct nfs4_opendata {
  681. struct kref kref;
  682. struct nfs_openargs o_arg;
  683. struct nfs_openres o_res;
  684. struct nfs_open_confirmargs c_arg;
  685. struct nfs_open_confirmres c_res;
  686. struct nfs4_string owner_name;
  687. struct nfs4_string group_name;
  688. struct nfs_fattr f_attr;
  689. struct dentry *dir;
  690. struct dentry *dentry;
  691. struct nfs4_state_owner *owner;
  692. struct nfs4_state *state;
  693. struct iattr attrs;
  694. unsigned long timestamp;
  695. unsigned int rpc_done : 1;
  696. int rpc_status;
  697. int cancelled;
  698. };
  699. static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
  700. int err, struct nfs4_exception *exception)
  701. {
  702. if (err != -EINVAL)
  703. return false;
  704. if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
  705. return false;
  706. server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
  707. exception->retry = 1;
  708. return true;
  709. }
  710. static enum open_claim_type4
  711. nfs4_map_atomic_open_claim(struct nfs_server *server,
  712. enum open_claim_type4 claim)
  713. {
  714. if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
  715. return claim;
  716. switch (claim) {
  717. default:
  718. return claim;
  719. case NFS4_OPEN_CLAIM_FH:
  720. return NFS4_OPEN_CLAIM_NULL;
  721. case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
  722. return NFS4_OPEN_CLAIM_DELEGATE_CUR;
  723. case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
  724. return NFS4_OPEN_CLAIM_DELEGATE_PREV;
  725. }
  726. }
  727. static void nfs4_init_opendata_res(struct nfs4_opendata *p)
  728. {
  729. p->o_res.f_attr = &p->f_attr;
  730. p->o_res.seqid = p->o_arg.seqid;
  731. p->c_res.seqid = p->c_arg.seqid;
  732. p->o_res.server = p->o_arg.server;
  733. p->o_res.access_request = p->o_arg.access;
  734. nfs_fattr_init(&p->f_attr);
  735. nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
  736. }
  737. static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
  738. struct nfs4_state_owner *sp, fmode_t fmode, int flags,
  739. const struct iattr *attrs,
  740. enum open_claim_type4 claim,
  741. gfp_t gfp_mask)
  742. {
  743. struct dentry *parent = dget_parent(dentry);
  744. struct inode *dir = parent->d_inode;
  745. struct nfs_server *server = NFS_SERVER(dir);
  746. struct nfs4_opendata *p;
  747. p = kzalloc(sizeof(*p), gfp_mask);
  748. if (p == NULL)
  749. goto err;
  750. p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
  751. if (p->o_arg.seqid == NULL)
  752. goto err_free;
  753. nfs_sb_active(dentry->d_sb);
  754. p->dentry = dget(dentry);
  755. p->dir = parent;
  756. p->owner = sp;
  757. atomic_inc(&sp->so_count);
  758. p->o_arg.open_flags = flags;
  759. p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
  760. /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
  761. * will return permission denied for all bits until close */
  762. if (!(flags & O_EXCL)) {
  763. /* ask server to check for all possible rights as results
  764. * are cached */
  765. p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
  766. NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
  767. }
  768. p->o_arg.clientid = server->nfs_client->cl_clientid;
  769. p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
  770. p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
  771. p->o_arg.name = &dentry->d_name;
  772. p->o_arg.server = server;
  773. p->o_arg.bitmask = server->attr_bitmask;
  774. p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
  775. p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
  776. switch (p->o_arg.claim) {
  777. case NFS4_OPEN_CLAIM_NULL:
  778. case NFS4_OPEN_CLAIM_DELEGATE_CUR:
  779. case NFS4_OPEN_CLAIM_DELEGATE_PREV:
  780. p->o_arg.fh = NFS_FH(dir);
  781. break;
  782. case NFS4_OPEN_CLAIM_PREVIOUS:
  783. case NFS4_OPEN_CLAIM_FH:
  784. case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
  785. case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
  786. p->o_arg.fh = NFS_FH(dentry->d_inode);
  787. }
  788. if (attrs != NULL && attrs->ia_valid != 0) {
  789. __be32 verf[2];
  790. p->o_arg.u.attrs = &p->attrs;
  791. memcpy(&p->attrs, attrs, sizeof(p->attrs));
  792. verf[0] = jiffies;
  793. verf[1] = current->pid;
  794. memcpy(p->o_arg.u.verifier.data, verf,
  795. sizeof(p->o_arg.u.verifier.data));
  796. }
  797. p->c_arg.fh = &p->o_res.fh;
  798. p->c_arg.stateid = &p->o_res.stateid;
  799. p->c_arg.seqid = p->o_arg.seqid;
  800. nfs4_init_opendata_res(p);
  801. kref_init(&p->kref);
  802. return p;
  803. err_free:
  804. kfree(p);
  805. err:
  806. dput(parent);
  807. return NULL;
  808. }
  809. static void nfs4_opendata_free(struct kref *kref)
  810. {
  811. struct nfs4_opendata *p = container_of(kref,
  812. struct nfs4_opendata, kref);
  813. struct super_block *sb = p->dentry->d_sb;
  814. nfs_free_seqid(p->o_arg.seqid);
  815. if (p->state != NULL)
  816. nfs4_put_open_state(p->state);
  817. nfs4_put_state_owner(p->owner);
  818. dput(p->dir);
  819. dput(p->dentry);
  820. nfs_sb_deactive(sb);
  821. nfs_fattr_free_names(&p->f_attr);
  822. kfree(p);
  823. }
  824. static void nfs4_opendata_put(struct nfs4_opendata *p)
  825. {
  826. if (p != NULL)
  827. kref_put(&p->kref, nfs4_opendata_free);
  828. }
  829. static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
  830. {
  831. int ret;
  832. ret = rpc_wait_for_completion_task(task);
  833. return ret;
  834. }
  835. static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
  836. {
  837. int ret = 0;
  838. if (open_mode & (O_EXCL|O_TRUNC))
  839. goto out;
  840. switch (mode & (FMODE_READ|FMODE_WRITE)) {
  841. case FMODE_READ:
  842. ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
  843. && state->n_rdonly != 0;
  844. break;
  845. case FMODE_WRITE:
  846. ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
  847. && state->n_wronly != 0;
  848. break;
  849. case FMODE_READ|FMODE_WRITE:
  850. ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
  851. && state->n_rdwr != 0;
  852. }
  853. out:
  854. return ret;
  855. }
  856. static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
  857. {
  858. if (delegation == NULL)
  859. return 0;
  860. if ((delegation->type & fmode) != fmode)
  861. return 0;
  862. if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
  863. return 0;
  864. if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
  865. return 0;
  866. nfs_mark_delegation_referenced(delegation);
  867. return 1;
  868. }
  869. static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
  870. {
  871. switch (fmode) {
  872. case FMODE_WRITE:
  873. state->n_wronly++;
  874. break;
  875. case FMODE_READ:
  876. state->n_rdonly++;
  877. break;
  878. case FMODE_READ|FMODE_WRITE:
  879. state->n_rdwr++;
  880. }
  881. nfs4_state_set_mode_locked(state, state->state | fmode);
  882. }
  883. static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
  884. {
  885. if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
  886. nfs4_stateid_copy(&state->stateid, stateid);
  887. nfs4_stateid_copy(&state->open_stateid, stateid);
  888. switch (fmode) {
  889. case FMODE_READ:
  890. set_bit(NFS_O_RDONLY_STATE, &state->flags);
  891. break;
  892. case FMODE_WRITE:
  893. set_bit(NFS_O_WRONLY_STATE, &state->flags);
  894. break;
  895. case FMODE_READ|FMODE_WRITE:
  896. set_bit(NFS_O_RDWR_STATE, &state->flags);
  897. }
  898. }
  899. static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
  900. {
  901. write_seqlock(&state->seqlock);
  902. nfs_set_open_stateid_locked(state, stateid, fmode);
  903. write_sequnlock(&state->seqlock);
  904. }
  905. static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
  906. {
  907. /*
  908. * Protect the call to nfs4_state_set_mode_locked and
  909. * serialise the stateid update
  910. */
  911. write_seqlock(&state->seqlock);
  912. if (deleg_stateid != NULL) {
  913. nfs4_stateid_copy(&state->stateid, deleg_stateid);
  914. set_bit(NFS_DELEGATED_STATE, &state->flags);
  915. }
  916. if (open_stateid != NULL)
  917. nfs_set_open_stateid_locked(state, open_stateid, fmode);
  918. write_sequnlock(&state->seqlock);
  919. spin_lock(&state->owner->so_lock);
  920. update_open_stateflags(state, fmode);
  921. spin_unlock(&state->owner->so_lock);
  922. }
  923. static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
  924. {
  925. struct nfs_inode *nfsi = NFS_I(state->inode);
  926. struct nfs_delegation *deleg_cur;
  927. int ret = 0;
  928. fmode &= (FMODE_READ|FMODE_WRITE);
  929. rcu_read_lock();
  930. deleg_cur = rcu_dereference(nfsi->delegation);
  931. if (deleg_cur == NULL)
  932. goto no_delegation;
  933. spin_lock(&deleg_cur->lock);
  934. if (nfsi->delegation != deleg_cur ||
  935. test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
  936. (deleg_cur->type & fmode) != fmode)
  937. goto no_delegation_unlock;
  938. if (delegation == NULL)
  939. delegation = &deleg_cur->stateid;
  940. else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
  941. goto no_delegation_unlock;
  942. nfs_mark_delegation_referenced(deleg_cur);
  943. __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
  944. ret = 1;
  945. no_delegation_unlock:
  946. spin_unlock(&deleg_cur->lock);
  947. no_delegation:
  948. rcu_read_unlock();
  949. if (!ret && open_stateid != NULL) {
  950. __update_open_stateid(state, open_stateid, NULL, fmode);
  951. ret = 1;
  952. }
  953. return ret;
  954. }
  955. static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
  956. {
  957. struct nfs_delegation *delegation;
  958. rcu_read_lock();
  959. delegation = rcu_dereference(NFS_I(inode)->delegation);
  960. if (delegation == NULL || (delegation->type & fmode) == fmode) {
  961. rcu_read_unlock();
  962. return;
  963. }
  964. rcu_read_unlock();
  965. nfs4_inode_return_delegation(inode);
  966. }
  967. static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
  968. {
  969. struct nfs4_state *state = opendata->state;
  970. struct nfs_inode *nfsi = NFS_I(state->inode);
  971. struct nfs_delegation *delegation;
  972. int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
  973. fmode_t fmode = opendata->o_arg.fmode;
  974. nfs4_stateid stateid;
  975. int ret = -EAGAIN;
  976. for (;;) {
  977. if (can_open_cached(state, fmode, open_mode)) {
  978. spin_lock(&state->owner->so_lock);
  979. if (can_open_cached(state, fmode, open_mode)) {
  980. update_open_stateflags(state, fmode);
  981. spin_unlock(&state->owner->so_lock);
  982. goto out_return_state;
  983. }
  984. spin_unlock(&state->owner->so_lock);
  985. }
  986. rcu_read_lock();
  987. delegation = rcu_dereference(nfsi->delegation);
  988. if (!can_open_delegated(delegation, fmode)) {
  989. rcu_read_unlock();
  990. break;
  991. }
  992. /* Save the delegation */
  993. nfs4_stateid_copy(&stateid, &delegation->stateid);
  994. rcu_read_unlock();
  995. ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
  996. if (ret != 0)
  997. goto out;
  998. ret = -EAGAIN;
  999. /* Try to update the stateid using the delegation */
  1000. if (update_open_stateid(state, NULL, &stateid, fmode))
  1001. goto out_return_state;
  1002. }
  1003. out:
  1004. return ERR_PTR(ret);
  1005. out_return_state:
  1006. atomic_inc(&state->count);
  1007. return state;
  1008. }
  1009. static void
  1010. nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
  1011. {
  1012. struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
  1013. struct nfs_delegation *delegation;
  1014. int delegation_flags = 0;
  1015. rcu_read_lock();
  1016. delegation = rcu_dereference(NFS_I(state->inode)->delegation);
  1017. if (delegation)
  1018. delegation_flags = delegation->flags;
  1019. rcu_read_unlock();
  1020. if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
  1021. pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
  1022. "returning a delegation for "
  1023. "OPEN(CLAIM_DELEGATE_CUR)\n",
  1024. clp->cl_hostname);
  1025. } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
  1026. nfs_inode_set_delegation(state->inode,
  1027. data->owner->so_cred,
  1028. &data->o_res);
  1029. else
  1030. nfs_inode_reclaim_delegation(state->inode,
  1031. data->owner->so_cred,
  1032. &data->o_res);
  1033. }
  1034. /*
  1035. * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
  1036. * and update the nfs4_state.
  1037. */
  1038. static struct nfs4_state *
  1039. _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
  1040. {
  1041. struct inode *inode = data->state->inode;
  1042. struct nfs4_state *state = data->state;
  1043. int ret;
  1044. if (!data->rpc_done) {
  1045. ret = data->rpc_status;
  1046. goto err;
  1047. }
  1048. ret = -ESTALE;
  1049. if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
  1050. !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
  1051. !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
  1052. goto err;
  1053. ret = -ENOMEM;
  1054. state = nfs4_get_open_state(inode, data->owner);
  1055. if (state == NULL)
  1056. goto err;
  1057. ret = nfs_refresh_inode(inode, &data->f_attr);
  1058. if (ret)
  1059. goto err;
  1060. if (data->o_res.delegation_type != 0)
  1061. nfs4_opendata_check_deleg(data, state);
  1062. update_open_stateid(state, &data->o_res.stateid, NULL,
  1063. data->o_arg.fmode);
  1064. return state;
  1065. err:
  1066. return ERR_PTR(ret);
  1067. }
  1068. static struct nfs4_state *
  1069. _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
  1070. {
  1071. struct inode *inode;
  1072. struct nfs4_state *state = NULL;
  1073. int ret;
  1074. if (!data->rpc_done) {
  1075. state = nfs4_try_open_cached(data);
  1076. goto out;
  1077. }
  1078. ret = -EAGAIN;
  1079. if (!(data->f_attr.valid & NFS_ATTR_FATTR))
  1080. goto err;
  1081. inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
  1082. ret = PTR_ERR(inode);
  1083. if (IS_ERR(inode))
  1084. goto err;
  1085. ret = -ENOMEM;
  1086. state = nfs4_get_open_state(inode, data->owner);
  1087. if (state == NULL)
  1088. goto err_put_inode;
  1089. if (data->o_res.delegation_type != 0)
  1090. nfs4_opendata_check_deleg(data, state);
  1091. update_open_stateid(state, &data->o_res.stateid, NULL,
  1092. data->o_arg.fmode);
  1093. iput(inode);
  1094. out:
  1095. nfs_release_seqid(data->o_arg.seqid);
  1096. return state;
  1097. err_put_inode:
  1098. iput(inode);
  1099. err:
  1100. return ERR_PTR(ret);
  1101. }
  1102. static struct nfs4_state *
  1103. nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
  1104. {
  1105. if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
  1106. return _nfs4_opendata_reclaim_to_nfs4_state(data);
  1107. return _nfs4_opendata_to_nfs4_state(data);
  1108. }
  1109. static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
  1110. {
  1111. struct nfs_inode *nfsi = NFS_I(state->inode);
  1112. struct nfs_open_context *ctx;
  1113. spin_lock(&state->inode->i_lock);
  1114. list_for_each_entry(ctx, &nfsi->open_files, list) {
  1115. if (ctx->state != state)
  1116. continue;
  1117. get_nfs_open_context(ctx);
  1118. spin_unlock(&state->inode->i_lock);
  1119. return ctx;
  1120. }
  1121. spin_unlock(&state->inode->i_lock);
  1122. return ERR_PTR(-ENOENT);
  1123. }
  1124. static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
  1125. struct nfs4_state *state, enum open_claim_type4 claim)
  1126. {
  1127. struct nfs4_opendata *opendata;
  1128. opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
  1129. NULL, claim, GFP_NOFS);
  1130. if (opendata == NULL)
  1131. return ERR_PTR(-ENOMEM);
  1132. opendata->state = state;
  1133. atomic_inc(&state->count);
  1134. return opendata;
  1135. }
  1136. static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
  1137. {
  1138. struct nfs4_state *newstate;
  1139. int ret;
  1140. opendata->o_arg.open_flags = 0;
  1141. opendata->o_arg.fmode = fmode;
  1142. memset(&opendata->o_res, 0, sizeof(opendata->o_res));
  1143. memset(&opendata->c_res, 0, sizeof(opendata->c_res));
  1144. nfs4_init_opendata_res(opendata);
  1145. ret = _nfs4_recover_proc_open(opendata);
  1146. if (ret != 0)
  1147. return ret;
  1148. newstate = nfs4_opendata_to_nfs4_state(opendata);
  1149. if (IS_ERR(newstate))
  1150. return PTR_ERR(newstate);
  1151. nfs4_close_state(newstate, fmode);
  1152. *res = newstate;
  1153. return 0;
  1154. }
  1155. static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
  1156. {
  1157. struct nfs4_state *newstate;
  1158. int ret;
  1159. /* memory barrier prior to reading state->n_* */
  1160. clear_bit(NFS_DELEGATED_STATE, &state->flags);
  1161. smp_rmb();
  1162. if (state->n_rdwr != 0) {
  1163. clear_bit(NFS_O_RDWR_STATE, &state->flags);
  1164. ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
  1165. if (ret != 0)
  1166. return ret;
  1167. if (newstate != state)
  1168. return -ESTALE;
  1169. }
  1170. if (state->n_wronly != 0) {
  1171. clear_bit(NFS_O_WRONLY_STATE, &state->flags);
  1172. ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
  1173. if (ret != 0)
  1174. return ret;
  1175. if (newstate != state)
  1176. return -ESTALE;
  1177. }
  1178. if (state->n_rdonly != 0) {
  1179. clear_bit(NFS_O_RDONLY_STATE, &state->flags);
  1180. ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
  1181. if (ret != 0)
  1182. return ret;
  1183. if (newstate != state)
  1184. return -ESTALE;
  1185. }
  1186. /*
  1187. * We may have performed cached opens for all three recoveries.
  1188. * Check if we need to update the current stateid.
  1189. */
  1190. if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
  1191. !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
  1192. write_seqlock(&state->seqlock);
  1193. if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
  1194. nfs4_stateid_copy(&state->stateid, &state->open_stateid);
  1195. write_sequnlock(&state->seqlock);
  1196. }
  1197. return 0;
  1198. }
  1199. /*
  1200. * OPEN_RECLAIM:
  1201. * reclaim state on the server after a reboot.
  1202. */
  1203. static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
  1204. {
  1205. struct nfs_delegation *delegation;
  1206. struct nfs4_opendata *opendata;
  1207. fmode_t delegation_type = 0;
  1208. int status;
  1209. opendata = nfs4_open_recoverdata_alloc(ctx, state,
  1210. NFS4_OPEN_CLAIM_PREVIOUS);
  1211. if (IS_ERR(opendata))
  1212. return PTR_ERR(opendata);
  1213. rcu_read_lock();
  1214. delegation = rcu_dereference(NFS_I(state->inode)->delegation);
  1215. if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
  1216. delegation_type = delegation->type;
  1217. rcu_read_unlock();
  1218. opendata->o_arg.u.delegation_type = delegation_type;
  1219. status = nfs4_open_recover(opendata, state);
  1220. nfs4_opendata_put(opendata);
  1221. return status;
  1222. }
  1223. static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
  1224. {
  1225. struct nfs_server *server = NFS_SERVER(state->inode);
  1226. struct nfs4_exception exception = { };
  1227. int err;
  1228. do {
  1229. err = _nfs4_do_open_reclaim(ctx, state);
  1230. if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
  1231. continue;
  1232. if (err != -NFS4ERR_DELAY)
  1233. break;
  1234. nfs4_handle_exception(server, err, &exception);
  1235. } while (exception.retry);
  1236. return err;
  1237. }
  1238. static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
  1239. {
  1240. struct nfs_open_context *ctx;
  1241. int ret;
  1242. ctx = nfs4_state_find_open_context(state);
  1243. if (IS_ERR(ctx))
  1244. return -EAGAIN;
  1245. ret = nfs4_do_open_reclaim(ctx, state);
  1246. put_nfs_open_context(ctx);
  1247. return ret;
  1248. }
  1249. static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
  1250. {
  1251. struct nfs4_opendata *opendata;
  1252. int ret;
  1253. opendata = nfs4_open_recoverdata_alloc(ctx, state,
  1254. NFS4_OPEN_CLAIM_DELEG_CUR_FH);
  1255. if (IS_ERR(opendata))
  1256. return PTR_ERR(opendata);
  1257. nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
  1258. ret = nfs4_open_recover(opendata, state);
  1259. nfs4_opendata_put(opendata);
  1260. return ret;
  1261. }
  1262. int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
  1263. {
  1264. struct nfs4_exception exception = { };
  1265. struct nfs_server *server = NFS_SERVER(state->inode);
  1266. int err;
  1267. do {
  1268. err = _nfs4_open_delegation_recall(ctx, state, stateid);
  1269. switch (err) {
  1270. case 0:
  1271. case -ENOENT:
  1272. case -ESTALE:
  1273. goto out;
  1274. case -NFS4ERR_BADSESSION:
  1275. case -NFS4ERR_BADSLOT:
  1276. case -NFS4ERR_BAD_HIGH_SLOT:
  1277. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  1278. case -NFS4ERR_DEADSESSION:
  1279. set_bit(NFS_DELEGATED_STATE, &state->flags);
  1280. nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
  1281. err = -EAGAIN;
  1282. goto out;
  1283. case -NFS4ERR_STALE_CLIENTID:
  1284. case -NFS4ERR_STALE_STATEID:
  1285. set_bit(NFS_DELEGATED_STATE, &state->flags);
  1286. case -NFS4ERR_EXPIRED:
  1287. /* Don't recall a delegation if it was lost */
  1288. nfs4_schedule_lease_recovery(server->nfs_client);
  1289. err = -EAGAIN;
  1290. goto out;
  1291. case -NFS4ERR_DELEG_REVOKED:
  1292. case -NFS4ERR_ADMIN_REVOKED:
  1293. case -NFS4ERR_BAD_STATEID:
  1294. nfs_inode_find_state_and_recover(state->inode,
  1295. stateid);
  1296. nfs4_schedule_stateid_recovery(server, state);
  1297. case -ENOMEM:
  1298. err = 0;
  1299. goto out;
  1300. case -NFS4ERR_DELAY:
  1301. case -NFS4ERR_GRACE:
  1302. set_bit(NFS_DELEGATED_STATE, &state->flags);
  1303. ssleep(1);
  1304. err = -EAGAIN;
  1305. goto out;
  1306. }
  1307. set_bit(NFS_DELEGATED_STATE, &state->flags);
  1308. err = nfs4_handle_exception(server, err, &exception);
  1309. } while (exception.retry);
  1310. out:
  1311. return err;
  1312. }
  1313. static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
  1314. {
  1315. struct nfs4_opendata *data = calldata;
  1316. data->rpc_status = task->tk_status;
  1317. if (data->rpc_status == 0) {
  1318. nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
  1319. nfs_confirm_seqid(&data->owner->so_seqid, 0);
  1320. renew_lease(data->o_res.server, data->timestamp);
  1321. data->rpc_done = 1;
  1322. }
  1323. }
  1324. static void nfs4_open_confirm_release(void *calldata)
  1325. {
  1326. struct nfs4_opendata *data = calldata;
  1327. struct nfs4_state *state = NULL;
  1328. /* If this request hasn't been cancelled, do nothing */
  1329. if (data->cancelled == 0)
  1330. goto out_free;
  1331. /* In case of error, no cleanup! */
  1332. if (!data->rpc_done)
  1333. goto out_free;
  1334. state = nfs4_opendata_to_nfs4_state(data);
  1335. if (!IS_ERR(state))
  1336. nfs4_close_state(state, data->o_arg.fmode);
  1337. out_free:
  1338. nfs4_opendata_put(data);
  1339. }
  1340. static const struct rpc_call_ops nfs4_open_confirm_ops = {
  1341. .rpc_call_done = nfs4_open_confirm_done,
  1342. .rpc_release = nfs4_open_confirm_release,
  1343. };
  1344. /*
  1345. * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
  1346. */
  1347. static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
  1348. {
  1349. struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
  1350. struct rpc_task *task;
  1351. struct rpc_message msg = {
  1352. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
  1353. .rpc_argp = &data->c_arg,
  1354. .rpc_resp = &data->c_res,
  1355. .rpc_cred = data->owner->so_cred,
  1356. };
  1357. struct rpc_task_setup task_setup_data = {
  1358. .rpc_client = server->client,
  1359. .rpc_message = &msg,
  1360. .callback_ops = &nfs4_open_confirm_ops,
  1361. .callback_data = data,
  1362. .workqueue = nfsiod_workqueue,
  1363. .flags = RPC_TASK_ASYNC,
  1364. };
  1365. int status;
  1366. kref_get(&data->kref);
  1367. data->rpc_done = 0;
  1368. data->rpc_status = 0;
  1369. data->timestamp = jiffies;
  1370. task = rpc_run_task(&task_setup_data);
  1371. if (IS_ERR(task))
  1372. return PTR_ERR(task);
  1373. status = nfs4_wait_for_completion_rpc_task(task);
  1374. if (status != 0) {
  1375. data->cancelled = 1;
  1376. smp_wmb();
  1377. } else
  1378. status = data->rpc_status;
  1379. rpc_put_task(task);
  1380. return status;
  1381. }
  1382. static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
  1383. {
  1384. struct nfs4_opendata *data = calldata;
  1385. struct nfs4_state_owner *sp = data->owner;
  1386. if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
  1387. goto out_wait;
  1388. /*
  1389. * Check if we still need to send an OPEN call, or if we can use
  1390. * a delegation instead.
  1391. */
  1392. if (data->state != NULL) {
  1393. struct nfs_delegation *delegation;
  1394. if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
  1395. goto out_no_action;
  1396. rcu_read_lock();
  1397. delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
  1398. if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
  1399. can_open_delegated(delegation, data->o_arg.fmode))
  1400. goto unlock_no_action;
  1401. rcu_read_unlock();
  1402. }
  1403. /* Update client id. */
  1404. data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
  1405. if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
  1406. task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
  1407. data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
  1408. nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
  1409. }
  1410. data->timestamp = jiffies;
  1411. if (nfs4_setup_sequence(data->o_arg.server,
  1412. &data->o_arg.seq_args,
  1413. &data->o_res.seq_res,
  1414. task) != 0)
  1415. nfs_release_seqid(data->o_arg.seqid);
  1416. return;
  1417. unlock_no_action:
  1418. rcu_read_unlock();
  1419. out_no_action:
  1420. task->tk_action = NULL;
  1421. out_wait:
  1422. nfs4_sequence_done(task, &data->o_res.seq_res);
  1423. }
  1424. static void nfs4_open_done(struct rpc_task *task, void *calldata)
  1425. {
  1426. struct nfs4_opendata *data = calldata;
  1427. data->rpc_status = task->tk_status;
  1428. if (!nfs4_sequence_done(task, &data->o_res.seq_res))
  1429. return;
  1430. if (task->tk_status == 0) {
  1431. if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
  1432. switch (data->o_res.f_attr->mode & S_IFMT) {
  1433. case S_IFREG:
  1434. break;
  1435. case S_IFLNK:
  1436. data->rpc_status = -ELOOP;
  1437. break;
  1438. case S_IFDIR:
  1439. data->rpc_status = -EISDIR;
  1440. break;
  1441. default:
  1442. data->rpc_status = -ENOTDIR;
  1443. }
  1444. }
  1445. renew_lease(data->o_res.server, data->timestamp);
  1446. if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
  1447. nfs_confirm_seqid(&data->owner->so_seqid, 0);
  1448. }
  1449. data->rpc_done = 1;
  1450. }
  1451. static void nfs4_open_release(void *calldata)
  1452. {
  1453. struct nfs4_opendata *data = calldata;
  1454. struct nfs4_state *state = NULL;
  1455. /* If this request hasn't been cancelled, do nothing */
  1456. if (data->cancelled == 0)
  1457. goto out_free;
  1458. /* In case of error, no cleanup! */
  1459. if (data->rpc_status != 0 || !data->rpc_done)
  1460. goto out_free;
  1461. /* In case we need an open_confirm, no cleanup! */
  1462. if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
  1463. goto out_free;
  1464. state = nfs4_opendata_to_nfs4_state(data);
  1465. if (!IS_ERR(state))
  1466. nfs4_close_state(state, data->o_arg.fmode);
  1467. out_free:
  1468. nfs4_opendata_put(data);
  1469. }
  1470. static const struct rpc_call_ops nfs4_open_ops = {
  1471. .rpc_call_prepare = nfs4_open_prepare,
  1472. .rpc_call_done = nfs4_open_done,
  1473. .rpc_release = nfs4_open_release,
  1474. };
  1475. static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
  1476. {
  1477. struct inode *dir = data->dir->d_inode;
  1478. struct nfs_server *server = NFS_SERVER(dir);
  1479. struct nfs_openargs *o_arg = &data->o_arg;
  1480. struct nfs_openres *o_res = &data->o_res;
  1481. struct rpc_task *task;
  1482. struct rpc_message msg = {
  1483. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
  1484. .rpc_argp = o_arg,
  1485. .rpc_resp = o_res,
  1486. .rpc_cred = data->owner->so_cred,
  1487. };
  1488. struct rpc_task_setup task_setup_data = {
  1489. .rpc_client = server->client,
  1490. .rpc_message = &msg,
  1491. .callback_ops = &nfs4_open_ops,
  1492. .callback_data = data,
  1493. .workqueue = nfsiod_workqueue,
  1494. .flags = RPC_TASK_ASYNC,
  1495. };
  1496. int status;
  1497. nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
  1498. kref_get(&data->kref);
  1499. data->rpc_done = 0;
  1500. data->rpc_status = 0;
  1501. data->cancelled = 0;
  1502. if (isrecover)
  1503. nfs4_set_sequence_privileged(&o_arg->seq_args);
  1504. task = rpc_run_task(&task_setup_data);
  1505. if (IS_ERR(task))
  1506. return PTR_ERR(task);
  1507. status = nfs4_wait_for_completion_rpc_task(task);
  1508. if (status != 0) {
  1509. data->cancelled = 1;
  1510. smp_wmb();
  1511. } else
  1512. status = data->rpc_status;
  1513. rpc_put_task(task);
  1514. return status;
  1515. }
  1516. static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
  1517. {
  1518. struct inode *dir = data->dir->d_inode;
  1519. struct nfs_openres *o_res = &data->o_res;
  1520. int status;
  1521. status = nfs4_run_open_task(data, 1);
  1522. if (status != 0 || !data->rpc_done)
  1523. return status;
  1524. nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
  1525. if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
  1526. status = _nfs4_proc_open_confirm(data);
  1527. if (status != 0)
  1528. return status;
  1529. }
  1530. return status;
  1531. }
  1532. static int nfs4_opendata_access(struct rpc_cred *cred,
  1533. struct nfs4_opendata *opendata,
  1534. struct nfs4_state *state, fmode_t fmode,
  1535. int openflags)
  1536. {
  1537. struct nfs_access_entry cache;
  1538. u32 mask;
  1539. /* access call failed or for some reason the server doesn't
  1540. * support any access modes -- defer access call until later */
  1541. if (opendata->o_res.access_supported == 0)
  1542. return 0;
  1543. mask = 0;
  1544. /* don't check MAY_WRITE - a newly created file may not have
  1545. * write mode bits, but POSIX allows the creating process to write.
  1546. * use openflags to check for exec, because fmode won't
  1547. * always have FMODE_EXEC set when file open for exec. */
  1548. if (openflags & __FMODE_EXEC) {
  1549. /* ONLY check for exec rights */
  1550. mask = MAY_EXEC;
  1551. } else if (fmode & FMODE_READ)
  1552. mask = MAY_READ;
  1553. cache.cred = cred;
  1554. cache.jiffies = jiffies;
  1555. nfs_access_set_mask(&cache, opendata->o_res.access_result);
  1556. nfs_access_add_cache(state->inode, &cache);
  1557. if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
  1558. return 0;
  1559. /* even though OPEN succeeded, access is denied. Close the file */
  1560. nfs4_close_state(state, fmode);
  1561. return -EACCES;
  1562. }
  1563. /*
  1564. * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
  1565. */
  1566. static int _nfs4_proc_open(struct nfs4_opendata *data)
  1567. {
  1568. struct inode *dir = data->dir->d_inode;
  1569. struct nfs_server *server = NFS_SERVER(dir);
  1570. struct nfs_openargs *o_arg = &data->o_arg;
  1571. struct nfs_openres *o_res = &data->o_res;
  1572. int status;
  1573. status = nfs4_run_open_task(data, 0);
  1574. if (!data->rpc_done)
  1575. return status;
  1576. if (status != 0) {
  1577. if (status == -NFS4ERR_BADNAME &&
  1578. !(o_arg->open_flags & O_CREAT))
  1579. return -ENOENT;
  1580. return status;
  1581. }
  1582. nfs_fattr_map_and_free_names(server, &data->f_attr);
  1583. if (o_arg->open_flags & O_CREAT)
  1584. update_changeattr(dir, &o_res->cinfo);
  1585. if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
  1586. server->caps &= ~NFS_CAP_POSIX_LOCK;
  1587. if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
  1588. status = _nfs4_proc_open_confirm(data);
  1589. if (status != 0)
  1590. return status;
  1591. }
  1592. if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
  1593. _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
  1594. return 0;
  1595. }
  1596. static int nfs4_recover_expired_lease(struct nfs_server *server)
  1597. {
  1598. return nfs4_client_recover_expired_lease(server->nfs_client);
  1599. }
  1600. /*
  1601. * OPEN_EXPIRED:
  1602. * reclaim state on the server after a network partition.
  1603. * Assumes caller holds the appropriate lock
  1604. */
  1605. static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
  1606. {
  1607. struct nfs4_opendata *opendata;
  1608. int ret;
  1609. opendata = nfs4_open_recoverdata_alloc(ctx, state,
  1610. NFS4_OPEN_CLAIM_FH);
  1611. if (IS_ERR(opendata))
  1612. return PTR_ERR(opendata);
  1613. ret = nfs4_open_recover(opendata, state);
  1614. if (ret == -ESTALE)
  1615. d_drop(ctx->dentry);
  1616. nfs4_opendata_put(opendata);
  1617. return ret;
  1618. }
  1619. static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
  1620. {
  1621. struct nfs_server *server = NFS_SERVER(state->inode);
  1622. struct nfs4_exception exception = { };
  1623. int err;
  1624. do {
  1625. err = _nfs4_open_expired(ctx, state);
  1626. if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
  1627. continue;
  1628. switch (err) {
  1629. default:
  1630. goto out;
  1631. case -NFS4ERR_GRACE:
  1632. case -NFS4ERR_DELAY:
  1633. nfs4_handle_exception(server, err, &exception);
  1634. err = 0;
  1635. }
  1636. } while (exception.retry);
  1637. out:
  1638. return err;
  1639. }
  1640. static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
  1641. {
  1642. struct nfs_open_context *ctx;
  1643. int ret;
  1644. ctx = nfs4_state_find_open_context(state);
  1645. if (IS_ERR(ctx))
  1646. return -EAGAIN;
  1647. ret = nfs4_do_open_expired(ctx, state);
  1648. put_nfs_open_context(ctx);
  1649. return ret;
  1650. }
  1651. #if defined(CONFIG_NFS_V4_1)
  1652. static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
  1653. {
  1654. struct nfs_server *server = NFS_SERVER(state->inode);
  1655. nfs4_stateid *stateid = &state->stateid;
  1656. int status;
  1657. /* If a state reset has been done, test_stateid is unneeded */
  1658. if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
  1659. return;
  1660. status = nfs41_test_stateid(server, stateid);
  1661. if (status != NFS_OK) {
  1662. /* Free the stateid unless the server explicitly
  1663. * informs us the stateid is unrecognized. */
  1664. if (status != -NFS4ERR_BAD_STATEID)
  1665. nfs41_free_stateid(server, stateid);
  1666. nfs_remove_bad_delegation(state->inode);
  1667. write_seqlock(&state->seqlock);
  1668. nfs4_stateid_copy(&state->stateid, &state->open_stateid);
  1669. write_sequnlock(&state->seqlock);
  1670. clear_bit(NFS_DELEGATED_STATE, &state->flags);
  1671. }
  1672. }
  1673. /**
  1674. * nfs41_check_open_stateid - possibly free an open stateid
  1675. *
  1676. * @state: NFSv4 state for an inode
  1677. *
  1678. * Returns NFS_OK if recovery for this stateid is now finished.
  1679. * Otherwise a negative NFS4ERR value is returned.
  1680. */
  1681. static int nfs41_check_open_stateid(struct nfs4_state *state)
  1682. {
  1683. struct nfs_server *server = NFS_SERVER(state->inode);
  1684. nfs4_stateid *stateid = &state->open_stateid;
  1685. int status;
  1686. /* If a state reset has been done, test_stateid is unneeded */
  1687. if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
  1688. (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
  1689. (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
  1690. return -NFS4ERR_BAD_STATEID;
  1691. status = nfs41_test_stateid(server, stateid);
  1692. if (status != NFS_OK) {
  1693. /* Free the stateid unless the server explicitly
  1694. * informs us the stateid is unrecognized. */
  1695. if (status != -NFS4ERR_BAD_STATEID)
  1696. nfs41_free_stateid(server, stateid);
  1697. clear_bit(NFS_O_RDONLY_STATE, &state->flags);
  1698. clear_bit(NFS_O_WRONLY_STATE, &state->flags);
  1699. clear_bit(NFS_O_RDWR_STATE, &state->flags);
  1700. }
  1701. return status;
  1702. }
  1703. static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
  1704. {
  1705. int status;
  1706. nfs41_clear_delegation_stateid(state);
  1707. status = nfs41_check_open_stateid(state);
  1708. if (status != NFS_OK)
  1709. status = nfs4_open_expired(sp, state);
  1710. return status;
  1711. }
  1712. #endif
  1713. /*
  1714. * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
  1715. * fields corresponding to attributes that were used to store the verifier.
  1716. * Make sure we clobber those fields in the later setattr call
  1717. */
  1718. static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
  1719. {
  1720. if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
  1721. !(sattr->ia_valid & ATTR_ATIME_SET))
  1722. sattr->ia_valid |= ATTR_ATIME;
  1723. if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
  1724. !(sattr->ia_valid & ATTR_MTIME_SET))
  1725. sattr->ia_valid |= ATTR_MTIME;
  1726. }
  1727. static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
  1728. fmode_t fmode,
  1729. int flags,
  1730. struct nfs4_state **res)
  1731. {
  1732. struct nfs4_state_owner *sp = opendata->owner;
  1733. struct nfs_server *server = sp->so_server;
  1734. struct nfs4_state *state;
  1735. unsigned int seq;
  1736. int ret;
  1737. seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
  1738. ret = _nfs4_proc_open(opendata);
  1739. if (ret != 0)
  1740. goto out;
  1741. state = nfs4_opendata_to_nfs4_state(opendata);
  1742. ret = PTR_ERR(state);
  1743. if (IS_ERR(state))
  1744. goto out;
  1745. if (server->caps & NFS_CAP_POSIX_LOCK)
  1746. set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
  1747. ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
  1748. if (ret != 0)
  1749. goto out;
  1750. if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
  1751. nfs4_schedule_stateid_recovery(server, state);
  1752. *res = state;
  1753. out:
  1754. return ret;
  1755. }
  1756. /*
  1757. * Returns a referenced nfs4_state
  1758. */
  1759. static int _nfs4_do_open(struct inode *dir,
  1760. struct dentry *dentry,
  1761. fmode_t fmode,
  1762. int flags,
  1763. struct iattr *sattr,
  1764. struct rpc_cred *cred,
  1765. struct nfs4_state **res,
  1766. struct nfs4_threshold **ctx_th)
  1767. {
  1768. struct nfs4_state_owner *sp;
  1769. struct nfs4_state *state = NULL;
  1770. struct nfs_server *server = NFS_SERVER(dir);
  1771. struct nfs4_opendata *opendata;
  1772. enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
  1773. int status;
  1774. /* Protect against reboot recovery conflicts */
  1775. status = -ENOMEM;
  1776. sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
  1777. if (sp == NULL) {
  1778. dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
  1779. goto out_err;
  1780. }
  1781. status = nfs4_recover_expired_lease(server);
  1782. if (status != 0)
  1783. goto err_put_state_owner;
  1784. if (dentry->d_inode != NULL)
  1785. nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
  1786. status = -ENOMEM;
  1787. if (dentry->d_inode)
  1788. claim = NFS4_OPEN_CLAIM_FH;
  1789. opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
  1790. claim, GFP_KERNEL);
  1791. if (opendata == NULL)
  1792. goto err_put_state_owner;
  1793. if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
  1794. opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
  1795. if (!opendata->f_attr.mdsthreshold)
  1796. goto err_opendata_put;
  1797. opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
  1798. }
  1799. if (dentry->d_inode != NULL)
  1800. opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
  1801. status = _nfs4_open_and_get_state(opendata, fmode, flags, &state);
  1802. if (status != 0)
  1803. goto err_opendata_put;
  1804. if (opendata->o_arg.open_flags & O_EXCL) {
  1805. nfs4_exclusive_attrset(opendata, sattr);
  1806. nfs_fattr_init(opendata->o_res.f_attr);
  1807. status = nfs4_do_setattr(state->inode, cred,
  1808. opendata->o_res.f_attr, sattr,
  1809. state);
  1810. if (status == 0)
  1811. nfs_setattr_update_inode(state->inode, sattr);
  1812. nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
  1813. }
  1814. if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
  1815. *ctx_th = opendata->f_attr.mdsthreshold;
  1816. else
  1817. kfree(opendata->f_attr.mdsthreshold);
  1818. opendata->f_attr.mdsthreshold = NULL;
  1819. nfs4_opendata_put(opendata);
  1820. nfs4_put_state_owner(sp);
  1821. *res = state;
  1822. return 0;
  1823. err_opendata_put:
  1824. kfree(opendata->f_attr.mdsthreshold);
  1825. nfs4_opendata_put(opendata);
  1826. err_put_state_owner:
  1827. nfs4_put_state_owner(sp);
  1828. out_err:
  1829. *res = NULL;
  1830. return status;
  1831. }
  1832. static struct nfs4_state *nfs4_do_open(struct inode *dir,
  1833. struct dentry *dentry,
  1834. fmode_t fmode,
  1835. int flags,
  1836. struct iattr *sattr,
  1837. struct rpc_cred *cred,
  1838. struct nfs4_threshold **ctx_th)
  1839. {
  1840. struct nfs_server *server = NFS_SERVER(dir);
  1841. struct nfs4_exception exception = { };
  1842. struct nfs4_state *res;
  1843. int status;
  1844. fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
  1845. do {
  1846. status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
  1847. &res, ctx_th);
  1848. if (status == 0)
  1849. break;
  1850. /* NOTE: BAD_SEQID means the server and client disagree about the
  1851. * book-keeping w.r.t. state-changing operations
  1852. * (OPEN/CLOSE/LOCK/LOCKU...)
  1853. * It is actually a sign of a bug on the client or on the server.
  1854. *
  1855. * If we receive a BAD_SEQID error in the particular case of
  1856. * doing an OPEN, we assume that nfs_increment_open_seqid() will
  1857. * have unhashed the old state_owner for us, and that we can
  1858. * therefore safely retry using a new one. We should still warn
  1859. * the user though...
  1860. */
  1861. if (status == -NFS4ERR_BAD_SEQID) {
  1862. pr_warn_ratelimited("NFS: v4 server %s "
  1863. " returned a bad sequence-id error!\n",
  1864. NFS_SERVER(dir)->nfs_client->cl_hostname);
  1865. exception.retry = 1;
  1866. continue;
  1867. }
  1868. /*
  1869. * BAD_STATEID on OPEN means that the server cancelled our
  1870. * state before it received the OPEN_CONFIRM.
  1871. * Recover by retrying the request as per the discussion
  1872. * on Page 181 of RFC3530.
  1873. */
  1874. if (status == -NFS4ERR_BAD_STATEID) {
  1875. exception.retry = 1;
  1876. continue;
  1877. }
  1878. if (status == -EAGAIN) {
  1879. /* We must have found a delegation */
  1880. exception.retry = 1;
  1881. continue;
  1882. }
  1883. if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
  1884. continue;
  1885. res = ERR_PTR(nfs4_handle_exception(server,
  1886. status, &exception));
  1887. } while (exception.retry);
  1888. return res;
  1889. }
  1890. static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
  1891. struct nfs_fattr *fattr, struct iattr *sattr,
  1892. struct nfs4_state *state)
  1893. {
  1894. struct nfs_server *server = NFS_SERVER(inode);
  1895. struct nfs_setattrargs arg = {
  1896. .fh = NFS_FH(inode),
  1897. .iap = sattr,
  1898. .server = server,
  1899. .bitmask = server->attr_bitmask,
  1900. };
  1901. struct nfs_setattrres res = {
  1902. .fattr = fattr,
  1903. .server = server,
  1904. };
  1905. struct rpc_message msg = {
  1906. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
  1907. .rpc_argp = &arg,
  1908. .rpc_resp = &res,
  1909. .rpc_cred = cred,
  1910. };
  1911. unsigned long timestamp = jiffies;
  1912. int status;
  1913. nfs_fattr_init(fattr);
  1914. if (state != NULL && nfs4_valid_open_stateid(state)) {
  1915. struct nfs_lockowner lockowner = {
  1916. .l_owner = current->files,
  1917. .l_pid = current->tgid,
  1918. };
  1919. nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
  1920. &lockowner);
  1921. } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
  1922. FMODE_WRITE)) {
  1923. /* Use that stateid */
  1924. } else
  1925. nfs4_stateid_copy(&arg.stateid, &zero_stateid);
  1926. status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
  1927. if (status == 0 && state != NULL)
  1928. renew_lease(server, timestamp);
  1929. return status;
  1930. }
  1931. static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
  1932. struct nfs_fattr *fattr, struct iattr *sattr,
  1933. struct nfs4_state *state)
  1934. {
  1935. struct nfs_server *server = NFS_SERVER(inode);
  1936. struct nfs4_exception exception = {
  1937. .state = state,
  1938. .inode = inode,
  1939. };
  1940. int err;
  1941. do {
  1942. err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
  1943. switch (err) {
  1944. case -NFS4ERR_OPENMODE:
  1945. if (state && !(state->state & FMODE_WRITE)) {
  1946. err = -EBADF;
  1947. if (sattr->ia_valid & ATTR_OPEN)
  1948. err = -EACCES;
  1949. goto out;
  1950. }
  1951. }
  1952. err = nfs4_handle_exception(server, err, &exception);
  1953. } while (exception.retry);
  1954. out:
  1955. return err;
  1956. }
  1957. struct nfs4_closedata {
  1958. struct inode *inode;
  1959. struct nfs4_state *state;
  1960. struct nfs_closeargs arg;
  1961. struct nfs_closeres res;
  1962. struct nfs_fattr fattr;
  1963. unsigned long timestamp;
  1964. bool roc;
  1965. u32 roc_barrier;
  1966. };
  1967. static void nfs4_free_closedata(void *data)
  1968. {
  1969. struct nfs4_closedata *calldata = data;
  1970. struct nfs4_state_owner *sp = calldata->state->owner;
  1971. struct super_block *sb = calldata->state->inode->i_sb;
  1972. if (calldata->roc)
  1973. pnfs_roc_release(calldata->state->inode);
  1974. nfs4_put_open_state(calldata->state);
  1975. nfs_free_seqid(calldata->arg.seqid);
  1976. nfs4_put_state_owner(sp);
  1977. nfs_sb_deactive(sb);
  1978. kfree(calldata);
  1979. }
  1980. static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
  1981. fmode_t fmode)
  1982. {
  1983. spin_lock(&state->owner->so_lock);
  1984. if (!(fmode & FMODE_READ))
  1985. clear_bit(NFS_O_RDONLY_STATE, &state->flags);
  1986. if (!(fmode & FMODE_WRITE))
  1987. clear_bit(NFS_O_WRONLY_STATE, &state->flags);
  1988. clear_bit(NFS_O_RDWR_STATE, &state->flags);
  1989. spin_unlock(&state->owner->so_lock);
  1990. }
  1991. static void nfs4_close_done(struct rpc_task *task, void *data)
  1992. {
  1993. struct nfs4_closedata *calldata = data;
  1994. struct nfs4_state *state = calldata->state;
  1995. struct nfs_server *server = NFS_SERVER(calldata->inode);
  1996. dprintk("%s: begin!\n", __func__);
  1997. if (!nfs4_sequence_done(task, &calldata->res.seq_res))
  1998. return;
  1999. /* hmm. we are done with the inode, and in the process of freeing
  2000. * the state_owner. we keep this around to process errors
  2001. */
  2002. switch (task->tk_status) {
  2003. case 0:
  2004. if (calldata->roc)
  2005. pnfs_roc_set_barrier(state->inode,
  2006. calldata->roc_barrier);
  2007. nfs_set_open_stateid(state, &calldata->res.stateid, 0);
  2008. renew_lease(server, calldata->timestamp);
  2009. nfs4_close_clear_stateid_flags(state,
  2010. calldata->arg.fmode);
  2011. break;
  2012. case -NFS4ERR_STALE_STATEID:
  2013. case -NFS4ERR_OLD_STATEID:
  2014. case -NFS4ERR_BAD_STATEID:
  2015. case -NFS4ERR_EXPIRED:
  2016. if (calldata->arg.fmode == 0)
  2017. break;
  2018. default:
  2019. if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
  2020. rpc_restart_call_prepare(task);
  2021. }
  2022. nfs_release_seqid(calldata->arg.seqid);
  2023. nfs_refresh_inode(calldata->inode, calldata->res.fattr);
  2024. dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
  2025. }
  2026. static void nfs4_close_prepare(struct rpc_task *task, void *data)
  2027. {
  2028. struct nfs4_closedata *calldata = data;
  2029. struct nfs4_state *state = calldata->state;
  2030. struct inode *inode = calldata->inode;
  2031. int call_close = 0;
  2032. dprintk("%s: begin!\n", __func__);
  2033. if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
  2034. goto out_wait;
  2035. task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
  2036. calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
  2037. spin_lock(&state->owner->so_lock);
  2038. /* Calculate the change in open mode */
  2039. if (state->n_rdwr == 0) {
  2040. if (state->n_rdonly == 0) {
  2041. call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
  2042. call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
  2043. calldata->arg.fmode &= ~FMODE_READ;
  2044. }
  2045. if (state->n_wronly == 0) {
  2046. call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
  2047. call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
  2048. calldata->arg.fmode &= ~FMODE_WRITE;
  2049. }
  2050. }
  2051. if (!nfs4_valid_open_stateid(state))
  2052. call_close = 0;
  2053. spin_unlock(&state->owner->so_lock);
  2054. if (!call_close) {
  2055. /* Note: exit _without_ calling nfs4_close_done */
  2056. goto out_no_action;
  2057. }
  2058. if (calldata->arg.fmode == 0) {
  2059. task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
  2060. if (calldata->roc &&
  2061. pnfs_roc_drain(inode, &calldata->roc_barrier, task))
  2062. goto out_wait;
  2063. }
  2064. nfs_fattr_init(calldata->res.fattr);
  2065. calldata->timestamp = jiffies;
  2066. if (nfs4_setup_sequence(NFS_SERVER(inode),
  2067. &calldata->arg.seq_args,
  2068. &calldata->res.seq_res,
  2069. task) != 0)
  2070. nfs_release_seqid(calldata->arg.seqid);
  2071. dprintk("%s: done!\n", __func__);
  2072. return;
  2073. out_no_action:
  2074. task->tk_action = NULL;
  2075. out_wait:
  2076. nfs4_sequence_done(task, &calldata->res.seq_res);
  2077. }
  2078. static const struct rpc_call_ops nfs4_close_ops = {
  2079. .rpc_call_prepare = nfs4_close_prepare,
  2080. .rpc_call_done = nfs4_close_done,
  2081. .rpc_release = nfs4_free_closedata,
  2082. };
  2083. /*
  2084. * It is possible for data to be read/written from a mem-mapped file
  2085. * after the sys_close call (which hits the vfs layer as a flush).
  2086. * This means that we can't safely call nfsv4 close on a file until
  2087. * the inode is cleared. This in turn means that we are not good
  2088. * NFSv4 citizens - we do not indicate to the server to update the file's
  2089. * share state even when we are done with one of the three share
  2090. * stateid's in the inode.
  2091. *
  2092. * NOTE: Caller must be holding the sp->so_owner semaphore!
  2093. */
  2094. int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
  2095. {
  2096. struct nfs_server *server = NFS_SERVER(state->inode);
  2097. struct nfs4_closedata *calldata;
  2098. struct nfs4_state_owner *sp = state->owner;
  2099. struct rpc_task *task;
  2100. struct rpc_message msg = {
  2101. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
  2102. .rpc_cred = state->owner->so_cred,
  2103. };
  2104. struct rpc_task_setup task_setup_data = {
  2105. .rpc_client = server->client,
  2106. .rpc_message = &msg,
  2107. .callback_ops = &nfs4_close_ops,
  2108. .workqueue = nfsiod_workqueue,
  2109. .flags = RPC_TASK_ASYNC,
  2110. };
  2111. int status = -ENOMEM;
  2112. calldata = kzalloc(sizeof(*calldata), gfp_mask);
  2113. if (calldata == NULL)
  2114. goto out;
  2115. nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
  2116. calldata->inode = state->inode;
  2117. calldata->state = state;
  2118. calldata->arg.fh = NFS_FH(state->inode);
  2119. calldata->arg.stateid = &state->open_stateid;
  2120. /* Serialization for the sequence id */
  2121. calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
  2122. if (calldata->arg.seqid == NULL)
  2123. goto out_free_calldata;
  2124. calldata->arg.fmode = 0;
  2125. calldata->arg.bitmask = server->cache_consistency_bitmask;
  2126. calldata->res.fattr = &calldata->fattr;
  2127. calldata->res.seqid = calldata->arg.seqid;
  2128. calldata->res.server = server;
  2129. calldata->roc = pnfs_roc(state->inode);
  2130. nfs_sb_active(calldata->inode->i_sb);
  2131. msg.rpc_argp = &calldata->arg;
  2132. msg.rpc_resp = &calldata->res;
  2133. task_setup_data.callback_data = calldata;
  2134. task = rpc_run_task(&task_setup_data);
  2135. if (IS_ERR(task))
  2136. return PTR_ERR(task);
  2137. status = 0;
  2138. if (wait)
  2139. status = rpc_wait_for_completion_task(task);
  2140. rpc_put_task(task);
  2141. return status;
  2142. out_free_calldata:
  2143. kfree(calldata);
  2144. out:
  2145. nfs4_put_open_state(state);
  2146. nfs4_put_state_owner(sp);
  2147. return status;
  2148. }
  2149. static struct inode *
  2150. nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
  2151. {
  2152. struct nfs4_state *state;
  2153. /* Protect against concurrent sillydeletes */
  2154. state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
  2155. ctx->cred, &ctx->mdsthreshold);
  2156. if (IS_ERR(state))
  2157. return ERR_CAST(state);
  2158. ctx->state = state;
  2159. return igrab(state->inode);
  2160. }
  2161. static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
  2162. {
  2163. if (ctx->state == NULL)
  2164. return;
  2165. if (is_sync)
  2166. nfs4_close_sync(ctx->state, ctx->mode);
  2167. else
  2168. nfs4_close_state(ctx->state, ctx->mode);
  2169. }
  2170. static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
  2171. {
  2172. struct nfs4_server_caps_arg args = {
  2173. .fhandle = fhandle,
  2174. };
  2175. struct nfs4_server_caps_res res = {};
  2176. struct rpc_message msg = {
  2177. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
  2178. .rpc_argp = &args,
  2179. .rpc_resp = &res,
  2180. };
  2181. int status;
  2182. status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  2183. if (status == 0) {
  2184. memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
  2185. server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
  2186. NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
  2187. NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
  2188. NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
  2189. NFS_CAP_CTIME|NFS_CAP_MTIME);
  2190. if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
  2191. server->caps |= NFS_CAP_ACLS;
  2192. if (res.has_links != 0)
  2193. server->caps |= NFS_CAP_HARDLINKS;
  2194. if (res.has_symlinks != 0)
  2195. server->caps |= NFS_CAP_SYMLINKS;
  2196. if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
  2197. server->caps |= NFS_CAP_FILEID;
  2198. if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
  2199. server->caps |= NFS_CAP_MODE;
  2200. if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
  2201. server->caps |= NFS_CAP_NLINK;
  2202. if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
  2203. server->caps |= NFS_CAP_OWNER;
  2204. if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
  2205. server->caps |= NFS_CAP_OWNER_GROUP;
  2206. if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
  2207. server->caps |= NFS_CAP_ATIME;
  2208. if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
  2209. server->caps |= NFS_CAP_CTIME;
  2210. if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
  2211. server->caps |= NFS_CAP_MTIME;
  2212. memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
  2213. server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
  2214. server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
  2215. server->acl_bitmask = res.acl_bitmask;
  2216. server->fh_expire_type = res.fh_expire_type;
  2217. }
  2218. return status;
  2219. }
  2220. int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
  2221. {
  2222. struct nfs4_exception exception = { };
  2223. int err;
  2224. do {
  2225. err = nfs4_handle_exception(server,
  2226. _nfs4_server_capabilities(server, fhandle),
  2227. &exception);
  2228. } while (exception.retry);
  2229. return err;
  2230. }
  2231. static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
  2232. struct nfs_fsinfo *info)
  2233. {
  2234. struct nfs4_lookup_root_arg args = {
  2235. .bitmask = nfs4_fattr_bitmap,
  2236. };
  2237. struct nfs4_lookup_res res = {
  2238. .server = server,
  2239. .fattr = info->fattr,
  2240. .fh = fhandle,
  2241. };
  2242. struct rpc_message msg = {
  2243. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
  2244. .rpc_argp = &args,
  2245. .rpc_resp = &res,
  2246. };
  2247. nfs_fattr_init(info->fattr);
  2248. return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  2249. }
  2250. static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
  2251. struct nfs_fsinfo *info)
  2252. {
  2253. struct nfs4_exception exception = { };
  2254. int err;
  2255. do {
  2256. err = _nfs4_lookup_root(server, fhandle, info);
  2257. switch (err) {
  2258. case 0:
  2259. case -NFS4ERR_WRONGSEC:
  2260. goto out;
  2261. default:
  2262. err = nfs4_handle_exception(server, err, &exception);
  2263. }
  2264. } while (exception.retry);
  2265. out:
  2266. return err;
  2267. }
  2268. static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
  2269. struct nfs_fsinfo *info, rpc_authflavor_t flavor)
  2270. {
  2271. struct rpc_auth *auth;
  2272. int ret;
  2273. auth = rpcauth_create(flavor, server->client);
  2274. if (IS_ERR(auth)) {
  2275. ret = -EIO;
  2276. goto out;
  2277. }
  2278. ret = nfs4_lookup_root(server, fhandle, info);
  2279. out:
  2280. return ret;
  2281. }
  2282. static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
  2283. struct nfs_fsinfo *info)
  2284. {
  2285. int i, len, status = 0;
  2286. rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
  2287. len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
  2288. if (len < 0)
  2289. return len;
  2290. for (i = 0; i < len; i++) {
  2291. /* AUTH_UNIX is the default flavor if none was specified,
  2292. * thus has already been tried. */
  2293. if (flav_array[i] == RPC_AUTH_UNIX)
  2294. continue;
  2295. status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
  2296. if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
  2297. continue;
  2298. break;
  2299. }
  2300. /*
  2301. * -EACCESS could mean that the user doesn't have correct permissions
  2302. * to access the mount. It could also mean that we tried to mount
  2303. * with a gss auth flavor, but rpc.gssd isn't running. Either way,
  2304. * existing mount programs don't handle -EACCES very well so it should
  2305. * be mapped to -EPERM instead.
  2306. */
  2307. if (status == -EACCES)
  2308. status = -EPERM;
  2309. return status;
  2310. }
  2311. /*
  2312. * get the file handle for the "/" directory on the server
  2313. */
  2314. int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
  2315. struct nfs_fsinfo *info)
  2316. {
  2317. int minor_version = server->nfs_client->cl_minorversion;
  2318. int status = nfs4_lookup_root(server, fhandle, info);
  2319. if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
  2320. /*
  2321. * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
  2322. * by nfs4_map_errors() as this function exits.
  2323. */
  2324. status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
  2325. if (status == 0)
  2326. status = nfs4_server_capabilities(server, fhandle);
  2327. if (status == 0)
  2328. status = nfs4_do_fsinfo(server, fhandle, info);
  2329. return nfs4_map_errors(status);
  2330. }
  2331. static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
  2332. struct nfs_fsinfo *info)
  2333. {
  2334. int error;
  2335. struct nfs_fattr *fattr = info->fattr;
  2336. error = nfs4_server_capabilities(server, mntfh);
  2337. if (error < 0) {
  2338. dprintk("nfs4_get_root: getcaps error = %d\n", -error);
  2339. return error;
  2340. }
  2341. error = nfs4_proc_getattr(server, mntfh, fattr);
  2342. if (error < 0) {
  2343. dprintk("nfs4_get_root: getattr error = %d\n", -error);
  2344. return error;
  2345. }
  2346. if (fattr->valid & NFS_ATTR_FATTR_FSID &&
  2347. !nfs_fsid_equal(&server->fsid, &fattr->fsid))
  2348. memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
  2349. return error;
  2350. }
  2351. /*
  2352. * Get locations and (maybe) other attributes of a referral.
  2353. * Note that we'll actually follow the referral later when
  2354. * we detect fsid mismatch in inode revalidation
  2355. */
  2356. static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
  2357. const struct qstr *name, struct nfs_fattr *fattr,
  2358. struct nfs_fh *fhandle)
  2359. {
  2360. int status = -ENOMEM;
  2361. struct page *page = NULL;
  2362. struct nfs4_fs_locations *locations = NULL;
  2363. page = alloc_page(GFP_KERNEL);
  2364. if (page == NULL)
  2365. goto out;
  2366. locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
  2367. if (locations == NULL)
  2368. goto out;
  2369. status = nfs4_proc_fs_locations(client, dir, name, locations, page);
  2370. if (status != 0)
  2371. goto out;
  2372. /* Make sure server returned a different fsid for the referral */
  2373. if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
  2374. dprintk("%s: server did not return a different fsid for"
  2375. " a referral at %s\n", __func__, name->name);
  2376. status = -EIO;
  2377. goto out;
  2378. }
  2379. /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
  2380. nfs_fixup_referral_attributes(&locations->fattr);
  2381. /* replace the lookup nfs_fattr with the locations nfs_fattr */
  2382. memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
  2383. memset(fhandle, 0, sizeof(struct nfs_fh));
  2384. out:
  2385. if (page)
  2386. __free_page(page);
  2387. kfree(locations);
  2388. return status;
  2389. }
  2390. static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
  2391. {
  2392. struct nfs4_getattr_arg args = {
  2393. .fh = fhandle,
  2394. .bitmask = server->attr_bitmask,
  2395. };
  2396. struct nfs4_getattr_res res = {
  2397. .fattr = fattr,
  2398. .server = server,
  2399. };
  2400. struct rpc_message msg = {
  2401. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
  2402. .rpc_argp = &args,
  2403. .rpc_resp = &res,
  2404. };
  2405. nfs_fattr_init(fattr);
  2406. return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  2407. }
  2408. static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
  2409. {
  2410. struct nfs4_exception exception = { };
  2411. int err;
  2412. do {
  2413. err = nfs4_handle_exception(server,
  2414. _nfs4_proc_getattr(server, fhandle, fattr),
  2415. &exception);
  2416. } while (exception.retry);
  2417. return err;
  2418. }
  2419. /*
  2420. * The file is not closed if it is opened due to the a request to change
  2421. * the size of the file. The open call will not be needed once the
  2422. * VFS layer lookup-intents are implemented.
  2423. *
  2424. * Close is called when the inode is destroyed.
  2425. * If we haven't opened the file for O_WRONLY, we
  2426. * need to in the size_change case to obtain a stateid.
  2427. *
  2428. * Got race?
  2429. * Because OPEN is always done by name in nfsv4, it is
  2430. * possible that we opened a different file by the same
  2431. * name. We can recognize this race condition, but we
  2432. * can't do anything about it besides returning an error.
  2433. *
  2434. * This will be fixed with VFS changes (lookup-intent).
  2435. */
  2436. static int
  2437. nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
  2438. struct iattr *sattr)
  2439. {
  2440. struct inode *inode = dentry->d_inode;
  2441. struct rpc_cred *cred = NULL;
  2442. struct nfs4_state *state = NULL;
  2443. int status;
  2444. if (pnfs_ld_layoutret_on_setattr(inode))
  2445. pnfs_commit_and_return_layout(inode);
  2446. nfs_fattr_init(fattr);
  2447. /* Deal with open(O_TRUNC) */
  2448. if (sattr->ia_valid & ATTR_OPEN)
  2449. sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
  2450. /* Optimization: if the end result is no change, don't RPC */
  2451. if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
  2452. return 0;
  2453. /* Search for an existing open(O_WRITE) file */
  2454. if (sattr->ia_valid & ATTR_FILE) {
  2455. struct nfs_open_context *ctx;
  2456. ctx = nfs_file_open_context(sattr->ia_file);
  2457. if (ctx) {
  2458. cred = ctx->cred;
  2459. state = ctx->state;
  2460. }
  2461. }
  2462. status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
  2463. if (status == 0)
  2464. nfs_setattr_update_inode(inode, sattr);
  2465. return status;
  2466. }
  2467. static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
  2468. const struct qstr *name, struct nfs_fh *fhandle,
  2469. struct nfs_fattr *fattr)
  2470. {
  2471. struct nfs_server *server = NFS_SERVER(dir);
  2472. int status;
  2473. struct nfs4_lookup_arg args = {
  2474. .bitmask = server->attr_bitmask,
  2475. .dir_fh = NFS_FH(dir),
  2476. .name = name,
  2477. };
  2478. struct nfs4_lookup_res res = {
  2479. .server = server,
  2480. .fattr = fattr,
  2481. .fh = fhandle,
  2482. };
  2483. struct rpc_message msg = {
  2484. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
  2485. .rpc_argp = &args,
  2486. .rpc_resp = &res,
  2487. };
  2488. nfs_fattr_init(fattr);
  2489. dprintk("NFS call lookup %s\n", name->name);
  2490. status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
  2491. dprintk("NFS reply lookup: %d\n", status);
  2492. return status;
  2493. }
  2494. static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
  2495. {
  2496. fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
  2497. NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
  2498. fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
  2499. fattr->nlink = 2;
  2500. }
  2501. static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
  2502. struct qstr *name, struct nfs_fh *fhandle,
  2503. struct nfs_fattr *fattr)
  2504. {
  2505. struct nfs4_exception exception = { };
  2506. struct rpc_clnt *client = *clnt;
  2507. int err;
  2508. do {
  2509. err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
  2510. switch (err) {
  2511. case -NFS4ERR_BADNAME:
  2512. err = -ENOENT;
  2513. goto out;
  2514. case -NFS4ERR_MOVED:
  2515. err = nfs4_get_referral(client, dir, name, fattr, fhandle);
  2516. goto out;
  2517. case -NFS4ERR_WRONGSEC:
  2518. err = -EPERM;
  2519. if (client != *clnt)
  2520. goto out;
  2521. client = nfs4_create_sec_client(client, dir, name);
  2522. if (IS_ERR(client))
  2523. return PTR_ERR(client);
  2524. exception.retry = 1;
  2525. break;
  2526. default:
  2527. err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
  2528. }
  2529. } while (exception.retry);
  2530. out:
  2531. if (err == 0)
  2532. *clnt = client;
  2533. else if (client != *clnt)
  2534. rpc_shutdown_client(client);
  2535. return err;
  2536. }
  2537. static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
  2538. struct nfs_fh *fhandle, struct nfs_fattr *fattr)
  2539. {
  2540. int status;
  2541. struct rpc_clnt *client = NFS_CLIENT(dir);
  2542. status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
  2543. if (client != NFS_CLIENT(dir)) {
  2544. rpc_shutdown_client(client);
  2545. nfs_fixup_secinfo_attributes(fattr);
  2546. }
  2547. return status;
  2548. }
  2549. struct rpc_clnt *
  2550. nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
  2551. struct nfs_fh *fhandle, struct nfs_fattr *fattr)
  2552. {
  2553. int status;
  2554. struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
  2555. status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
  2556. if (status < 0) {
  2557. rpc_shutdown_client(client);
  2558. return ERR_PTR(status);
  2559. }
  2560. return client;
  2561. }
  2562. static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
  2563. {
  2564. struct nfs_server *server = NFS_SERVER(inode);
  2565. struct nfs4_accessargs args = {
  2566. .fh = NFS_FH(inode),
  2567. .bitmask = server->cache_consistency_bitmask,
  2568. };
  2569. struct nfs4_accessres res = {
  2570. .server = server,
  2571. };
  2572. struct rpc_message msg = {
  2573. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
  2574. .rpc_argp = &args,
  2575. .rpc_resp = &res,
  2576. .rpc_cred = entry->cred,
  2577. };
  2578. int mode = entry->mask;
  2579. int status;
  2580. /*
  2581. * Determine which access bits we want to ask for...
  2582. */
  2583. if (mode & MAY_READ)
  2584. args.access |= NFS4_ACCESS_READ;
  2585. if (S_ISDIR(inode->i_mode)) {
  2586. if (mode & MAY_WRITE)
  2587. args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
  2588. if (mode & MAY_EXEC)
  2589. args.access |= NFS4_ACCESS_LOOKUP;
  2590. } else {
  2591. if (mode & MAY_WRITE)
  2592. args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
  2593. if (mode & MAY_EXEC)
  2594. args.access |= NFS4_ACCESS_EXECUTE;
  2595. }
  2596. res.fattr = nfs_alloc_fattr();
  2597. if (res.fattr == NULL)
  2598. return -ENOMEM;
  2599. status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  2600. if (!status) {
  2601. nfs_access_set_mask(entry, res.access);
  2602. nfs_refresh_inode(inode, res.fattr);
  2603. }
  2604. nfs_free_fattr(res.fattr);
  2605. return status;
  2606. }
  2607. static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
  2608. {
  2609. struct nfs4_exception exception = { };
  2610. int err;
  2611. do {
  2612. err = nfs4_handle_exception(NFS_SERVER(inode),
  2613. _nfs4_proc_access(inode, entry),
  2614. &exception);
  2615. } while (exception.retry);
  2616. return err;
  2617. }
  2618. /*
  2619. * TODO: For the time being, we don't try to get any attributes
  2620. * along with any of the zero-copy operations READ, READDIR,
  2621. * READLINK, WRITE.
  2622. *
  2623. * In the case of the first three, we want to put the GETATTR
  2624. * after the read-type operation -- this is because it is hard
  2625. * to predict the length of a GETATTR response in v4, and thus
  2626. * align the READ data correctly. This means that the GETATTR
  2627. * may end up partially falling into the page cache, and we should
  2628. * shift it into the 'tail' of the xdr_buf before processing.
  2629. * To do this efficiently, we need to know the total length
  2630. * of data received, which doesn't seem to be available outside
  2631. * of the RPC layer.
  2632. *
  2633. * In the case of WRITE, we also want to put the GETATTR after
  2634. * the operation -- in this case because we want to make sure
  2635. * we get the post-operation mtime and size.
  2636. *
  2637. * Both of these changes to the XDR layer would in fact be quite
  2638. * minor, but I decided to leave them for a subsequent patch.
  2639. */
  2640. static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
  2641. unsigned int pgbase, unsigned int pglen)
  2642. {
  2643. struct nfs4_readlink args = {
  2644. .fh = NFS_FH(inode),
  2645. .pgbase = pgbase,
  2646. .pglen = pglen,
  2647. .pages = &page,
  2648. };
  2649. struct nfs4_readlink_res res;
  2650. struct rpc_message msg = {
  2651. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
  2652. .rpc_argp = &args,
  2653. .rpc_resp = &res,
  2654. };
  2655. return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
  2656. }
  2657. static int nfs4_proc_readlink(struct inode *inode, struct page *page,
  2658. unsigned int pgbase, unsigned int pglen)
  2659. {
  2660. struct nfs4_exception exception = { };
  2661. int err;
  2662. do {
  2663. err = nfs4_handle_exception(NFS_SERVER(inode),
  2664. _nfs4_proc_readlink(inode, page, pgbase, pglen),
  2665. &exception);
  2666. } while (exception.retry);
  2667. return err;
  2668. }
  2669. /*
  2670. * This is just for mknod. open(O_CREAT) will always do ->open_context().
  2671. */
  2672. static int
  2673. nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
  2674. int flags)
  2675. {
  2676. struct nfs_open_context *ctx;
  2677. struct nfs4_state *state;
  2678. int status = 0;
  2679. ctx = alloc_nfs_open_context(dentry, FMODE_READ);
  2680. if (IS_ERR(ctx))
  2681. return PTR_ERR(ctx);
  2682. sattr->ia_mode &= ~current_umask();
  2683. state = nfs4_do_open(dir, dentry, ctx->mode,
  2684. flags, sattr, ctx->cred,
  2685. &ctx->mdsthreshold);
  2686. d_drop(dentry);
  2687. if (IS_ERR(state)) {
  2688. status = PTR_ERR(state);
  2689. goto out;
  2690. }
  2691. d_add(dentry, igrab(state->inode));
  2692. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  2693. ctx->state = state;
  2694. out:
  2695. put_nfs_open_context(ctx);
  2696. return status;
  2697. }
  2698. static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
  2699. {
  2700. struct nfs_server *server = NFS_SERVER(dir);
  2701. struct nfs_removeargs args = {
  2702. .fh = NFS_FH(dir),
  2703. .name = *name,
  2704. };
  2705. struct nfs_removeres res = {
  2706. .server = server,
  2707. };
  2708. struct rpc_message msg = {
  2709. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
  2710. .rpc_argp = &args,
  2711. .rpc_resp = &res,
  2712. };
  2713. int status;
  2714. status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
  2715. if (status == 0)
  2716. update_changeattr(dir, &res.cinfo);
  2717. return status;
  2718. }
  2719. static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
  2720. {
  2721. struct nfs4_exception exception = { };
  2722. int err;
  2723. do {
  2724. err = nfs4_handle_exception(NFS_SERVER(dir),
  2725. _nfs4_proc_remove(dir, name),
  2726. &exception);
  2727. } while (exception.retry);
  2728. return err;
  2729. }
  2730. static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
  2731. {
  2732. struct nfs_server *server = NFS_SERVER(dir);
  2733. struct nfs_removeargs *args = msg->rpc_argp;
  2734. struct nfs_removeres *res = msg->rpc_resp;
  2735. res->server = server;
  2736. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
  2737. nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
  2738. }
  2739. static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
  2740. {
  2741. nfs4_setup_sequence(NFS_SERVER(data->dir),
  2742. &data->args.seq_args,
  2743. &data->res.seq_res,
  2744. task);
  2745. }
  2746. static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
  2747. {
  2748. struct nfs_removeres *res = task->tk_msg.rpc_resp;
  2749. if (!nfs4_sequence_done(task, &res->seq_res))
  2750. return 0;
  2751. if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
  2752. return 0;
  2753. update_changeattr(dir, &res->cinfo);
  2754. return 1;
  2755. }
  2756. static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
  2757. {
  2758. struct nfs_server *server = NFS_SERVER(dir);
  2759. struct nfs_renameargs *arg = msg->rpc_argp;
  2760. struct nfs_renameres *res = msg->rpc_resp;
  2761. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
  2762. res->server = server;
  2763. nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
  2764. }
  2765. static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
  2766. {
  2767. nfs4_setup_sequence(NFS_SERVER(data->old_dir),
  2768. &data->args.seq_args,
  2769. &data->res.seq_res,
  2770. task);
  2771. }
  2772. static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
  2773. struct inode *new_dir)
  2774. {
  2775. struct nfs_renameres *res = task->tk_msg.rpc_resp;
  2776. if (!nfs4_sequence_done(task, &res->seq_res))
  2777. return 0;
  2778. if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
  2779. return 0;
  2780. update_changeattr(old_dir, &res->old_cinfo);
  2781. update_changeattr(new_dir, &res->new_cinfo);
  2782. return 1;
  2783. }
  2784. static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
  2785. struct inode *new_dir, struct qstr *new_name)
  2786. {
  2787. struct nfs_server *server = NFS_SERVER(old_dir);
  2788. struct nfs_renameargs arg = {
  2789. .old_dir = NFS_FH(old_dir),
  2790. .new_dir = NFS_FH(new_dir),
  2791. .old_name = old_name,
  2792. .new_name = new_name,
  2793. };
  2794. struct nfs_renameres res = {
  2795. .server = server,
  2796. };
  2797. struct rpc_message msg = {
  2798. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
  2799. .rpc_argp = &arg,
  2800. .rpc_resp = &res,
  2801. };
  2802. int status = -ENOMEM;
  2803. status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
  2804. if (!status) {
  2805. update_changeattr(old_dir, &res.old_cinfo);
  2806. update_changeattr(new_dir, &res.new_cinfo);
  2807. }
  2808. return status;
  2809. }
  2810. static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
  2811. struct inode *new_dir, struct qstr *new_name)
  2812. {
  2813. struct nfs4_exception exception = { };
  2814. int err;
  2815. do {
  2816. err = nfs4_handle_exception(NFS_SERVER(old_dir),
  2817. _nfs4_proc_rename(old_dir, old_name,
  2818. new_dir, new_name),
  2819. &exception);
  2820. } while (exception.retry);
  2821. return err;
  2822. }
  2823. static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
  2824. {
  2825. struct nfs_server *server = NFS_SERVER(inode);
  2826. struct nfs4_link_arg arg = {
  2827. .fh = NFS_FH(inode),
  2828. .dir_fh = NFS_FH(dir),
  2829. .name = name,
  2830. .bitmask = server->attr_bitmask,
  2831. };
  2832. struct nfs4_link_res res = {
  2833. .server = server,
  2834. };
  2835. struct rpc_message msg = {
  2836. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
  2837. .rpc_argp = &arg,
  2838. .rpc_resp = &res,
  2839. };
  2840. int status = -ENOMEM;
  2841. res.fattr = nfs_alloc_fattr();
  2842. if (res.fattr == NULL)
  2843. goto out;
  2844. status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
  2845. if (!status) {
  2846. update_changeattr(dir, &res.cinfo);
  2847. nfs_post_op_update_inode(inode, res.fattr);
  2848. }
  2849. out:
  2850. nfs_free_fattr(res.fattr);
  2851. return status;
  2852. }
  2853. static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
  2854. {
  2855. struct nfs4_exception exception = { };
  2856. int err;
  2857. do {
  2858. err = nfs4_handle_exception(NFS_SERVER(inode),
  2859. _nfs4_proc_link(inode, dir, name),
  2860. &exception);
  2861. } while (exception.retry);
  2862. return err;
  2863. }
  2864. struct nfs4_createdata {
  2865. struct rpc_message msg;
  2866. struct nfs4_create_arg arg;
  2867. struct nfs4_create_res res;
  2868. struct nfs_fh fh;
  2869. struct nfs_fattr fattr;
  2870. };
  2871. static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
  2872. struct qstr *name, struct iattr *sattr, u32 ftype)
  2873. {
  2874. struct nfs4_createdata *data;
  2875. data = kzalloc(sizeof(*data), GFP_KERNEL);
  2876. if (data != NULL) {
  2877. struct nfs_server *server = NFS_SERVER(dir);
  2878. data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
  2879. data->msg.rpc_argp = &data->arg;
  2880. data->msg.rpc_resp = &data->res;
  2881. data->arg.dir_fh = NFS_FH(dir);
  2882. data->arg.server = server;
  2883. data->arg.name = name;
  2884. data->arg.attrs = sattr;
  2885. data->arg.ftype = ftype;
  2886. data->arg.bitmask = server->attr_bitmask;
  2887. data->res.server = server;
  2888. data->res.fh = &data->fh;
  2889. data->res.fattr = &data->fattr;
  2890. nfs_fattr_init(data->res.fattr);
  2891. }
  2892. return data;
  2893. }
  2894. static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
  2895. {
  2896. int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
  2897. &data->arg.seq_args, &data->res.seq_res, 1);
  2898. if (status == 0) {
  2899. update_changeattr(dir, &data->res.dir_cinfo);
  2900. status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
  2901. }
  2902. return status;
  2903. }
  2904. static void nfs4_free_createdata(struct nfs4_createdata *data)
  2905. {
  2906. kfree(data);
  2907. }
  2908. static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
  2909. struct page *page, unsigned int len, struct iattr *sattr)
  2910. {
  2911. struct nfs4_createdata *data;
  2912. int status = -ENAMETOOLONG;
  2913. if (len > NFS4_MAXPATHLEN)
  2914. goto out;
  2915. status = -ENOMEM;
  2916. data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
  2917. if (data == NULL)
  2918. goto out;
  2919. data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
  2920. data->arg.u.symlink.pages = &page;
  2921. data->arg.u.symlink.len = len;
  2922. status = nfs4_do_create(dir, dentry, data);
  2923. nfs4_free_createdata(data);
  2924. out:
  2925. return status;
  2926. }
  2927. static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
  2928. struct page *page, unsigned int len, struct iattr *sattr)
  2929. {
  2930. struct nfs4_exception exception = { };
  2931. int err;
  2932. do {
  2933. err = nfs4_handle_exception(NFS_SERVER(dir),
  2934. _nfs4_proc_symlink(dir, dentry, page,
  2935. len, sattr),
  2936. &exception);
  2937. } while (exception.retry);
  2938. return err;
  2939. }
  2940. static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
  2941. struct iattr *sattr)
  2942. {
  2943. struct nfs4_createdata *data;
  2944. int status = -ENOMEM;
  2945. data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
  2946. if (data == NULL)
  2947. goto out;
  2948. status = nfs4_do_create(dir, dentry, data);
  2949. nfs4_free_createdata(data);
  2950. out:
  2951. return status;
  2952. }
  2953. static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
  2954. struct iattr *sattr)
  2955. {
  2956. struct nfs4_exception exception = { };
  2957. int err;
  2958. sattr->ia_mode &= ~current_umask();
  2959. do {
  2960. err = nfs4_handle_exception(NFS_SERVER(dir),
  2961. _nfs4_proc_mkdir(dir, dentry, sattr),
  2962. &exception);
  2963. } while (exception.retry);
  2964. return err;
  2965. }
  2966. static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
  2967. u64 cookie, struct page **pages, unsigned int count, int plus)
  2968. {
  2969. struct inode *dir = dentry->d_inode;
  2970. struct nfs4_readdir_arg args = {
  2971. .fh = NFS_FH(dir),
  2972. .pages = pages,
  2973. .pgbase = 0,
  2974. .count = count,
  2975. .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
  2976. .plus = plus,
  2977. };
  2978. struct nfs4_readdir_res res;
  2979. struct rpc_message msg = {
  2980. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
  2981. .rpc_argp = &args,
  2982. .rpc_resp = &res,
  2983. .rpc_cred = cred,
  2984. };
  2985. int status;
  2986. dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
  2987. dentry->d_parent->d_name.name,
  2988. dentry->d_name.name,
  2989. (unsigned long long)cookie);
  2990. nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
  2991. res.pgbase = args.pgbase;
  2992. status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
  2993. if (status >= 0) {
  2994. memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
  2995. status += args.pgbase;
  2996. }
  2997. nfs_invalidate_atime(dir);
  2998. dprintk("%s: returns %d\n", __func__, status);
  2999. return status;
  3000. }
  3001. static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
  3002. u64 cookie, struct page **pages, unsigned int count, int plus)
  3003. {
  3004. struct nfs4_exception exception = { };
  3005. int err;
  3006. do {
  3007. err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
  3008. _nfs4_proc_readdir(dentry, cred, cookie,
  3009. pages, count, plus),
  3010. &exception);
  3011. } while (exception.retry);
  3012. return err;
  3013. }
  3014. static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
  3015. struct iattr *sattr, dev_t rdev)
  3016. {
  3017. struct nfs4_createdata *data;
  3018. int mode = sattr->ia_mode;
  3019. int status = -ENOMEM;
  3020. data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
  3021. if (data == NULL)
  3022. goto out;
  3023. if (S_ISFIFO(mode))
  3024. data->arg.ftype = NF4FIFO;
  3025. else if (S_ISBLK(mode)) {
  3026. data->arg.ftype = NF4BLK;
  3027. data->arg.u.device.specdata1 = MAJOR(rdev);
  3028. data->arg.u.device.specdata2 = MINOR(rdev);
  3029. }
  3030. else if (S_ISCHR(mode)) {
  3031. data->arg.ftype = NF4CHR;
  3032. data->arg.u.device.specdata1 = MAJOR(rdev);
  3033. data->arg.u.device.specdata2 = MINOR(rdev);
  3034. } else if (!S_ISSOCK(mode)) {
  3035. status = -EINVAL;
  3036. goto out_free;
  3037. }
  3038. status = nfs4_do_create(dir, dentry, data);
  3039. out_free:
  3040. nfs4_free_createdata(data);
  3041. out:
  3042. return status;
  3043. }
  3044. static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
  3045. struct iattr *sattr, dev_t rdev)
  3046. {
  3047. struct nfs4_exception exception = { };
  3048. int err;
  3049. sattr->ia_mode &= ~current_umask();
  3050. do {
  3051. err = nfs4_handle_exception(NFS_SERVER(dir),
  3052. _nfs4_proc_mknod(dir, dentry, sattr, rdev),
  3053. &exception);
  3054. } while (exception.retry);
  3055. return err;
  3056. }
  3057. static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
  3058. struct nfs_fsstat *fsstat)
  3059. {
  3060. struct nfs4_statfs_arg args = {
  3061. .fh = fhandle,
  3062. .bitmask = server->attr_bitmask,
  3063. };
  3064. struct nfs4_statfs_res res = {
  3065. .fsstat = fsstat,
  3066. };
  3067. struct rpc_message msg = {
  3068. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
  3069. .rpc_argp = &args,
  3070. .rpc_resp = &res,
  3071. };
  3072. nfs_fattr_init(fsstat->fattr);
  3073. return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  3074. }
  3075. static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
  3076. {
  3077. struct nfs4_exception exception = { };
  3078. int err;
  3079. do {
  3080. err = nfs4_handle_exception(server,
  3081. _nfs4_proc_statfs(server, fhandle, fsstat),
  3082. &exception);
  3083. } while (exception.retry);
  3084. return err;
  3085. }
  3086. static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
  3087. struct nfs_fsinfo *fsinfo)
  3088. {
  3089. struct nfs4_fsinfo_arg args = {
  3090. .fh = fhandle,
  3091. .bitmask = server->attr_bitmask,
  3092. };
  3093. struct nfs4_fsinfo_res res = {
  3094. .fsinfo = fsinfo,
  3095. };
  3096. struct rpc_message msg = {
  3097. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
  3098. .rpc_argp = &args,
  3099. .rpc_resp = &res,
  3100. };
  3101. return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  3102. }
  3103. static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
  3104. {
  3105. struct nfs4_exception exception = { };
  3106. int err;
  3107. do {
  3108. err = nfs4_handle_exception(server,
  3109. _nfs4_do_fsinfo(server, fhandle, fsinfo),
  3110. &exception);
  3111. } while (exception.retry);
  3112. return err;
  3113. }
  3114. static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
  3115. {
  3116. int error;
  3117. nfs_fattr_init(fsinfo->fattr);
  3118. error = nfs4_do_fsinfo(server, fhandle, fsinfo);
  3119. if (error == 0) {
  3120. /* block layout checks this! */
  3121. server->pnfs_blksize = fsinfo->blksize;
  3122. set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
  3123. }
  3124. return error;
  3125. }
  3126. static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
  3127. struct nfs_pathconf *pathconf)
  3128. {
  3129. struct nfs4_pathconf_arg args = {
  3130. .fh = fhandle,
  3131. .bitmask = server->attr_bitmask,
  3132. };
  3133. struct nfs4_pathconf_res res = {
  3134. .pathconf = pathconf,
  3135. };
  3136. struct rpc_message msg = {
  3137. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
  3138. .rpc_argp = &args,
  3139. .rpc_resp = &res,
  3140. };
  3141. /* None of the pathconf attributes are mandatory to implement */
  3142. if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
  3143. memset(pathconf, 0, sizeof(*pathconf));
  3144. return 0;
  3145. }
  3146. nfs_fattr_init(pathconf->fattr);
  3147. return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  3148. }
  3149. static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
  3150. struct nfs_pathconf *pathconf)
  3151. {
  3152. struct nfs4_exception exception = { };
  3153. int err;
  3154. do {
  3155. err = nfs4_handle_exception(server,
  3156. _nfs4_proc_pathconf(server, fhandle, pathconf),
  3157. &exception);
  3158. } while (exception.retry);
  3159. return err;
  3160. }
  3161. int nfs4_set_rw_stateid(nfs4_stateid *stateid,
  3162. const struct nfs_open_context *ctx,
  3163. const struct nfs_lock_context *l_ctx,
  3164. fmode_t fmode)
  3165. {
  3166. const struct nfs_lockowner *lockowner = NULL;
  3167. if (l_ctx != NULL)
  3168. lockowner = &l_ctx->lockowner;
  3169. return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
  3170. }
  3171. EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
  3172. static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
  3173. const struct nfs_open_context *ctx,
  3174. const struct nfs_lock_context *l_ctx,
  3175. fmode_t fmode)
  3176. {
  3177. nfs4_stateid current_stateid;
  3178. if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode))
  3179. return false;
  3180. return nfs4_stateid_match(stateid, &current_stateid);
  3181. }
  3182. static bool nfs4_error_stateid_expired(int err)
  3183. {
  3184. switch (err) {
  3185. case -NFS4ERR_DELEG_REVOKED:
  3186. case -NFS4ERR_ADMIN_REVOKED:
  3187. case -NFS4ERR_BAD_STATEID:
  3188. case -NFS4ERR_STALE_STATEID:
  3189. case -NFS4ERR_OLD_STATEID:
  3190. case -NFS4ERR_OPENMODE:
  3191. case -NFS4ERR_EXPIRED:
  3192. return true;
  3193. }
  3194. return false;
  3195. }
  3196. void __nfs4_read_done_cb(struct nfs_read_data *data)
  3197. {
  3198. nfs_invalidate_atime(data->header->inode);
  3199. }
  3200. static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
  3201. {
  3202. struct nfs_server *server = NFS_SERVER(data->header->inode);
  3203. if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
  3204. rpc_restart_call_prepare(task);
  3205. return -EAGAIN;
  3206. }
  3207. __nfs4_read_done_cb(data);
  3208. if (task->tk_status > 0)
  3209. renew_lease(server, data->timestamp);
  3210. return 0;
  3211. }
  3212. static bool nfs4_read_stateid_changed(struct rpc_task *task,
  3213. struct nfs_readargs *args)
  3214. {
  3215. if (!nfs4_error_stateid_expired(task->tk_status) ||
  3216. nfs4_stateid_is_current(&args->stateid,
  3217. args->context,
  3218. args->lock_context,
  3219. FMODE_READ))
  3220. return false;
  3221. rpc_restart_call_prepare(task);
  3222. return true;
  3223. }
  3224. static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
  3225. {
  3226. dprintk("--> %s\n", __func__);
  3227. if (!nfs4_sequence_done(task, &data->res.seq_res))
  3228. return -EAGAIN;
  3229. if (nfs4_read_stateid_changed(task, &data->args))
  3230. return -EAGAIN;
  3231. return data->read_done_cb ? data->read_done_cb(task, data) :
  3232. nfs4_read_done_cb(task, data);
  3233. }
  3234. static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
  3235. {
  3236. data->timestamp = jiffies;
  3237. data->read_done_cb = nfs4_read_done_cb;
  3238. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
  3239. nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
  3240. }
  3241. static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
  3242. {
  3243. if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
  3244. &data->args.seq_args,
  3245. &data->res.seq_res,
  3246. task))
  3247. return;
  3248. nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
  3249. data->args.lock_context, FMODE_READ);
  3250. }
  3251. static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
  3252. {
  3253. struct inode *inode = data->header->inode;
  3254. if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
  3255. rpc_restart_call_prepare(task);
  3256. return -EAGAIN;
  3257. }
  3258. if (task->tk_status >= 0) {
  3259. renew_lease(NFS_SERVER(inode), data->timestamp);
  3260. nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
  3261. }
  3262. return 0;
  3263. }
  3264. static bool nfs4_write_stateid_changed(struct rpc_task *task,
  3265. struct nfs_writeargs *args)
  3266. {
  3267. if (!nfs4_error_stateid_expired(task->tk_status) ||
  3268. nfs4_stateid_is_current(&args->stateid,
  3269. args->context,
  3270. args->lock_context,
  3271. FMODE_WRITE))
  3272. return false;
  3273. rpc_restart_call_prepare(task);
  3274. return true;
  3275. }
  3276. static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
  3277. {
  3278. if (!nfs4_sequence_done(task, &data->res.seq_res))
  3279. return -EAGAIN;
  3280. if (nfs4_write_stateid_changed(task, &data->args))
  3281. return -EAGAIN;
  3282. return data->write_done_cb ? data->write_done_cb(task, data) :
  3283. nfs4_write_done_cb(task, data);
  3284. }
  3285. static
  3286. bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
  3287. {
  3288. const struct nfs_pgio_header *hdr = data->header;
  3289. /* Don't request attributes for pNFS or O_DIRECT writes */
  3290. if (data->ds_clp != NULL || hdr->dreq != NULL)
  3291. return false;
  3292. /* Otherwise, request attributes if and only if we don't hold
  3293. * a delegation
  3294. */
  3295. return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
  3296. }
  3297. static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
  3298. {
  3299. struct nfs_server *server = NFS_SERVER(data->header->inode);
  3300. if (!nfs4_write_need_cache_consistency_data(data)) {
  3301. data->args.bitmask = NULL;
  3302. data->res.fattr = NULL;
  3303. } else
  3304. data->args.bitmask = server->cache_consistency_bitmask;
  3305. if (!data->write_done_cb)
  3306. data->write_done_cb = nfs4_write_done_cb;
  3307. data->res.server = server;
  3308. data->timestamp = jiffies;
  3309. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
  3310. nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
  3311. }
  3312. static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
  3313. {
  3314. if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
  3315. &data->args.seq_args,
  3316. &data->res.seq_res,
  3317. task))
  3318. return;
  3319. nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
  3320. data->args.lock_context, FMODE_WRITE);
  3321. }
  3322. static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
  3323. {
  3324. nfs4_setup_sequence(NFS_SERVER(data->inode),
  3325. &data->args.seq_args,
  3326. &data->res.seq_res,
  3327. task);
  3328. }
  3329. static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
  3330. {
  3331. struct inode *inode = data->inode;
  3332. if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
  3333. rpc_restart_call_prepare(task);
  3334. return -EAGAIN;
  3335. }
  3336. return 0;
  3337. }
  3338. static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
  3339. {
  3340. if (!nfs4_sequence_done(task, &data->res.seq_res))
  3341. return -EAGAIN;
  3342. return data->commit_done_cb(task, data);
  3343. }
  3344. static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
  3345. {
  3346. struct nfs_server *server = NFS_SERVER(data->inode);
  3347. if (data->commit_done_cb == NULL)
  3348. data->commit_done_cb = nfs4_commit_done_cb;
  3349. data->res.server = server;
  3350. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
  3351. nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
  3352. }
  3353. struct nfs4_renewdata {
  3354. struct nfs_client *client;
  3355. unsigned long timestamp;
  3356. };
  3357. /*
  3358. * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
  3359. * standalone procedure for queueing an asynchronous RENEW.
  3360. */
  3361. static void nfs4_renew_release(void *calldata)
  3362. {
  3363. struct nfs4_renewdata *data = calldata;
  3364. struct nfs_client *clp = data->client;
  3365. if (atomic_read(&clp->cl_count) > 1)
  3366. nfs4_schedule_state_renewal(clp);
  3367. nfs_put_client(clp);
  3368. kfree(data);
  3369. }
  3370. static void nfs4_renew_done(struct rpc_task *task, void *calldata)
  3371. {
  3372. struct nfs4_renewdata *data = calldata;
  3373. struct nfs_client *clp = data->client;
  3374. unsigned long timestamp = data->timestamp;
  3375. if (task->tk_status < 0) {
  3376. /* Unless we're shutting down, schedule state recovery! */
  3377. if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
  3378. return;
  3379. if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
  3380. nfs4_schedule_lease_recovery(clp);
  3381. return;
  3382. }
  3383. nfs4_schedule_path_down_recovery(clp);
  3384. }
  3385. do_renew_lease(clp, timestamp);
  3386. }
  3387. static const struct rpc_call_ops nfs4_renew_ops = {
  3388. .rpc_call_done = nfs4_renew_done,
  3389. .rpc_release = nfs4_renew_release,
  3390. };
  3391. static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
  3392. {
  3393. struct rpc_message msg = {
  3394. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
  3395. .rpc_argp = clp,
  3396. .rpc_cred = cred,
  3397. };
  3398. struct nfs4_renewdata *data;
  3399. if (renew_flags == 0)
  3400. return 0;
  3401. if (!atomic_inc_not_zero(&clp->cl_count))
  3402. return -EIO;
  3403. data = kmalloc(sizeof(*data), GFP_NOFS);
  3404. if (data == NULL)
  3405. return -ENOMEM;
  3406. data->client = clp;
  3407. data->timestamp = jiffies;
  3408. return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
  3409. &nfs4_renew_ops, data);
  3410. }
  3411. static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
  3412. {
  3413. struct rpc_message msg = {
  3414. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
  3415. .rpc_argp = clp,
  3416. .rpc_cred = cred,
  3417. };
  3418. unsigned long now = jiffies;
  3419. int status;
  3420. status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
  3421. if (status < 0)
  3422. return status;
  3423. do_renew_lease(clp, now);
  3424. return 0;
  3425. }
  3426. static inline int nfs4_server_supports_acls(struct nfs_server *server)
  3427. {
  3428. return (server->caps & NFS_CAP_ACLS)
  3429. && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
  3430. && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
  3431. }
  3432. /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
  3433. * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
  3434. * the stack.
  3435. */
  3436. #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
  3437. static int buf_to_pages_noslab(const void *buf, size_t buflen,
  3438. struct page **pages, unsigned int *pgbase)
  3439. {
  3440. struct page *newpage, **spages;
  3441. int rc = 0;
  3442. size_t len;
  3443. spages = pages;
  3444. do {
  3445. len = min_t(size_t, PAGE_SIZE, buflen);
  3446. newpage = alloc_page(GFP_KERNEL);
  3447. if (newpage == NULL)
  3448. goto unwind;
  3449. memcpy(page_address(newpage), buf, len);
  3450. buf += len;
  3451. buflen -= len;
  3452. *pages++ = newpage;
  3453. rc++;
  3454. } while (buflen != 0);
  3455. return rc;
  3456. unwind:
  3457. for(; rc > 0; rc--)
  3458. __free_page(spages[rc-1]);
  3459. return -ENOMEM;
  3460. }
  3461. struct nfs4_cached_acl {
  3462. int cached;
  3463. size_t len;
  3464. char data[0];
  3465. };
  3466. static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
  3467. {
  3468. struct nfs_inode *nfsi = NFS_I(inode);
  3469. spin_lock(&inode->i_lock);
  3470. kfree(nfsi->nfs4_acl);
  3471. nfsi->nfs4_acl = acl;
  3472. spin_unlock(&inode->i_lock);
  3473. }
  3474. static void nfs4_zap_acl_attr(struct inode *inode)
  3475. {
  3476. nfs4_set_cached_acl(inode, NULL);
  3477. }
  3478. static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
  3479. {
  3480. struct nfs_inode *nfsi = NFS_I(inode);
  3481. struct nfs4_cached_acl *acl;
  3482. int ret = -ENOENT;
  3483. spin_lock(&inode->i_lock);
  3484. acl = nfsi->nfs4_acl;
  3485. if (acl == NULL)
  3486. goto out;
  3487. if (buf == NULL) /* user is just asking for length */
  3488. goto out_len;
  3489. if (acl->cached == 0)
  3490. goto out;
  3491. ret = -ERANGE; /* see getxattr(2) man page */
  3492. if (acl->len > buflen)
  3493. goto out;
  3494. memcpy(buf, acl->data, acl->len);
  3495. out_len:
  3496. ret = acl->len;
  3497. out:
  3498. spin_unlock(&inode->i_lock);
  3499. return ret;
  3500. }
  3501. static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
  3502. {
  3503. struct nfs4_cached_acl *acl;
  3504. size_t buflen = sizeof(*acl) + acl_len;
  3505. if (buflen <= PAGE_SIZE) {
  3506. acl = kmalloc(buflen, GFP_KERNEL);
  3507. if (acl == NULL)
  3508. goto out;
  3509. acl->cached = 1;
  3510. _copy_from_pages(acl->data, pages, pgbase, acl_len);
  3511. } else {
  3512. acl = kmalloc(sizeof(*acl), GFP_KERNEL);
  3513. if (acl == NULL)
  3514. goto out;
  3515. acl->cached = 0;
  3516. }
  3517. acl->len = acl_len;
  3518. out:
  3519. nfs4_set_cached_acl(inode, acl);
  3520. }
  3521. /*
  3522. * The getxattr API returns the required buffer length when called with a
  3523. * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
  3524. * the required buf. On a NULL buf, we send a page of data to the server
  3525. * guessing that the ACL request can be serviced by a page. If so, we cache
  3526. * up to the page of ACL data, and the 2nd call to getxattr is serviced by
  3527. * the cache. If not so, we throw away the page, and cache the required
  3528. * length. The next getxattr call will then produce another round trip to
  3529. * the server, this time with the input buf of the required size.
  3530. */
  3531. static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
  3532. {
  3533. struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
  3534. struct nfs_getaclargs args = {
  3535. .fh = NFS_FH(inode),
  3536. .acl_pages = pages,
  3537. .acl_len = buflen,
  3538. };
  3539. struct nfs_getaclres res = {
  3540. .acl_len = buflen,
  3541. };
  3542. struct rpc_message msg = {
  3543. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
  3544. .rpc_argp = &args,
  3545. .rpc_resp = &res,
  3546. };
  3547. unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
  3548. int ret = -ENOMEM, i;
  3549. /* As long as we're doing a round trip to the server anyway,
  3550. * let's be prepared for a page of acl data. */
  3551. if (npages == 0)
  3552. npages = 1;
  3553. if (npages > ARRAY_SIZE(pages))
  3554. return -ERANGE;
  3555. for (i = 0; i < npages; i++) {
  3556. pages[i] = alloc_page(GFP_KERNEL);
  3557. if (!pages[i])
  3558. goto out_free;
  3559. }
  3560. /* for decoding across pages */
  3561. res.acl_scratch = alloc_page(GFP_KERNEL);
  3562. if (!res.acl_scratch)
  3563. goto out_free;
  3564. args.acl_len = npages * PAGE_SIZE;
  3565. args.acl_pgbase = 0;
  3566. dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
  3567. __func__, buf, buflen, npages, args.acl_len);
  3568. ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
  3569. &msg, &args.seq_args, &res.seq_res, 0);
  3570. if (ret)
  3571. goto out_free;
  3572. /* Handle the case where the passed-in buffer is too short */
  3573. if (res.acl_flags & NFS4_ACL_TRUNC) {
  3574. /* Did the user only issue a request for the acl length? */
  3575. if (buf == NULL)
  3576. goto out_ok;
  3577. ret = -ERANGE;
  3578. goto out_free;
  3579. }
  3580. nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
  3581. if (buf) {
  3582. if (res.acl_len > buflen) {
  3583. ret = -ERANGE;
  3584. goto out_free;
  3585. }
  3586. _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
  3587. }
  3588. out_ok:
  3589. ret = res.acl_len;
  3590. out_free:
  3591. for (i = 0; i < npages; i++)
  3592. if (pages[i])
  3593. __free_page(pages[i]);
  3594. if (res.acl_scratch)
  3595. __free_page(res.acl_scratch);
  3596. return ret;
  3597. }
  3598. static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
  3599. {
  3600. struct nfs4_exception exception = { };
  3601. ssize_t ret;
  3602. do {
  3603. ret = __nfs4_get_acl_uncached(inode, buf, buflen);
  3604. if (ret >= 0)
  3605. break;
  3606. ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
  3607. } while (exception.retry);
  3608. return ret;
  3609. }
  3610. static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
  3611. {
  3612. struct nfs_server *server = NFS_SERVER(inode);
  3613. int ret;
  3614. if (!nfs4_server_supports_acls(server))
  3615. return -EOPNOTSUPP;
  3616. ret = nfs_revalidate_inode(server, inode);
  3617. if (ret < 0)
  3618. return ret;
  3619. if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
  3620. nfs_zap_acl_cache(inode);
  3621. ret = nfs4_read_cached_acl(inode, buf, buflen);
  3622. if (ret != -ENOENT)
  3623. /* -ENOENT is returned if there is no ACL or if there is an ACL
  3624. * but no cached acl data, just the acl length */
  3625. return ret;
  3626. return nfs4_get_acl_uncached(inode, buf, buflen);
  3627. }
  3628. static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
  3629. {
  3630. struct nfs_server *server = NFS_SERVER(inode);
  3631. struct page *pages[NFS4ACL_MAXPAGES];
  3632. struct nfs_setaclargs arg = {
  3633. .fh = NFS_FH(inode),
  3634. .acl_pages = pages,
  3635. .acl_len = buflen,
  3636. };
  3637. struct nfs_setaclres res;
  3638. struct rpc_message msg = {
  3639. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
  3640. .rpc_argp = &arg,
  3641. .rpc_resp = &res,
  3642. };
  3643. unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
  3644. int ret, i;
  3645. if (!nfs4_server_supports_acls(server))
  3646. return -EOPNOTSUPP;
  3647. if (npages > ARRAY_SIZE(pages))
  3648. return -ERANGE;
  3649. i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
  3650. if (i < 0)
  3651. return i;
  3652. nfs4_inode_return_delegation(inode);
  3653. ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
  3654. /*
  3655. * Free each page after tx, so the only ref left is
  3656. * held by the network stack
  3657. */
  3658. for (; i > 0; i--)
  3659. put_page(pages[i-1]);
  3660. /*
  3661. * Acl update can result in inode attribute update.
  3662. * so mark the attribute cache invalid.
  3663. */
  3664. spin_lock(&inode->i_lock);
  3665. NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
  3666. spin_unlock(&inode->i_lock);
  3667. nfs_access_zap_cache(inode);
  3668. nfs_zap_acl_cache(inode);
  3669. return ret;
  3670. }
  3671. static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
  3672. {
  3673. struct nfs4_exception exception = { };
  3674. int err;
  3675. do {
  3676. err = nfs4_handle_exception(NFS_SERVER(inode),
  3677. __nfs4_proc_set_acl(inode, buf, buflen),
  3678. &exception);
  3679. } while (exception.retry);
  3680. return err;
  3681. }
  3682. static int
  3683. nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
  3684. {
  3685. struct nfs_client *clp = server->nfs_client;
  3686. if (task->tk_status >= 0)
  3687. return 0;
  3688. switch(task->tk_status) {
  3689. case -NFS4ERR_DELEG_REVOKED:
  3690. case -NFS4ERR_ADMIN_REVOKED:
  3691. case -NFS4ERR_BAD_STATEID:
  3692. if (state == NULL)
  3693. break;
  3694. nfs_remove_bad_delegation(state->inode);
  3695. case -NFS4ERR_OPENMODE:
  3696. if (state == NULL)
  3697. break;
  3698. if (nfs4_schedule_stateid_recovery(server, state) < 0)
  3699. goto stateid_invalid;
  3700. goto wait_on_recovery;
  3701. case -NFS4ERR_EXPIRED:
  3702. if (state != NULL) {
  3703. if (nfs4_schedule_stateid_recovery(server, state) < 0)
  3704. goto stateid_invalid;
  3705. }
  3706. case -NFS4ERR_STALE_STATEID:
  3707. case -NFS4ERR_STALE_CLIENTID:
  3708. nfs4_schedule_lease_recovery(clp);
  3709. goto wait_on_recovery;
  3710. #if defined(CONFIG_NFS_V4_1)
  3711. case -NFS4ERR_BADSESSION:
  3712. case -NFS4ERR_BADSLOT:
  3713. case -NFS4ERR_BAD_HIGH_SLOT:
  3714. case -NFS4ERR_DEADSESSION:
  3715. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  3716. case -NFS4ERR_SEQ_FALSE_RETRY:
  3717. case -NFS4ERR_SEQ_MISORDERED:
  3718. dprintk("%s ERROR %d, Reset session\n", __func__,
  3719. task->tk_status);
  3720. nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
  3721. task->tk_status = 0;
  3722. return -EAGAIN;
  3723. #endif /* CONFIG_NFS_V4_1 */
  3724. case -NFS4ERR_DELAY:
  3725. nfs_inc_server_stats(server, NFSIOS_DELAY);
  3726. case -NFS4ERR_GRACE:
  3727. rpc_delay(task, NFS4_POLL_RETRY_MAX);
  3728. task->tk_status = 0;
  3729. return -EAGAIN;
  3730. case -NFS4ERR_RETRY_UNCACHED_REP:
  3731. case -NFS4ERR_OLD_STATEID:
  3732. task->tk_status = 0;
  3733. return -EAGAIN;
  3734. }
  3735. task->tk_status = nfs4_map_errors(task->tk_status);
  3736. return 0;
  3737. stateid_invalid:
  3738. task->tk_status = -EIO;
  3739. return 0;
  3740. wait_on_recovery:
  3741. rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
  3742. if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
  3743. rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
  3744. task->tk_status = 0;
  3745. return -EAGAIN;
  3746. }
  3747. static void nfs4_init_boot_verifier(const struct nfs_client *clp,
  3748. nfs4_verifier *bootverf)
  3749. {
  3750. __be32 verf[2];
  3751. if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
  3752. /* An impossible timestamp guarantees this value
  3753. * will never match a generated boot time. */
  3754. verf[0] = 0;
  3755. verf[1] = (__be32)(NSEC_PER_SEC + 1);
  3756. } else {
  3757. struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
  3758. verf[0] = (__be32)nn->boot_time.tv_sec;
  3759. verf[1] = (__be32)nn->boot_time.tv_nsec;
  3760. }
  3761. memcpy(bootverf->data, verf, sizeof(bootverf->data));
  3762. }
  3763. static unsigned int
  3764. nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
  3765. char *buf, size_t len)
  3766. {
  3767. unsigned int result;
  3768. rcu_read_lock();
  3769. result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
  3770. clp->cl_ipaddr,
  3771. rpc_peeraddr2str(clp->cl_rpcclient,
  3772. RPC_DISPLAY_ADDR),
  3773. rpc_peeraddr2str(clp->cl_rpcclient,
  3774. RPC_DISPLAY_PROTO));
  3775. rcu_read_unlock();
  3776. return result;
  3777. }
  3778. static unsigned int
  3779. nfs4_init_uniform_client_string(const struct nfs_client *clp,
  3780. char *buf, size_t len)
  3781. {
  3782. char *nodename = clp->cl_rpcclient->cl_nodename;
  3783. if (nfs4_client_id_uniquifier[0] != '\0')
  3784. nodename = nfs4_client_id_uniquifier;
  3785. return scnprintf(buf, len, "Linux NFSv%u.%u %s",
  3786. clp->rpc_ops->version, clp->cl_minorversion,
  3787. nodename);
  3788. }
  3789. /**
  3790. * nfs4_proc_setclientid - Negotiate client ID
  3791. * @clp: state data structure
  3792. * @program: RPC program for NFSv4 callback service
  3793. * @port: IP port number for NFS4 callback service
  3794. * @cred: RPC credential to use for this call
  3795. * @res: where to place the result
  3796. *
  3797. * Returns zero, a negative errno, or a negative NFS4ERR status code.
  3798. */
  3799. int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
  3800. unsigned short port, struct rpc_cred *cred,
  3801. struct nfs4_setclientid_res *res)
  3802. {
  3803. nfs4_verifier sc_verifier;
  3804. struct nfs4_setclientid setclientid = {
  3805. .sc_verifier = &sc_verifier,
  3806. .sc_prog = program,
  3807. .sc_cb_ident = clp->cl_cb_ident,
  3808. };
  3809. struct rpc_message msg = {
  3810. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
  3811. .rpc_argp = &setclientid,
  3812. .rpc_resp = res,
  3813. .rpc_cred = cred,
  3814. };
  3815. int status;
  3816. /* nfs_client_id4 */
  3817. nfs4_init_boot_verifier(clp, &sc_verifier);
  3818. if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
  3819. setclientid.sc_name_len =
  3820. nfs4_init_uniform_client_string(clp,
  3821. setclientid.sc_name,
  3822. sizeof(setclientid.sc_name));
  3823. else
  3824. setclientid.sc_name_len =
  3825. nfs4_init_nonuniform_client_string(clp,
  3826. setclientid.sc_name,
  3827. sizeof(setclientid.sc_name));
  3828. /* cb_client4 */
  3829. rcu_read_lock();
  3830. setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
  3831. sizeof(setclientid.sc_netid),
  3832. rpc_peeraddr2str(clp->cl_rpcclient,
  3833. RPC_DISPLAY_NETID));
  3834. rcu_read_unlock();
  3835. setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
  3836. sizeof(setclientid.sc_uaddr), "%s.%u.%u",
  3837. clp->cl_ipaddr, port >> 8, port & 255);
  3838. dprintk("NFS call setclientid auth=%s, '%.*s'\n",
  3839. clp->cl_rpcclient->cl_auth->au_ops->au_name,
  3840. setclientid.sc_name_len, setclientid.sc_name);
  3841. status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
  3842. dprintk("NFS reply setclientid: %d\n", status);
  3843. return status;
  3844. }
  3845. /**
  3846. * nfs4_proc_setclientid_confirm - Confirm client ID
  3847. * @clp: state data structure
  3848. * @res: result of a previous SETCLIENTID
  3849. * @cred: RPC credential to use for this call
  3850. *
  3851. * Returns zero, a negative errno, or a negative NFS4ERR status code.
  3852. */
  3853. int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
  3854. struct nfs4_setclientid_res *arg,
  3855. struct rpc_cred *cred)
  3856. {
  3857. struct nfs_fsinfo fsinfo;
  3858. struct rpc_message msg = {
  3859. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
  3860. .rpc_argp = arg,
  3861. .rpc_resp = &fsinfo,
  3862. .rpc_cred = cred,
  3863. };
  3864. unsigned long now;
  3865. int status;
  3866. dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
  3867. clp->cl_rpcclient->cl_auth->au_ops->au_name,
  3868. clp->cl_clientid);
  3869. now = jiffies;
  3870. status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
  3871. if (status == 0) {
  3872. spin_lock(&clp->cl_lock);
  3873. clp->cl_lease_time = fsinfo.lease_time * HZ;
  3874. clp->cl_last_renewal = now;
  3875. spin_unlock(&clp->cl_lock);
  3876. }
  3877. dprintk("NFS reply setclientid_confirm: %d\n", status);
  3878. return status;
  3879. }
  3880. struct nfs4_delegreturndata {
  3881. struct nfs4_delegreturnargs args;
  3882. struct nfs4_delegreturnres res;
  3883. struct nfs_fh fh;
  3884. nfs4_stateid stateid;
  3885. unsigned long timestamp;
  3886. struct nfs_fattr fattr;
  3887. int rpc_status;
  3888. };
  3889. static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
  3890. {
  3891. struct nfs4_delegreturndata *data = calldata;
  3892. if (!nfs4_sequence_done(task, &data->res.seq_res))
  3893. return;
  3894. switch (task->tk_status) {
  3895. case -NFS4ERR_STALE_STATEID:
  3896. case -NFS4ERR_EXPIRED:
  3897. case 0:
  3898. renew_lease(data->res.server, data->timestamp);
  3899. break;
  3900. default:
  3901. if (nfs4_async_handle_error(task, data->res.server, NULL) ==
  3902. -EAGAIN) {
  3903. rpc_restart_call_prepare(task);
  3904. return;
  3905. }
  3906. }
  3907. data->rpc_status = task->tk_status;
  3908. }
  3909. static void nfs4_delegreturn_release(void *calldata)
  3910. {
  3911. kfree(calldata);
  3912. }
  3913. #if defined(CONFIG_NFS_V4_1)
  3914. static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
  3915. {
  3916. struct nfs4_delegreturndata *d_data;
  3917. d_data = (struct nfs4_delegreturndata *)data;
  3918. nfs4_setup_sequence(d_data->res.server,
  3919. &d_data->args.seq_args,
  3920. &d_data->res.seq_res,
  3921. task);
  3922. }
  3923. #endif /* CONFIG_NFS_V4_1 */
  3924. static const struct rpc_call_ops nfs4_delegreturn_ops = {
  3925. #if defined(CONFIG_NFS_V4_1)
  3926. .rpc_call_prepare = nfs4_delegreturn_prepare,
  3927. #endif /* CONFIG_NFS_V4_1 */
  3928. .rpc_call_done = nfs4_delegreturn_done,
  3929. .rpc_release = nfs4_delegreturn_release,
  3930. };
  3931. static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
  3932. {
  3933. struct nfs4_delegreturndata *data;
  3934. struct nfs_server *server = NFS_SERVER(inode);
  3935. struct rpc_task *task;
  3936. struct rpc_message msg = {
  3937. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
  3938. .rpc_cred = cred,
  3939. };
  3940. struct rpc_task_setup task_setup_data = {
  3941. .rpc_client = server->client,
  3942. .rpc_message = &msg,
  3943. .callback_ops = &nfs4_delegreturn_ops,
  3944. .flags = RPC_TASK_ASYNC,
  3945. };
  3946. int status = 0;
  3947. data = kzalloc(sizeof(*data), GFP_NOFS);
  3948. if (data == NULL)
  3949. return -ENOMEM;
  3950. nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
  3951. data->args.fhandle = &data->fh;
  3952. data->args.stateid = &data->stateid;
  3953. data->args.bitmask = server->cache_consistency_bitmask;
  3954. nfs_copy_fh(&data->fh, NFS_FH(inode));
  3955. nfs4_stateid_copy(&data->stateid, stateid);
  3956. data->res.fattr = &data->fattr;
  3957. data->res.server = server;
  3958. nfs_fattr_init(data->res.fattr);
  3959. data->timestamp = jiffies;
  3960. data->rpc_status = 0;
  3961. task_setup_data.callback_data = data;
  3962. msg.rpc_argp = &data->args;
  3963. msg.rpc_resp = &data->res;
  3964. task = rpc_run_task(&task_setup_data);
  3965. if (IS_ERR(task))
  3966. return PTR_ERR(task);
  3967. if (!issync)
  3968. goto out;
  3969. status = nfs4_wait_for_completion_rpc_task(task);
  3970. if (status != 0)
  3971. goto out;
  3972. status = data->rpc_status;
  3973. if (status == 0)
  3974. nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
  3975. else
  3976. nfs_refresh_inode(inode, &data->fattr);
  3977. out:
  3978. rpc_put_task(task);
  3979. return status;
  3980. }
  3981. int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
  3982. {
  3983. struct nfs_server *server = NFS_SERVER(inode);
  3984. struct nfs4_exception exception = { };
  3985. int err;
  3986. do {
  3987. err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
  3988. switch (err) {
  3989. case -NFS4ERR_STALE_STATEID:
  3990. case -NFS4ERR_EXPIRED:
  3991. case 0:
  3992. return 0;
  3993. }
  3994. err = nfs4_handle_exception(server, err, &exception);
  3995. } while (exception.retry);
  3996. return err;
  3997. }
  3998. #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
  3999. #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
  4000. /*
  4001. * sleep, with exponential backoff, and retry the LOCK operation.
  4002. */
  4003. static unsigned long
  4004. nfs4_set_lock_task_retry(unsigned long timeout)
  4005. {
  4006. freezable_schedule_timeout_killable(timeout);
  4007. timeout <<= 1;
  4008. if (timeout > NFS4_LOCK_MAXTIMEOUT)
  4009. return NFS4_LOCK_MAXTIMEOUT;
  4010. return timeout;
  4011. }
  4012. static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  4013. {
  4014. struct inode *inode = state->inode;
  4015. struct nfs_server *server = NFS_SERVER(inode);
  4016. struct nfs_client *clp = server->nfs_client;
  4017. struct nfs_lockt_args arg = {
  4018. .fh = NFS_FH(inode),
  4019. .fl = request,
  4020. };
  4021. struct nfs_lockt_res res = {
  4022. .denied = request,
  4023. };
  4024. struct rpc_message msg = {
  4025. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
  4026. .rpc_argp = &arg,
  4027. .rpc_resp = &res,
  4028. .rpc_cred = state->owner->so_cred,
  4029. };
  4030. struct nfs4_lock_state *lsp;
  4031. int status;
  4032. arg.lock_owner.clientid = clp->cl_clientid;
  4033. status = nfs4_set_lock_state(state, request);
  4034. if (status != 0)
  4035. goto out;
  4036. lsp = request->fl_u.nfs4_fl.owner;
  4037. arg.lock_owner.id = lsp->ls_seqid.owner_id;
  4038. arg.lock_owner.s_dev = server->s_dev;
  4039. status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
  4040. switch (status) {
  4041. case 0:
  4042. request->fl_type = F_UNLCK;
  4043. break;
  4044. case -NFS4ERR_DENIED:
  4045. status = 0;
  4046. }
  4047. request->fl_ops->fl_release_private(request);
  4048. out:
  4049. return status;
  4050. }
  4051. static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  4052. {
  4053. struct nfs4_exception exception = { };
  4054. int err;
  4055. do {
  4056. err = nfs4_handle_exception(NFS_SERVER(state->inode),
  4057. _nfs4_proc_getlk(state, cmd, request),
  4058. &exception);
  4059. } while (exception.retry);
  4060. return err;
  4061. }
  4062. static int do_vfs_lock(struct file *file, struct file_lock *fl)
  4063. {
  4064. int res = 0;
  4065. switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
  4066. case FL_POSIX:
  4067. res = posix_lock_file_wait(file, fl);
  4068. break;
  4069. case FL_FLOCK:
  4070. res = flock_lock_file_wait(file, fl);
  4071. break;
  4072. default:
  4073. BUG();
  4074. }
  4075. return res;
  4076. }
  4077. struct nfs4_unlockdata {
  4078. struct nfs_locku_args arg;
  4079. struct nfs_locku_res res;
  4080. struct nfs4_lock_state *lsp;
  4081. struct nfs_open_context *ctx;
  4082. struct file_lock fl;
  4083. const struct nfs_server *server;
  4084. unsigned long timestamp;
  4085. };
  4086. static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
  4087. struct nfs_open_context *ctx,
  4088. struct nfs4_lock_state *lsp,
  4089. struct nfs_seqid *seqid)
  4090. {
  4091. struct nfs4_unlockdata *p;
  4092. struct inode *inode = lsp->ls_state->inode;
  4093. p = kzalloc(sizeof(*p), GFP_NOFS);
  4094. if (p == NULL)
  4095. return NULL;
  4096. p->arg.fh = NFS_FH(inode);
  4097. p->arg.fl = &p->fl;
  4098. p->arg.seqid = seqid;
  4099. p->res.seqid = seqid;
  4100. p->arg.stateid = &lsp->ls_stateid;
  4101. p->lsp = lsp;
  4102. atomic_inc(&lsp->ls_count);
  4103. /* Ensure we don't close file until we're done freeing locks! */
  4104. p->ctx = get_nfs_open_context(ctx);
  4105. memcpy(&p->fl, fl, sizeof(p->fl));
  4106. p->server = NFS_SERVER(inode);
  4107. return p;
  4108. }
  4109. static void nfs4_locku_release_calldata(void *data)
  4110. {
  4111. struct nfs4_unlockdata *calldata = data;
  4112. nfs_free_seqid(calldata->arg.seqid);
  4113. nfs4_put_lock_state(calldata->lsp);
  4114. put_nfs_open_context(calldata->ctx);
  4115. kfree(calldata);
  4116. }
  4117. static void nfs4_locku_done(struct rpc_task *task, void *data)
  4118. {
  4119. struct nfs4_unlockdata *calldata = data;
  4120. if (!nfs4_sequence_done(task, &calldata->res.seq_res))
  4121. return;
  4122. switch (task->tk_status) {
  4123. case 0:
  4124. nfs4_stateid_copy(&calldata->lsp->ls_stateid,
  4125. &calldata->res.stateid);
  4126. renew_lease(calldata->server, calldata->timestamp);
  4127. break;
  4128. case -NFS4ERR_BAD_STATEID:
  4129. case -NFS4ERR_OLD_STATEID:
  4130. case -NFS4ERR_STALE_STATEID:
  4131. case -NFS4ERR_EXPIRED:
  4132. break;
  4133. default:
  4134. if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
  4135. rpc_restart_call_prepare(task);
  4136. }
  4137. nfs_release_seqid(calldata->arg.seqid);
  4138. }
  4139. static void nfs4_locku_prepare(struct rpc_task *task, void *data)
  4140. {
  4141. struct nfs4_unlockdata *calldata = data;
  4142. if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
  4143. goto out_wait;
  4144. if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
  4145. /* Note: exit _without_ running nfs4_locku_done */
  4146. goto out_no_action;
  4147. }
  4148. calldata->timestamp = jiffies;
  4149. if (nfs4_setup_sequence(calldata->server,
  4150. &calldata->arg.seq_args,
  4151. &calldata->res.seq_res,
  4152. task) != 0)
  4153. nfs_release_seqid(calldata->arg.seqid);
  4154. return;
  4155. out_no_action:
  4156. task->tk_action = NULL;
  4157. out_wait:
  4158. nfs4_sequence_done(task, &calldata->res.seq_res);
  4159. }
  4160. static const struct rpc_call_ops nfs4_locku_ops = {
  4161. .rpc_call_prepare = nfs4_locku_prepare,
  4162. .rpc_call_done = nfs4_locku_done,
  4163. .rpc_release = nfs4_locku_release_calldata,
  4164. };
  4165. static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
  4166. struct nfs_open_context *ctx,
  4167. struct nfs4_lock_state *lsp,
  4168. struct nfs_seqid *seqid)
  4169. {
  4170. struct nfs4_unlockdata *data;
  4171. struct rpc_message msg = {
  4172. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
  4173. .rpc_cred = ctx->cred,
  4174. };
  4175. struct rpc_task_setup task_setup_data = {
  4176. .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
  4177. .rpc_message = &msg,
  4178. .callback_ops = &nfs4_locku_ops,
  4179. .workqueue = nfsiod_workqueue,
  4180. .flags = RPC_TASK_ASYNC,
  4181. };
  4182. /* Ensure this is an unlock - when canceling a lock, the
  4183. * canceled lock is passed in, and it won't be an unlock.
  4184. */
  4185. fl->fl_type = F_UNLCK;
  4186. data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
  4187. if (data == NULL) {
  4188. nfs_free_seqid(seqid);
  4189. return ERR_PTR(-ENOMEM);
  4190. }
  4191. nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
  4192. msg.rpc_argp = &data->arg;
  4193. msg.rpc_resp = &data->res;
  4194. task_setup_data.callback_data = data;
  4195. return rpc_run_task(&task_setup_data);
  4196. }
  4197. static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
  4198. {
  4199. struct inode *inode = state->inode;
  4200. struct nfs4_state_owner *sp = state->owner;
  4201. struct nfs_inode *nfsi = NFS_I(inode);
  4202. struct nfs_seqid *seqid;
  4203. struct nfs4_lock_state *lsp;
  4204. struct rpc_task *task;
  4205. int status = 0;
  4206. unsigned char fl_flags = request->fl_flags;
  4207. status = nfs4_set_lock_state(state, request);
  4208. /* Unlock _before_ we do the RPC call */
  4209. request->fl_flags |= FL_EXISTS;
  4210. /* Exclude nfs_delegation_claim_locks() */
  4211. mutex_lock(&sp->so_delegreturn_mutex);
  4212. /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
  4213. down_read(&nfsi->rwsem);
  4214. if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
  4215. up_read(&nfsi->rwsem);
  4216. mutex_unlock(&sp->so_delegreturn_mutex);
  4217. goto out;
  4218. }
  4219. up_read(&nfsi->rwsem);
  4220. mutex_unlock(&sp->so_delegreturn_mutex);
  4221. if (status != 0)
  4222. goto out;
  4223. /* Is this a delegated lock? */
  4224. if (test_bit(NFS_DELEGATED_STATE, &state->flags))
  4225. goto out;
  4226. lsp = request->fl_u.nfs4_fl.owner;
  4227. seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
  4228. status = -ENOMEM;
  4229. if (seqid == NULL)
  4230. goto out;
  4231. task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
  4232. status = PTR_ERR(task);
  4233. if (IS_ERR(task))
  4234. goto out;
  4235. status = nfs4_wait_for_completion_rpc_task(task);
  4236. rpc_put_task(task);
  4237. out:
  4238. request->fl_flags = fl_flags;
  4239. return status;
  4240. }
  4241. struct nfs4_lockdata {
  4242. struct nfs_lock_args arg;
  4243. struct nfs_lock_res res;
  4244. struct nfs4_lock_state *lsp;
  4245. struct nfs_open_context *ctx;
  4246. struct file_lock fl;
  4247. unsigned long timestamp;
  4248. int rpc_status;
  4249. int cancelled;
  4250. struct nfs_server *server;
  4251. };
  4252. static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
  4253. struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
  4254. gfp_t gfp_mask)
  4255. {
  4256. struct nfs4_lockdata *p;
  4257. struct inode *inode = lsp->ls_state->inode;
  4258. struct nfs_server *server = NFS_SERVER(inode);
  4259. p = kzalloc(sizeof(*p), gfp_mask);
  4260. if (p == NULL)
  4261. return NULL;
  4262. p->arg.fh = NFS_FH(inode);
  4263. p->arg.fl = &p->fl;
  4264. p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
  4265. if (p->arg.open_seqid == NULL)
  4266. goto out_free;
  4267. p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
  4268. if (p->arg.lock_seqid == NULL)
  4269. goto out_free_seqid;
  4270. p->arg.lock_stateid = &lsp->ls_stateid;
  4271. p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
  4272. p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
  4273. p->arg.lock_owner.s_dev = server->s_dev;
  4274. p->res.lock_seqid = p->arg.lock_seqid;
  4275. p->lsp = lsp;
  4276. p->server = server;
  4277. atomic_inc(&lsp->ls_count);
  4278. p->ctx = get_nfs_open_context(ctx);
  4279. memcpy(&p->fl, fl, sizeof(p->fl));
  4280. return p;
  4281. out_free_seqid:
  4282. nfs_free_seqid(p->arg.open_seqid);
  4283. out_free:
  4284. kfree(p);
  4285. return NULL;
  4286. }
  4287. static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
  4288. {
  4289. struct nfs4_lockdata *data = calldata;
  4290. struct nfs4_state *state = data->lsp->ls_state;
  4291. dprintk("%s: begin!\n", __func__);
  4292. if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
  4293. goto out_wait;
  4294. /* Do we need to do an open_to_lock_owner? */
  4295. if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
  4296. if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
  4297. goto out_release_lock_seqid;
  4298. }
  4299. data->arg.open_stateid = &state->stateid;
  4300. data->arg.new_lock_owner = 1;
  4301. data->res.open_seqid = data->arg.open_seqid;
  4302. } else
  4303. data->arg.new_lock_owner = 0;
  4304. if (!nfs4_valid_open_stateid(state)) {
  4305. data->rpc_status = -EBADF;
  4306. task->tk_action = NULL;
  4307. goto out_release_open_seqid;
  4308. }
  4309. data->timestamp = jiffies;
  4310. if (nfs4_setup_sequence(data->server,
  4311. &data->arg.seq_args,
  4312. &data->res.seq_res,
  4313. task) == 0)
  4314. return;
  4315. out_release_open_seqid:
  4316. nfs_release_seqid(data->arg.open_seqid);
  4317. out_release_lock_seqid:
  4318. nfs_release_seqid(data->arg.lock_seqid);
  4319. out_wait:
  4320. nfs4_sequence_done(task, &data->res.seq_res);
  4321. dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
  4322. }
  4323. static void nfs4_lock_done(struct rpc_task *task, void *calldata)
  4324. {
  4325. struct nfs4_lockdata *data = calldata;
  4326. dprintk("%s: begin!\n", __func__);
  4327. if (!nfs4_sequence_done(task, &data->res.seq_res))
  4328. return;
  4329. data->rpc_status = task->tk_status;
  4330. if (data->arg.new_lock_owner != 0) {
  4331. if (data->rpc_status == 0)
  4332. nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
  4333. else
  4334. goto out;
  4335. }
  4336. if (data->rpc_status == 0) {
  4337. nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
  4338. set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
  4339. renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
  4340. }
  4341. out:
  4342. dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
  4343. }
  4344. static void nfs4_lock_release(void *calldata)
  4345. {
  4346. struct nfs4_lockdata *data = calldata;
  4347. dprintk("%s: begin!\n", __func__);
  4348. nfs_free_seqid(data->arg.open_seqid);
  4349. if (data->cancelled != 0) {
  4350. struct rpc_task *task;
  4351. task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
  4352. data->arg.lock_seqid);
  4353. if (!IS_ERR(task))
  4354. rpc_put_task_async(task);
  4355. dprintk("%s: cancelling lock!\n", __func__);
  4356. } else
  4357. nfs_free_seqid(data->arg.lock_seqid);
  4358. nfs4_put_lock_state(data->lsp);
  4359. put_nfs_open_context(data->ctx);
  4360. kfree(data);
  4361. dprintk("%s: done!\n", __func__);
  4362. }
  4363. static const struct rpc_call_ops nfs4_lock_ops = {
  4364. .rpc_call_prepare = nfs4_lock_prepare,
  4365. .rpc_call_done = nfs4_lock_done,
  4366. .rpc_release = nfs4_lock_release,
  4367. };
  4368. static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
  4369. {
  4370. switch (error) {
  4371. case -NFS4ERR_ADMIN_REVOKED:
  4372. case -NFS4ERR_BAD_STATEID:
  4373. lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
  4374. if (new_lock_owner != 0 ||
  4375. test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
  4376. nfs4_schedule_stateid_recovery(server, lsp->ls_state);
  4377. break;
  4378. case -NFS4ERR_STALE_STATEID:
  4379. lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
  4380. case -NFS4ERR_EXPIRED:
  4381. nfs4_schedule_lease_recovery(server->nfs_client);
  4382. };
  4383. }
  4384. static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
  4385. {
  4386. struct nfs4_lockdata *data;
  4387. struct rpc_task *task;
  4388. struct rpc_message msg = {
  4389. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
  4390. .rpc_cred = state->owner->so_cred,
  4391. };
  4392. struct rpc_task_setup task_setup_data = {
  4393. .rpc_client = NFS_CLIENT(state->inode),
  4394. .rpc_message = &msg,
  4395. .callback_ops = &nfs4_lock_ops,
  4396. .workqueue = nfsiod_workqueue,
  4397. .flags = RPC_TASK_ASYNC,
  4398. };
  4399. int ret;
  4400. dprintk("%s: begin!\n", __func__);
  4401. data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
  4402. fl->fl_u.nfs4_fl.owner,
  4403. recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
  4404. if (data == NULL)
  4405. return -ENOMEM;
  4406. if (IS_SETLKW(cmd))
  4407. data->arg.block = 1;
  4408. nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
  4409. msg.rpc_argp = &data->arg;
  4410. msg.rpc_resp = &data->res;
  4411. task_setup_data.callback_data = data;
  4412. if (recovery_type > NFS_LOCK_NEW) {
  4413. if (recovery_type == NFS_LOCK_RECLAIM)
  4414. data->arg.reclaim = NFS_LOCK_RECLAIM;
  4415. nfs4_set_sequence_privileged(&data->arg.seq_args);
  4416. }
  4417. task = rpc_run_task(&task_setup_data);
  4418. if (IS_ERR(task))
  4419. return PTR_ERR(task);
  4420. ret = nfs4_wait_for_completion_rpc_task(task);
  4421. if (ret == 0) {
  4422. ret = data->rpc_status;
  4423. if (ret)
  4424. nfs4_handle_setlk_error(data->server, data->lsp,
  4425. data->arg.new_lock_owner, ret);
  4426. } else
  4427. data->cancelled = 1;
  4428. rpc_put_task(task);
  4429. dprintk("%s: done, ret = %d!\n", __func__, ret);
  4430. return ret;
  4431. }
  4432. static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
  4433. {
  4434. struct nfs_server *server = NFS_SERVER(state->inode);
  4435. struct nfs4_exception exception = {
  4436. .inode = state->inode,
  4437. };
  4438. int err;
  4439. do {
  4440. /* Cache the lock if possible... */
  4441. if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
  4442. return 0;
  4443. err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
  4444. if (err != -NFS4ERR_DELAY)
  4445. break;
  4446. nfs4_handle_exception(server, err, &exception);
  4447. } while (exception.retry);
  4448. return err;
  4449. }
  4450. static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
  4451. {
  4452. struct nfs_server *server = NFS_SERVER(state->inode);
  4453. struct nfs4_exception exception = {
  4454. .inode = state->inode,
  4455. };
  4456. int err;
  4457. err = nfs4_set_lock_state(state, request);
  4458. if (err != 0)
  4459. return err;
  4460. do {
  4461. if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
  4462. return 0;
  4463. err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
  4464. switch (err) {
  4465. default:
  4466. goto out;
  4467. case -NFS4ERR_GRACE:
  4468. case -NFS4ERR_DELAY:
  4469. nfs4_handle_exception(server, err, &exception);
  4470. err = 0;
  4471. }
  4472. } while (exception.retry);
  4473. out:
  4474. return err;
  4475. }
  4476. #if defined(CONFIG_NFS_V4_1)
  4477. /**
  4478. * nfs41_check_expired_locks - possibly free a lock stateid
  4479. *
  4480. * @state: NFSv4 state for an inode
  4481. *
  4482. * Returns NFS_OK if recovery for this stateid is now finished.
  4483. * Otherwise a negative NFS4ERR value is returned.
  4484. */
  4485. static int nfs41_check_expired_locks(struct nfs4_state *state)
  4486. {
  4487. int status, ret = -NFS4ERR_BAD_STATEID;
  4488. struct nfs4_lock_state *lsp;
  4489. struct nfs_server *server = NFS_SERVER(state->inode);
  4490. list_for_each_entry(lsp, &state->lock_states, ls_locks) {
  4491. if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
  4492. status = nfs41_test_stateid(server, &lsp->ls_stateid);
  4493. if (status != NFS_OK) {
  4494. /* Free the stateid unless the server
  4495. * informs us the stateid is unrecognized. */
  4496. if (status != -NFS4ERR_BAD_STATEID)
  4497. nfs41_free_stateid(server,
  4498. &lsp->ls_stateid);
  4499. clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
  4500. ret = status;
  4501. }
  4502. }
  4503. };
  4504. return ret;
  4505. }
  4506. static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
  4507. {
  4508. int status = NFS_OK;
  4509. if (test_bit(LK_STATE_IN_USE, &state->flags))
  4510. status = nfs41_check_expired_locks(state);
  4511. if (status != NFS_OK)
  4512. status = nfs4_lock_expired(state, request);
  4513. return status;
  4514. }
  4515. #endif
  4516. static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  4517. {
  4518. struct nfs4_state_owner *sp = state->owner;
  4519. struct nfs_inode *nfsi = NFS_I(state->inode);
  4520. unsigned char fl_flags = request->fl_flags;
  4521. unsigned int seq;
  4522. int status = -ENOLCK;
  4523. if ((fl_flags & FL_POSIX) &&
  4524. !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
  4525. goto out;
  4526. /* Is this a delegated open? */
  4527. status = nfs4_set_lock_state(state, request);
  4528. if (status != 0)
  4529. goto out;
  4530. request->fl_flags |= FL_ACCESS;
  4531. status = do_vfs_lock(request->fl_file, request);
  4532. if (status < 0)
  4533. goto out;
  4534. down_read(&nfsi->rwsem);
  4535. if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
  4536. /* Yes: cache locks! */
  4537. /* ...but avoid races with delegation recall... */
  4538. request->fl_flags = fl_flags & ~FL_SLEEP;
  4539. status = do_vfs_lock(request->fl_file, request);
  4540. goto out_unlock;
  4541. }
  4542. seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
  4543. up_read(&nfsi->rwsem);
  4544. status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
  4545. if (status != 0)
  4546. goto out;
  4547. down_read(&nfsi->rwsem);
  4548. if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
  4549. status = -NFS4ERR_DELAY;
  4550. goto out_unlock;
  4551. }
  4552. /* Note: we always want to sleep here! */
  4553. request->fl_flags = fl_flags | FL_SLEEP;
  4554. if (do_vfs_lock(request->fl_file, request) < 0)
  4555. printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
  4556. "manager!\n", __func__);
  4557. out_unlock:
  4558. up_read(&nfsi->rwsem);
  4559. out:
  4560. request->fl_flags = fl_flags;
  4561. return status;
  4562. }
  4563. static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  4564. {
  4565. struct nfs4_exception exception = {
  4566. .state = state,
  4567. .inode = state->inode,
  4568. };
  4569. int err;
  4570. do {
  4571. err = _nfs4_proc_setlk(state, cmd, request);
  4572. if (err == -NFS4ERR_DENIED)
  4573. err = -EAGAIN;
  4574. err = nfs4_handle_exception(NFS_SERVER(state->inode),
  4575. err, &exception);
  4576. } while (exception.retry);
  4577. return err;
  4578. }
  4579. static int
  4580. nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
  4581. {
  4582. struct nfs_open_context *ctx;
  4583. struct nfs4_state *state;
  4584. unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
  4585. int status;
  4586. /* verify open state */
  4587. ctx = nfs_file_open_context(filp);
  4588. state = ctx->state;
  4589. if (request->fl_start < 0 || request->fl_end < 0)
  4590. return -EINVAL;
  4591. if (IS_GETLK(cmd)) {
  4592. if (state != NULL)
  4593. return nfs4_proc_getlk(state, F_GETLK, request);
  4594. return 0;
  4595. }
  4596. if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
  4597. return -EINVAL;
  4598. if (request->fl_type == F_UNLCK) {
  4599. if (state != NULL)
  4600. return nfs4_proc_unlck(state, cmd, request);
  4601. return 0;
  4602. }
  4603. if (state == NULL)
  4604. return -ENOLCK;
  4605. /*
  4606. * Don't rely on the VFS having checked the file open mode,
  4607. * since it won't do this for flock() locks.
  4608. */
  4609. switch (request->fl_type) {
  4610. case F_RDLCK:
  4611. if (!(filp->f_mode & FMODE_READ))
  4612. return -EBADF;
  4613. break;
  4614. case F_WRLCK:
  4615. if (!(filp->f_mode & FMODE_WRITE))
  4616. return -EBADF;
  4617. }
  4618. do {
  4619. status = nfs4_proc_setlk(state, cmd, request);
  4620. if ((status != -EAGAIN) || IS_SETLK(cmd))
  4621. break;
  4622. timeout = nfs4_set_lock_task_retry(timeout);
  4623. status = -ERESTARTSYS;
  4624. if (signalled())
  4625. break;
  4626. } while(status < 0);
  4627. return status;
  4628. }
  4629. int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
  4630. {
  4631. struct nfs_server *server = NFS_SERVER(state->inode);
  4632. int err;
  4633. err = nfs4_set_lock_state(state, fl);
  4634. if (err != 0)
  4635. goto out;
  4636. err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
  4637. switch (err) {
  4638. default:
  4639. printk(KERN_ERR "NFS: %s: unhandled error "
  4640. "%d.\n", __func__, err);
  4641. case 0:
  4642. case -ESTALE:
  4643. goto out;
  4644. case -NFS4ERR_STALE_CLIENTID:
  4645. case -NFS4ERR_STALE_STATEID:
  4646. set_bit(NFS_DELEGATED_STATE, &state->flags);
  4647. case -NFS4ERR_EXPIRED:
  4648. nfs4_schedule_lease_recovery(server->nfs_client);
  4649. return -EAGAIN;
  4650. case -NFS4ERR_BADSESSION:
  4651. case -NFS4ERR_BADSLOT:
  4652. case -NFS4ERR_BAD_HIGH_SLOT:
  4653. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  4654. case -NFS4ERR_DEADSESSION:
  4655. set_bit(NFS_DELEGATED_STATE, &state->flags);
  4656. nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
  4657. return -EAGAIN;
  4658. case -NFS4ERR_DELEG_REVOKED:
  4659. case -NFS4ERR_ADMIN_REVOKED:
  4660. case -NFS4ERR_BAD_STATEID:
  4661. case -NFS4ERR_OPENMODE:
  4662. nfs4_schedule_stateid_recovery(server, state);
  4663. return 0;
  4664. case -NFS4ERR_DELAY:
  4665. case -NFS4ERR_GRACE:
  4666. set_bit(NFS_DELEGATED_STATE, &state->flags);
  4667. ssleep(1);
  4668. return -EAGAIN;
  4669. case -ENOMEM:
  4670. case -NFS4ERR_DENIED:
  4671. /* kill_proc(fl->fl_pid, SIGLOST, 1); */
  4672. return 0;
  4673. }
  4674. out:
  4675. return err;
  4676. }
  4677. struct nfs_release_lockowner_data {
  4678. struct nfs4_lock_state *lsp;
  4679. struct nfs_server *server;
  4680. struct nfs_release_lockowner_args args;
  4681. };
  4682. static void nfs4_release_lockowner_release(void *calldata)
  4683. {
  4684. struct nfs_release_lockowner_data *data = calldata;
  4685. nfs4_free_lock_state(data->server, data->lsp);
  4686. kfree(calldata);
  4687. }
  4688. static const struct rpc_call_ops nfs4_release_lockowner_ops = {
  4689. .rpc_release = nfs4_release_lockowner_release,
  4690. };
  4691. int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
  4692. {
  4693. struct nfs_server *server = lsp->ls_state->owner->so_server;
  4694. struct nfs_release_lockowner_data *data;
  4695. struct rpc_message msg = {
  4696. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
  4697. };
  4698. if (server->nfs_client->cl_mvops->minor_version != 0)
  4699. return -EINVAL;
  4700. data = kmalloc(sizeof(*data), GFP_NOFS);
  4701. if (!data)
  4702. return -ENOMEM;
  4703. data->lsp = lsp;
  4704. data->server = server;
  4705. data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
  4706. data->args.lock_owner.id = lsp->ls_seqid.owner_id;
  4707. data->args.lock_owner.s_dev = server->s_dev;
  4708. msg.rpc_argp = &data->args;
  4709. rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
  4710. return 0;
  4711. }
  4712. #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
  4713. static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
  4714. const void *buf, size_t buflen,
  4715. int flags, int type)
  4716. {
  4717. if (strcmp(key, "") != 0)
  4718. return -EINVAL;
  4719. return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
  4720. }
  4721. static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
  4722. void *buf, size_t buflen, int type)
  4723. {
  4724. if (strcmp(key, "") != 0)
  4725. return -EINVAL;
  4726. return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
  4727. }
  4728. static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
  4729. size_t list_len, const char *name,
  4730. size_t name_len, int type)
  4731. {
  4732. size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
  4733. if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
  4734. return 0;
  4735. if (list && len <= list_len)
  4736. memcpy(list, XATTR_NAME_NFSV4_ACL, len);
  4737. return len;
  4738. }
  4739. /*
  4740. * nfs_fhget will use either the mounted_on_fileid or the fileid
  4741. */
  4742. static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
  4743. {
  4744. if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
  4745. (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
  4746. (fattr->valid & NFS_ATTR_FATTR_FSID) &&
  4747. (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
  4748. return;
  4749. fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
  4750. NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
  4751. fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
  4752. fattr->nlink = 2;
  4753. }
  4754. static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
  4755. const struct qstr *name,
  4756. struct nfs4_fs_locations *fs_locations,
  4757. struct page *page)
  4758. {
  4759. struct nfs_server *server = NFS_SERVER(dir);
  4760. u32 bitmask[2] = {
  4761. [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
  4762. };
  4763. struct nfs4_fs_locations_arg args = {
  4764. .dir_fh = NFS_FH(dir),
  4765. .name = name,
  4766. .page = page,
  4767. .bitmask = bitmask,
  4768. };
  4769. struct nfs4_fs_locations_res res = {
  4770. .fs_locations = fs_locations,
  4771. };
  4772. struct rpc_message msg = {
  4773. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
  4774. .rpc_argp = &args,
  4775. .rpc_resp = &res,
  4776. };
  4777. int status;
  4778. dprintk("%s: start\n", __func__);
  4779. /* Ask for the fileid of the absent filesystem if mounted_on_fileid
  4780. * is not supported */
  4781. if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
  4782. bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
  4783. else
  4784. bitmask[0] |= FATTR4_WORD0_FILEID;
  4785. nfs_fattr_init(&fs_locations->fattr);
  4786. fs_locations->server = server;
  4787. fs_locations->nlocations = 0;
  4788. status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
  4789. dprintk("%s: returned status = %d\n", __func__, status);
  4790. return status;
  4791. }
  4792. int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
  4793. const struct qstr *name,
  4794. struct nfs4_fs_locations *fs_locations,
  4795. struct page *page)
  4796. {
  4797. struct nfs4_exception exception = { };
  4798. int err;
  4799. do {
  4800. err = nfs4_handle_exception(NFS_SERVER(dir),
  4801. _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
  4802. &exception);
  4803. } while (exception.retry);
  4804. return err;
  4805. }
  4806. static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
  4807. {
  4808. int status;
  4809. struct nfs4_secinfo_arg args = {
  4810. .dir_fh = NFS_FH(dir),
  4811. .name = name,
  4812. };
  4813. struct nfs4_secinfo_res res = {
  4814. .flavors = flavors,
  4815. };
  4816. struct rpc_message msg = {
  4817. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
  4818. .rpc_argp = &args,
  4819. .rpc_resp = &res,
  4820. };
  4821. dprintk("NFS call secinfo %s\n", name->name);
  4822. status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
  4823. dprintk("NFS reply secinfo: %d\n", status);
  4824. return status;
  4825. }
  4826. int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
  4827. struct nfs4_secinfo_flavors *flavors)
  4828. {
  4829. struct nfs4_exception exception = { };
  4830. int err;
  4831. do {
  4832. err = nfs4_handle_exception(NFS_SERVER(dir),
  4833. _nfs4_proc_secinfo(dir, name, flavors),
  4834. &exception);
  4835. } while (exception.retry);
  4836. return err;
  4837. }
  4838. #ifdef CONFIG_NFS_V4_1
  4839. /*
  4840. * Check the exchange flags returned by the server for invalid flags, having
  4841. * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
  4842. * DS flags set.
  4843. */
  4844. static int nfs4_check_cl_exchange_flags(u32 flags)
  4845. {
  4846. if (flags & ~EXCHGID4_FLAG_MASK_R)
  4847. goto out_inval;
  4848. if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
  4849. (flags & EXCHGID4_FLAG_USE_NON_PNFS))
  4850. goto out_inval;
  4851. if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
  4852. goto out_inval;
  4853. return NFS_OK;
  4854. out_inval:
  4855. return -NFS4ERR_INVAL;
  4856. }
  4857. static bool
  4858. nfs41_same_server_scope(struct nfs41_server_scope *a,
  4859. struct nfs41_server_scope *b)
  4860. {
  4861. if (a->server_scope_sz == b->server_scope_sz &&
  4862. memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
  4863. return true;
  4864. return false;
  4865. }
  4866. /*
  4867. * nfs4_proc_bind_conn_to_session()
  4868. *
  4869. * The 4.1 client currently uses the same TCP connection for the
  4870. * fore and backchannel.
  4871. */
  4872. int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
  4873. {
  4874. int status;
  4875. struct nfs41_bind_conn_to_session_res res;
  4876. struct rpc_message msg = {
  4877. .rpc_proc =
  4878. &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
  4879. .rpc_argp = clp,
  4880. .rpc_resp = &res,
  4881. .rpc_cred = cred,
  4882. };
  4883. dprintk("--> %s\n", __func__);
  4884. res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
  4885. if (unlikely(res.session == NULL)) {
  4886. status = -ENOMEM;
  4887. goto out;
  4888. }
  4889. status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
  4890. if (status == 0) {
  4891. if (memcmp(res.session->sess_id.data,
  4892. clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
  4893. dprintk("NFS: %s: Session ID mismatch\n", __func__);
  4894. status = -EIO;
  4895. goto out_session;
  4896. }
  4897. if (res.dir != NFS4_CDFS4_BOTH) {
  4898. dprintk("NFS: %s: Unexpected direction from server\n",
  4899. __func__);
  4900. status = -EIO;
  4901. goto out_session;
  4902. }
  4903. if (res.use_conn_in_rdma_mode) {
  4904. dprintk("NFS: %s: Server returned RDMA mode = true\n",
  4905. __func__);
  4906. status = -EIO;
  4907. goto out_session;
  4908. }
  4909. }
  4910. out_session:
  4911. kfree(res.session);
  4912. out:
  4913. dprintk("<-- %s status= %d\n", __func__, status);
  4914. return status;
  4915. }
  4916. /*
  4917. * nfs4_proc_exchange_id()
  4918. *
  4919. * Returns zero, a negative errno, or a negative NFS4ERR status code.
  4920. *
  4921. * Since the clientid has expired, all compounds using sessions
  4922. * associated with the stale clientid will be returning
  4923. * NFS4ERR_BADSESSION in the sequence operation, and will therefore
  4924. * be in some phase of session reset.
  4925. */
  4926. int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
  4927. {
  4928. nfs4_verifier verifier;
  4929. struct nfs41_exchange_id_args args = {
  4930. .verifier = &verifier,
  4931. .client = clp,
  4932. .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
  4933. };
  4934. struct nfs41_exchange_id_res res = {
  4935. 0
  4936. };
  4937. int status;
  4938. struct rpc_message msg = {
  4939. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
  4940. .rpc_argp = &args,
  4941. .rpc_resp = &res,
  4942. .rpc_cred = cred,
  4943. };
  4944. nfs4_init_boot_verifier(clp, &verifier);
  4945. args.id_len = nfs4_init_uniform_client_string(clp, args.id,
  4946. sizeof(args.id));
  4947. dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
  4948. clp->cl_rpcclient->cl_auth->au_ops->au_name,
  4949. args.id_len, args.id);
  4950. res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
  4951. GFP_NOFS);
  4952. if (unlikely(res.server_owner == NULL)) {
  4953. status = -ENOMEM;
  4954. goto out;
  4955. }
  4956. res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
  4957. GFP_NOFS);
  4958. if (unlikely(res.server_scope == NULL)) {
  4959. status = -ENOMEM;
  4960. goto out_server_owner;
  4961. }
  4962. res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
  4963. if (unlikely(res.impl_id == NULL)) {
  4964. status = -ENOMEM;
  4965. goto out_server_scope;
  4966. }
  4967. status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
  4968. if (status == 0)
  4969. status = nfs4_check_cl_exchange_flags(res.flags);
  4970. if (status == 0) {
  4971. clp->cl_clientid = res.clientid;
  4972. clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
  4973. if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
  4974. clp->cl_seqid = res.seqid;
  4975. kfree(clp->cl_serverowner);
  4976. clp->cl_serverowner = res.server_owner;
  4977. res.server_owner = NULL;
  4978. /* use the most recent implementation id */
  4979. kfree(clp->cl_implid);
  4980. clp->cl_implid = res.impl_id;
  4981. if (clp->cl_serverscope != NULL &&
  4982. !nfs41_same_server_scope(clp->cl_serverscope,
  4983. res.server_scope)) {
  4984. dprintk("%s: server_scope mismatch detected\n",
  4985. __func__);
  4986. set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
  4987. kfree(clp->cl_serverscope);
  4988. clp->cl_serverscope = NULL;
  4989. }
  4990. if (clp->cl_serverscope == NULL) {
  4991. clp->cl_serverscope = res.server_scope;
  4992. goto out;
  4993. }
  4994. } else
  4995. kfree(res.impl_id);
  4996. out_server_owner:
  4997. kfree(res.server_owner);
  4998. out_server_scope:
  4999. kfree(res.server_scope);
  5000. out:
  5001. if (clp->cl_implid != NULL)
  5002. dprintk("NFS reply exchange_id: Server Implementation ID: "
  5003. "domain: %s, name: %s, date: %llu,%u\n",
  5004. clp->cl_implid->domain, clp->cl_implid->name,
  5005. clp->cl_implid->date.seconds,
  5006. clp->cl_implid->date.nseconds);
  5007. dprintk("NFS reply exchange_id: %d\n", status);
  5008. return status;
  5009. }
  5010. static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
  5011. struct rpc_cred *cred)
  5012. {
  5013. struct rpc_message msg = {
  5014. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
  5015. .rpc_argp = clp,
  5016. .rpc_cred = cred,
  5017. };
  5018. int status;
  5019. status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
  5020. if (status)
  5021. dprintk("NFS: Got error %d from the server %s on "
  5022. "DESTROY_CLIENTID.", status, clp->cl_hostname);
  5023. return status;
  5024. }
  5025. static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
  5026. struct rpc_cred *cred)
  5027. {
  5028. unsigned int loop;
  5029. int ret;
  5030. for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
  5031. ret = _nfs4_proc_destroy_clientid(clp, cred);
  5032. switch (ret) {
  5033. case -NFS4ERR_DELAY:
  5034. case -NFS4ERR_CLIENTID_BUSY:
  5035. ssleep(1);
  5036. break;
  5037. default:
  5038. return ret;
  5039. }
  5040. }
  5041. return 0;
  5042. }
  5043. int nfs4_destroy_clientid(struct nfs_client *clp)
  5044. {
  5045. struct rpc_cred *cred;
  5046. int ret = 0;
  5047. if (clp->cl_mvops->minor_version < 1)
  5048. goto out;
  5049. if (clp->cl_exchange_flags == 0)
  5050. goto out;
  5051. if (clp->cl_preserve_clid)
  5052. goto out;
  5053. cred = nfs4_get_exchange_id_cred(clp);
  5054. ret = nfs4_proc_destroy_clientid(clp, cred);
  5055. if (cred)
  5056. put_rpccred(cred);
  5057. switch (ret) {
  5058. case 0:
  5059. case -NFS4ERR_STALE_CLIENTID:
  5060. clp->cl_exchange_flags = 0;
  5061. }
  5062. out:
  5063. return ret;
  5064. }
  5065. struct nfs4_get_lease_time_data {
  5066. struct nfs4_get_lease_time_args *args;
  5067. struct nfs4_get_lease_time_res *res;
  5068. struct nfs_client *clp;
  5069. };
  5070. static void nfs4_get_lease_time_prepare(struct rpc_task *task,
  5071. void *calldata)
  5072. {
  5073. struct nfs4_get_lease_time_data *data =
  5074. (struct nfs4_get_lease_time_data *)calldata;
  5075. dprintk("--> %s\n", __func__);
  5076. /* just setup sequence, do not trigger session recovery
  5077. since we're invoked within one */
  5078. nfs41_setup_sequence(data->clp->cl_session,
  5079. &data->args->la_seq_args,
  5080. &data->res->lr_seq_res,
  5081. task);
  5082. dprintk("<-- %s\n", __func__);
  5083. }
  5084. /*
  5085. * Called from nfs4_state_manager thread for session setup, so don't recover
  5086. * from sequence operation or clientid errors.
  5087. */
  5088. static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
  5089. {
  5090. struct nfs4_get_lease_time_data *data =
  5091. (struct nfs4_get_lease_time_data *)calldata;
  5092. dprintk("--> %s\n", __func__);
  5093. if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
  5094. return;
  5095. switch (task->tk_status) {
  5096. case -NFS4ERR_DELAY:
  5097. case -NFS4ERR_GRACE:
  5098. dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
  5099. rpc_delay(task, NFS4_POLL_RETRY_MIN);
  5100. task->tk_status = 0;
  5101. /* fall through */
  5102. case -NFS4ERR_RETRY_UNCACHED_REP:
  5103. rpc_restart_call_prepare(task);
  5104. return;
  5105. }
  5106. dprintk("<-- %s\n", __func__);
  5107. }
  5108. static const struct rpc_call_ops nfs4_get_lease_time_ops = {
  5109. .rpc_call_prepare = nfs4_get_lease_time_prepare,
  5110. .rpc_call_done = nfs4_get_lease_time_done,
  5111. };
  5112. int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
  5113. {
  5114. struct rpc_task *task;
  5115. struct nfs4_get_lease_time_args args;
  5116. struct nfs4_get_lease_time_res res = {
  5117. .lr_fsinfo = fsinfo,
  5118. };
  5119. struct nfs4_get_lease_time_data data = {
  5120. .args = &args,
  5121. .res = &res,
  5122. .clp = clp,
  5123. };
  5124. struct rpc_message msg = {
  5125. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
  5126. .rpc_argp = &args,
  5127. .rpc_resp = &res,
  5128. };
  5129. struct rpc_task_setup task_setup = {
  5130. .rpc_client = clp->cl_rpcclient,
  5131. .rpc_message = &msg,
  5132. .callback_ops = &nfs4_get_lease_time_ops,
  5133. .callback_data = &data,
  5134. .flags = RPC_TASK_TIMEOUT,
  5135. };
  5136. int status;
  5137. nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
  5138. nfs4_set_sequence_privileged(&args.la_seq_args);
  5139. dprintk("--> %s\n", __func__);
  5140. task = rpc_run_task(&task_setup);
  5141. if (IS_ERR(task))
  5142. status = PTR_ERR(task);
  5143. else {
  5144. status = task->tk_status;
  5145. rpc_put_task(task);
  5146. }
  5147. dprintk("<-- %s return %d\n", __func__, status);
  5148. return status;
  5149. }
  5150. /*
  5151. * Initialize the values to be used by the client in CREATE_SESSION
  5152. * If nfs4_init_session set the fore channel request and response sizes,
  5153. * use them.
  5154. *
  5155. * Set the back channel max_resp_sz_cached to zero to force the client to
  5156. * always set csa_cachethis to FALSE because the current implementation
  5157. * of the back channel DRC only supports caching the CB_SEQUENCE operation.
  5158. */
  5159. static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
  5160. {
  5161. struct nfs4_session *session = args->client->cl_session;
  5162. unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
  5163. mxresp_sz = session->fc_target_max_resp_sz;
  5164. if (mxrqst_sz == 0)
  5165. mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
  5166. if (mxresp_sz == 0)
  5167. mxresp_sz = NFS_MAX_FILE_IO_SIZE;
  5168. /* Fore channel attributes */
  5169. args->fc_attrs.max_rqst_sz = mxrqst_sz;
  5170. args->fc_attrs.max_resp_sz = mxresp_sz;
  5171. args->fc_attrs.max_ops = NFS4_MAX_OPS;
  5172. args->fc_attrs.max_reqs = max_session_slots;
  5173. dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
  5174. "max_ops=%u max_reqs=%u\n",
  5175. __func__,
  5176. args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
  5177. args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
  5178. /* Back channel attributes */
  5179. args->bc_attrs.max_rqst_sz = PAGE_SIZE;
  5180. args->bc_attrs.max_resp_sz = PAGE_SIZE;
  5181. args->bc_attrs.max_resp_sz_cached = 0;
  5182. args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
  5183. args->bc_attrs.max_reqs = 1;
  5184. dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
  5185. "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
  5186. __func__,
  5187. args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
  5188. args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
  5189. args->bc_attrs.max_reqs);
  5190. }
  5191. static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
  5192. {
  5193. struct nfs4_channel_attrs *sent = &args->fc_attrs;
  5194. struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
  5195. if (rcvd->max_resp_sz > sent->max_resp_sz)
  5196. return -EINVAL;
  5197. /*
  5198. * Our requested max_ops is the minimum we need; we're not
  5199. * prepared to break up compounds into smaller pieces than that.
  5200. * So, no point even trying to continue if the server won't
  5201. * cooperate:
  5202. */
  5203. if (rcvd->max_ops < sent->max_ops)
  5204. return -EINVAL;
  5205. if (rcvd->max_reqs == 0)
  5206. return -EINVAL;
  5207. if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
  5208. rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
  5209. return 0;
  5210. }
  5211. static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
  5212. {
  5213. struct nfs4_channel_attrs *sent = &args->bc_attrs;
  5214. struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
  5215. if (rcvd->max_rqst_sz > sent->max_rqst_sz)
  5216. return -EINVAL;
  5217. if (rcvd->max_resp_sz < sent->max_resp_sz)
  5218. return -EINVAL;
  5219. if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
  5220. return -EINVAL;
  5221. /* These would render the backchannel useless: */
  5222. if (rcvd->max_ops != sent->max_ops)
  5223. return -EINVAL;
  5224. if (rcvd->max_reqs != sent->max_reqs)
  5225. return -EINVAL;
  5226. return 0;
  5227. }
  5228. static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
  5229. struct nfs4_session *session)
  5230. {
  5231. int ret;
  5232. ret = nfs4_verify_fore_channel_attrs(args, session);
  5233. if (ret)
  5234. return ret;
  5235. return nfs4_verify_back_channel_attrs(args, session);
  5236. }
  5237. static int _nfs4_proc_create_session(struct nfs_client *clp,
  5238. struct rpc_cred *cred)
  5239. {
  5240. struct nfs4_session *session = clp->cl_session;
  5241. struct nfs41_create_session_args args = {
  5242. .client = clp,
  5243. .cb_program = NFS4_CALLBACK,
  5244. };
  5245. struct nfs41_create_session_res res = {
  5246. .client = clp,
  5247. };
  5248. struct rpc_message msg = {
  5249. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
  5250. .rpc_argp = &args,
  5251. .rpc_resp = &res,
  5252. .rpc_cred = cred,
  5253. };
  5254. int status;
  5255. nfs4_init_channel_attrs(&args);
  5256. args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
  5257. status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
  5258. if (!status) {
  5259. /* Verify the session's negotiated channel_attrs values */
  5260. status = nfs4_verify_channel_attrs(&args, session);
  5261. /* Increment the clientid slot sequence id */
  5262. clp->cl_seqid++;
  5263. }
  5264. return status;
  5265. }
  5266. /*
  5267. * Issues a CREATE_SESSION operation to the server.
  5268. * It is the responsibility of the caller to verify the session is
  5269. * expired before calling this routine.
  5270. */
  5271. int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
  5272. {
  5273. int status;
  5274. unsigned *ptr;
  5275. struct nfs4_session *session = clp->cl_session;
  5276. dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
  5277. status = _nfs4_proc_create_session(clp, cred);
  5278. if (status)
  5279. goto out;
  5280. /* Init or reset the session slot tables */
  5281. status = nfs4_setup_session_slot_tables(session);
  5282. dprintk("slot table setup returned %d\n", status);
  5283. if (status)
  5284. goto out;
  5285. ptr = (unsigned *)&session->sess_id.data[0];
  5286. dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
  5287. clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
  5288. out:
  5289. dprintk("<-- %s\n", __func__);
  5290. return status;
  5291. }
  5292. /*
  5293. * Issue the over-the-wire RPC DESTROY_SESSION.
  5294. * The caller must serialize access to this routine.
  5295. */
  5296. int nfs4_proc_destroy_session(struct nfs4_session *session,
  5297. struct rpc_cred *cred)
  5298. {
  5299. struct rpc_message msg = {
  5300. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
  5301. .rpc_argp = session,
  5302. .rpc_cred = cred,
  5303. };
  5304. int status = 0;
  5305. dprintk("--> nfs4_proc_destroy_session\n");
  5306. /* session is still being setup */
  5307. if (session->clp->cl_cons_state != NFS_CS_READY)
  5308. return status;
  5309. status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
  5310. if (status)
  5311. dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
  5312. "Session has been destroyed regardless...\n", status);
  5313. dprintk("<-- nfs4_proc_destroy_session\n");
  5314. return status;
  5315. }
  5316. /*
  5317. * Renew the cl_session lease.
  5318. */
  5319. struct nfs4_sequence_data {
  5320. struct nfs_client *clp;
  5321. struct nfs4_sequence_args args;
  5322. struct nfs4_sequence_res res;
  5323. };
  5324. static void nfs41_sequence_release(void *data)
  5325. {
  5326. struct nfs4_sequence_data *calldata = data;
  5327. struct nfs_client *clp = calldata->clp;
  5328. if (atomic_read(&clp->cl_count) > 1)
  5329. nfs4_schedule_state_renewal(clp);
  5330. nfs_put_client(clp);
  5331. kfree(calldata);
  5332. }
  5333. static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
  5334. {
  5335. switch(task->tk_status) {
  5336. case -NFS4ERR_DELAY:
  5337. rpc_delay(task, NFS4_POLL_RETRY_MAX);
  5338. return -EAGAIN;
  5339. default:
  5340. nfs4_schedule_lease_recovery(clp);
  5341. }
  5342. return 0;
  5343. }
  5344. static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
  5345. {
  5346. struct nfs4_sequence_data *calldata = data;
  5347. struct nfs_client *clp = calldata->clp;
  5348. if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
  5349. return;
  5350. if (task->tk_status < 0) {
  5351. dprintk("%s ERROR %d\n", __func__, task->tk_status);
  5352. if (atomic_read(&clp->cl_count) == 1)
  5353. goto out;
  5354. if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
  5355. rpc_restart_call_prepare(task);
  5356. return;
  5357. }
  5358. }
  5359. dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
  5360. out:
  5361. dprintk("<-- %s\n", __func__);
  5362. }
  5363. static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
  5364. {
  5365. struct nfs4_sequence_data *calldata = data;
  5366. struct nfs_client *clp = calldata->clp;
  5367. struct nfs4_sequence_args *args;
  5368. struct nfs4_sequence_res *res;
  5369. args = task->tk_msg.rpc_argp;
  5370. res = task->tk_msg.rpc_resp;
  5371. nfs41_setup_sequence(clp->cl_session, args, res, task);
  5372. }
  5373. static const struct rpc_call_ops nfs41_sequence_ops = {
  5374. .rpc_call_done = nfs41_sequence_call_done,
  5375. .rpc_call_prepare = nfs41_sequence_prepare,
  5376. .rpc_release = nfs41_sequence_release,
  5377. };
  5378. static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
  5379. struct rpc_cred *cred,
  5380. bool is_privileged)
  5381. {
  5382. struct nfs4_sequence_data *calldata;
  5383. struct rpc_message msg = {
  5384. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
  5385. .rpc_cred = cred,
  5386. };
  5387. struct rpc_task_setup task_setup_data = {
  5388. .rpc_client = clp->cl_rpcclient,
  5389. .rpc_message = &msg,
  5390. .callback_ops = &nfs41_sequence_ops,
  5391. .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
  5392. };
  5393. if (!atomic_inc_not_zero(&clp->cl_count))
  5394. return ERR_PTR(-EIO);
  5395. calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
  5396. if (calldata == NULL) {
  5397. nfs_put_client(clp);
  5398. return ERR_PTR(-ENOMEM);
  5399. }
  5400. nfs41_init_sequence(&calldata->args, &calldata->res, 0);
  5401. if (is_privileged)
  5402. nfs4_set_sequence_privileged(&calldata->args);
  5403. msg.rpc_argp = &calldata->args;
  5404. msg.rpc_resp = &calldata->res;
  5405. calldata->clp = clp;
  5406. task_setup_data.callback_data = calldata;
  5407. return rpc_run_task(&task_setup_data);
  5408. }
  5409. static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
  5410. {
  5411. struct rpc_task *task;
  5412. int ret = 0;
  5413. if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
  5414. return 0;
  5415. task = _nfs41_proc_sequence(clp, cred, false);
  5416. if (IS_ERR(task))
  5417. ret = PTR_ERR(task);
  5418. else
  5419. rpc_put_task_async(task);
  5420. dprintk("<-- %s status=%d\n", __func__, ret);
  5421. return ret;
  5422. }
  5423. static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
  5424. {
  5425. struct rpc_task *task;
  5426. int ret;
  5427. task = _nfs41_proc_sequence(clp, cred, true);
  5428. if (IS_ERR(task)) {
  5429. ret = PTR_ERR(task);
  5430. goto out;
  5431. }
  5432. ret = rpc_wait_for_completion_task(task);
  5433. if (!ret) {
  5434. struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
  5435. if (task->tk_status == 0)
  5436. nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
  5437. ret = task->tk_status;
  5438. }
  5439. rpc_put_task(task);
  5440. out:
  5441. dprintk("<-- %s status=%d\n", __func__, ret);
  5442. return ret;
  5443. }
  5444. struct nfs4_reclaim_complete_data {
  5445. struct nfs_client *clp;
  5446. struct nfs41_reclaim_complete_args arg;
  5447. struct nfs41_reclaim_complete_res res;
  5448. };
  5449. static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
  5450. {
  5451. struct nfs4_reclaim_complete_data *calldata = data;
  5452. nfs41_setup_sequence(calldata->clp->cl_session,
  5453. &calldata->arg.seq_args,
  5454. &calldata->res.seq_res,
  5455. task);
  5456. }
  5457. static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
  5458. {
  5459. switch(task->tk_status) {
  5460. case 0:
  5461. case -NFS4ERR_COMPLETE_ALREADY:
  5462. case -NFS4ERR_WRONG_CRED: /* What to do here? */
  5463. break;
  5464. case -NFS4ERR_DELAY:
  5465. rpc_delay(task, NFS4_POLL_RETRY_MAX);
  5466. /* fall through */
  5467. case -NFS4ERR_RETRY_UNCACHED_REP:
  5468. return -EAGAIN;
  5469. default:
  5470. nfs4_schedule_lease_recovery(clp);
  5471. }
  5472. return 0;
  5473. }
  5474. static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
  5475. {
  5476. struct nfs4_reclaim_complete_data *calldata = data;
  5477. struct nfs_client *clp = calldata->clp;
  5478. struct nfs4_sequence_res *res = &calldata->res.seq_res;
  5479. dprintk("--> %s\n", __func__);
  5480. if (!nfs41_sequence_done(task, res))
  5481. return;
  5482. if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
  5483. rpc_restart_call_prepare(task);
  5484. return;
  5485. }
  5486. dprintk("<-- %s\n", __func__);
  5487. }
  5488. static void nfs4_free_reclaim_complete_data(void *data)
  5489. {
  5490. struct nfs4_reclaim_complete_data *calldata = data;
  5491. kfree(calldata);
  5492. }
  5493. static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
  5494. .rpc_call_prepare = nfs4_reclaim_complete_prepare,
  5495. .rpc_call_done = nfs4_reclaim_complete_done,
  5496. .rpc_release = nfs4_free_reclaim_complete_data,
  5497. };
  5498. /*
  5499. * Issue a global reclaim complete.
  5500. */
  5501. static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
  5502. {
  5503. struct nfs4_reclaim_complete_data *calldata;
  5504. struct rpc_task *task;
  5505. struct rpc_message msg = {
  5506. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
  5507. };
  5508. struct rpc_task_setup task_setup_data = {
  5509. .rpc_client = clp->cl_rpcclient,
  5510. .rpc_message = &msg,
  5511. .callback_ops = &nfs4_reclaim_complete_call_ops,
  5512. .flags = RPC_TASK_ASYNC,
  5513. };
  5514. int status = -ENOMEM;
  5515. dprintk("--> %s\n", __func__);
  5516. calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
  5517. if (calldata == NULL)
  5518. goto out;
  5519. calldata->clp = clp;
  5520. calldata->arg.one_fs = 0;
  5521. nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
  5522. nfs4_set_sequence_privileged(&calldata->arg.seq_args);
  5523. msg.rpc_argp = &calldata->arg;
  5524. msg.rpc_resp = &calldata->res;
  5525. task_setup_data.callback_data = calldata;
  5526. task = rpc_run_task(&task_setup_data);
  5527. if (IS_ERR(task)) {
  5528. status = PTR_ERR(task);
  5529. goto out;
  5530. }
  5531. status = nfs4_wait_for_completion_rpc_task(task);
  5532. if (status == 0)
  5533. status = task->tk_status;
  5534. rpc_put_task(task);
  5535. return 0;
  5536. out:
  5537. dprintk("<-- %s status=%d\n", __func__, status);
  5538. return status;
  5539. }
  5540. static void
  5541. nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
  5542. {
  5543. struct nfs4_layoutget *lgp = calldata;
  5544. struct nfs_server *server = NFS_SERVER(lgp->args.inode);
  5545. struct nfs4_session *session = nfs4_get_session(server);
  5546. dprintk("--> %s\n", __func__);
  5547. /* Note the is a race here, where a CB_LAYOUTRECALL can come in
  5548. * right now covering the LAYOUTGET we are about to send.
  5549. * However, that is not so catastrophic, and there seems
  5550. * to be no way to prevent it completely.
  5551. */
  5552. if (nfs41_setup_sequence(session, &lgp->args.seq_args,
  5553. &lgp->res.seq_res, task))
  5554. return;
  5555. if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
  5556. NFS_I(lgp->args.inode)->layout,
  5557. lgp->args.ctx->state)) {
  5558. rpc_exit(task, NFS4_OK);
  5559. }
  5560. }
  5561. static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
  5562. {
  5563. struct nfs4_layoutget *lgp = calldata;
  5564. struct inode *inode = lgp->args.inode;
  5565. struct nfs_server *server = NFS_SERVER(inode);
  5566. struct pnfs_layout_hdr *lo;
  5567. struct nfs4_state *state = NULL;
  5568. unsigned long timeo, giveup;
  5569. dprintk("--> %s\n", __func__);
  5570. if (!nfs41_sequence_done(task, &lgp->res.seq_res))
  5571. goto out;
  5572. switch (task->tk_status) {
  5573. case 0:
  5574. goto out;
  5575. case -NFS4ERR_LAYOUTTRYLATER:
  5576. case -NFS4ERR_RECALLCONFLICT:
  5577. timeo = rpc_get_timeout(task->tk_client);
  5578. giveup = lgp->args.timestamp + timeo;
  5579. if (time_after(giveup, jiffies))
  5580. task->tk_status = -NFS4ERR_DELAY;
  5581. break;
  5582. case -NFS4ERR_EXPIRED:
  5583. case -NFS4ERR_BAD_STATEID:
  5584. spin_lock(&inode->i_lock);
  5585. lo = NFS_I(inode)->layout;
  5586. if (!lo || list_empty(&lo->plh_segs)) {
  5587. spin_unlock(&inode->i_lock);
  5588. /* If the open stateid was bad, then recover it. */
  5589. state = lgp->args.ctx->state;
  5590. } else {
  5591. LIST_HEAD(head);
  5592. pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
  5593. spin_unlock(&inode->i_lock);
  5594. /* Mark the bad layout state as invalid, then
  5595. * retry using the open stateid. */
  5596. pnfs_free_lseg_list(&head);
  5597. }
  5598. }
  5599. if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
  5600. rpc_restart_call_prepare(task);
  5601. out:
  5602. dprintk("<-- %s\n", __func__);
  5603. }
  5604. static size_t max_response_pages(struct nfs_server *server)
  5605. {
  5606. u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
  5607. return nfs_page_array_len(0, max_resp_sz);
  5608. }
  5609. static void nfs4_free_pages(struct page **pages, size_t size)
  5610. {
  5611. int i;
  5612. if (!pages)
  5613. return;
  5614. for (i = 0; i < size; i++) {
  5615. if (!pages[i])
  5616. break;
  5617. __free_page(pages[i]);
  5618. }
  5619. kfree(pages);
  5620. }
  5621. static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
  5622. {
  5623. struct page **pages;
  5624. int i;
  5625. pages = kcalloc(size, sizeof(struct page *), gfp_flags);
  5626. if (!pages) {
  5627. dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
  5628. return NULL;
  5629. }
  5630. for (i = 0; i < size; i++) {
  5631. pages[i] = alloc_page(gfp_flags);
  5632. if (!pages[i]) {
  5633. dprintk("%s: failed to allocate page\n", __func__);
  5634. nfs4_free_pages(pages, size);
  5635. return NULL;
  5636. }
  5637. }
  5638. return pages;
  5639. }
  5640. static void nfs4_layoutget_release(void *calldata)
  5641. {
  5642. struct nfs4_layoutget *lgp = calldata;
  5643. struct inode *inode = lgp->args.inode;
  5644. struct nfs_server *server = NFS_SERVER(inode);
  5645. size_t max_pages = max_response_pages(server);
  5646. dprintk("--> %s\n", __func__);
  5647. nfs4_free_pages(lgp->args.layout.pages, max_pages);
  5648. pnfs_put_layout_hdr(NFS_I(inode)->layout);
  5649. put_nfs_open_context(lgp->args.ctx);
  5650. kfree(calldata);
  5651. dprintk("<-- %s\n", __func__);
  5652. }
  5653. static const struct rpc_call_ops nfs4_layoutget_call_ops = {
  5654. .rpc_call_prepare = nfs4_layoutget_prepare,
  5655. .rpc_call_done = nfs4_layoutget_done,
  5656. .rpc_release = nfs4_layoutget_release,
  5657. };
  5658. struct pnfs_layout_segment *
  5659. nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
  5660. {
  5661. struct inode *inode = lgp->args.inode;
  5662. struct nfs_server *server = NFS_SERVER(inode);
  5663. size_t max_pages = max_response_pages(server);
  5664. struct rpc_task *task;
  5665. struct rpc_message msg = {
  5666. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
  5667. .rpc_argp = &lgp->args,
  5668. .rpc_resp = &lgp->res,
  5669. };
  5670. struct rpc_task_setup task_setup_data = {
  5671. .rpc_client = server->client,
  5672. .rpc_message = &msg,
  5673. .callback_ops = &nfs4_layoutget_call_ops,
  5674. .callback_data = lgp,
  5675. .flags = RPC_TASK_ASYNC,
  5676. };
  5677. struct pnfs_layout_segment *lseg = NULL;
  5678. int status = 0;
  5679. dprintk("--> %s\n", __func__);
  5680. lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
  5681. if (!lgp->args.layout.pages) {
  5682. nfs4_layoutget_release(lgp);
  5683. return ERR_PTR(-ENOMEM);
  5684. }
  5685. lgp->args.layout.pglen = max_pages * PAGE_SIZE;
  5686. lgp->args.timestamp = jiffies;
  5687. lgp->res.layoutp = &lgp->args.layout;
  5688. lgp->res.seq_res.sr_slot = NULL;
  5689. nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
  5690. /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
  5691. pnfs_get_layout_hdr(NFS_I(inode)->layout);
  5692. task = rpc_run_task(&task_setup_data);
  5693. if (IS_ERR(task))
  5694. return ERR_CAST(task);
  5695. status = nfs4_wait_for_completion_rpc_task(task);
  5696. if (status == 0)
  5697. status = task->tk_status;
  5698. /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
  5699. if (status == 0 && lgp->res.layoutp->len)
  5700. lseg = pnfs_layout_process(lgp);
  5701. rpc_put_task(task);
  5702. dprintk("<-- %s status=%d\n", __func__, status);
  5703. if (status)
  5704. return ERR_PTR(status);
  5705. return lseg;
  5706. }
  5707. static void
  5708. nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
  5709. {
  5710. struct nfs4_layoutreturn *lrp = calldata;
  5711. dprintk("--> %s\n", __func__);
  5712. nfs41_setup_sequence(lrp->clp->cl_session,
  5713. &lrp->args.seq_args,
  5714. &lrp->res.seq_res,
  5715. task);
  5716. }
  5717. static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
  5718. {
  5719. struct nfs4_layoutreturn *lrp = calldata;
  5720. struct nfs_server *server;
  5721. dprintk("--> %s\n", __func__);
  5722. if (!nfs41_sequence_done(task, &lrp->res.seq_res))
  5723. return;
  5724. server = NFS_SERVER(lrp->args.inode);
  5725. if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
  5726. rpc_restart_call_prepare(task);
  5727. return;
  5728. }
  5729. dprintk("<-- %s\n", __func__);
  5730. }
  5731. static void nfs4_layoutreturn_release(void *calldata)
  5732. {
  5733. struct nfs4_layoutreturn *lrp = calldata;
  5734. struct pnfs_layout_hdr *lo = lrp->args.layout;
  5735. dprintk("--> %s\n", __func__);
  5736. spin_lock(&lo->plh_inode->i_lock);
  5737. if (lrp->res.lrs_present)
  5738. pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
  5739. lo->plh_block_lgets--;
  5740. spin_unlock(&lo->plh_inode->i_lock);
  5741. pnfs_put_layout_hdr(lrp->args.layout);
  5742. kfree(calldata);
  5743. dprintk("<-- %s\n", __func__);
  5744. }
  5745. static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
  5746. .rpc_call_prepare = nfs4_layoutreturn_prepare,
  5747. .rpc_call_done = nfs4_layoutreturn_done,
  5748. .rpc_release = nfs4_layoutreturn_release,
  5749. };
  5750. int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
  5751. {
  5752. struct rpc_task *task;
  5753. struct rpc_message msg = {
  5754. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
  5755. .rpc_argp = &lrp->args,
  5756. .rpc_resp = &lrp->res,
  5757. };
  5758. struct rpc_task_setup task_setup_data = {
  5759. .rpc_client = lrp->clp->cl_rpcclient,
  5760. .rpc_message = &msg,
  5761. .callback_ops = &nfs4_layoutreturn_call_ops,
  5762. .callback_data = lrp,
  5763. };
  5764. int status;
  5765. dprintk("--> %s\n", __func__);
  5766. nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
  5767. task = rpc_run_task(&task_setup_data);
  5768. if (IS_ERR(task))
  5769. return PTR_ERR(task);
  5770. status = task->tk_status;
  5771. dprintk("<-- %s status=%d\n", __func__, status);
  5772. rpc_put_task(task);
  5773. return status;
  5774. }
  5775. /*
  5776. * Retrieve the list of Data Server devices from the MDS.
  5777. */
  5778. static int _nfs4_getdevicelist(struct nfs_server *server,
  5779. const struct nfs_fh *fh,
  5780. struct pnfs_devicelist *devlist)
  5781. {
  5782. struct nfs4_getdevicelist_args args = {
  5783. .fh = fh,
  5784. .layoutclass = server->pnfs_curr_ld->id,
  5785. };
  5786. struct nfs4_getdevicelist_res res = {
  5787. .devlist = devlist,
  5788. };
  5789. struct rpc_message msg = {
  5790. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
  5791. .rpc_argp = &args,
  5792. .rpc_resp = &res,
  5793. };
  5794. int status;
  5795. dprintk("--> %s\n", __func__);
  5796. status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
  5797. &res.seq_res, 0);
  5798. dprintk("<-- %s status=%d\n", __func__, status);
  5799. return status;
  5800. }
  5801. int nfs4_proc_getdevicelist(struct nfs_server *server,
  5802. const struct nfs_fh *fh,
  5803. struct pnfs_devicelist *devlist)
  5804. {
  5805. struct nfs4_exception exception = { };
  5806. int err;
  5807. do {
  5808. err = nfs4_handle_exception(server,
  5809. _nfs4_getdevicelist(server, fh, devlist),
  5810. &exception);
  5811. } while (exception.retry);
  5812. dprintk("%s: err=%d, num_devs=%u\n", __func__,
  5813. err, devlist->num_devs);
  5814. return err;
  5815. }
  5816. EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
  5817. static int
  5818. _nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
  5819. {
  5820. struct nfs4_getdeviceinfo_args args = {
  5821. .pdev = pdev,
  5822. };
  5823. struct nfs4_getdeviceinfo_res res = {
  5824. .pdev = pdev,
  5825. };
  5826. struct rpc_message msg = {
  5827. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
  5828. .rpc_argp = &args,
  5829. .rpc_resp = &res,
  5830. };
  5831. int status;
  5832. dprintk("--> %s\n", __func__);
  5833. status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  5834. dprintk("<-- %s status=%d\n", __func__, status);
  5835. return status;
  5836. }
  5837. int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
  5838. {
  5839. struct nfs4_exception exception = { };
  5840. int err;
  5841. do {
  5842. err = nfs4_handle_exception(server,
  5843. _nfs4_proc_getdeviceinfo(server, pdev),
  5844. &exception);
  5845. } while (exception.retry);
  5846. return err;
  5847. }
  5848. EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
  5849. static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
  5850. {
  5851. struct nfs4_layoutcommit_data *data = calldata;
  5852. struct nfs_server *server = NFS_SERVER(data->args.inode);
  5853. struct nfs4_session *session = nfs4_get_session(server);
  5854. nfs41_setup_sequence(session,
  5855. &data->args.seq_args,
  5856. &data->res.seq_res,
  5857. task);
  5858. }
  5859. static void
  5860. nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
  5861. {
  5862. struct nfs4_layoutcommit_data *data = calldata;
  5863. struct nfs_server *server = NFS_SERVER(data->args.inode);
  5864. if (!nfs41_sequence_done(task, &data->res.seq_res))
  5865. return;
  5866. switch (task->tk_status) { /* Just ignore these failures */
  5867. case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
  5868. case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
  5869. case -NFS4ERR_BADLAYOUT: /* no layout */
  5870. case -NFS4ERR_GRACE: /* loca_recalim always false */
  5871. task->tk_status = 0;
  5872. break;
  5873. case 0:
  5874. nfs_post_op_update_inode_force_wcc(data->args.inode,
  5875. data->res.fattr);
  5876. break;
  5877. default:
  5878. if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
  5879. rpc_restart_call_prepare(task);
  5880. return;
  5881. }
  5882. }
  5883. }
  5884. static void nfs4_layoutcommit_release(void *calldata)
  5885. {
  5886. struct nfs4_layoutcommit_data *data = calldata;
  5887. pnfs_cleanup_layoutcommit(data);
  5888. put_rpccred(data->cred);
  5889. kfree(data);
  5890. }
  5891. static const struct rpc_call_ops nfs4_layoutcommit_ops = {
  5892. .rpc_call_prepare = nfs4_layoutcommit_prepare,
  5893. .rpc_call_done = nfs4_layoutcommit_done,
  5894. .rpc_release = nfs4_layoutcommit_release,
  5895. };
  5896. int
  5897. nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
  5898. {
  5899. struct rpc_message msg = {
  5900. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
  5901. .rpc_argp = &data->args,
  5902. .rpc_resp = &data->res,
  5903. .rpc_cred = data->cred,
  5904. };
  5905. struct rpc_task_setup task_setup_data = {
  5906. .task = &data->task,
  5907. .rpc_client = NFS_CLIENT(data->args.inode),
  5908. .rpc_message = &msg,
  5909. .callback_ops = &nfs4_layoutcommit_ops,
  5910. .callback_data = data,
  5911. .flags = RPC_TASK_ASYNC,
  5912. };
  5913. struct rpc_task *task;
  5914. int status = 0;
  5915. dprintk("NFS: %4d initiating layoutcommit call. sync %d "
  5916. "lbw: %llu inode %lu\n",
  5917. data->task.tk_pid, sync,
  5918. data->args.lastbytewritten,
  5919. data->args.inode->i_ino);
  5920. nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
  5921. task = rpc_run_task(&task_setup_data);
  5922. if (IS_ERR(task))
  5923. return PTR_ERR(task);
  5924. if (sync == false)
  5925. goto out;
  5926. status = nfs4_wait_for_completion_rpc_task(task);
  5927. if (status != 0)
  5928. goto out;
  5929. status = task->tk_status;
  5930. out:
  5931. dprintk("%s: status %d\n", __func__, status);
  5932. rpc_put_task(task);
  5933. return status;
  5934. }
  5935. static int
  5936. _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
  5937. struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
  5938. {
  5939. struct nfs41_secinfo_no_name_args args = {
  5940. .style = SECINFO_STYLE_CURRENT_FH,
  5941. };
  5942. struct nfs4_secinfo_res res = {
  5943. .flavors = flavors,
  5944. };
  5945. struct rpc_message msg = {
  5946. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
  5947. .rpc_argp = &args,
  5948. .rpc_resp = &res,
  5949. };
  5950. return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  5951. }
  5952. static int
  5953. nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
  5954. struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
  5955. {
  5956. struct nfs4_exception exception = { };
  5957. int err;
  5958. do {
  5959. err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
  5960. switch (err) {
  5961. case 0:
  5962. case -NFS4ERR_WRONGSEC:
  5963. case -NFS4ERR_NOTSUPP:
  5964. goto out;
  5965. default:
  5966. err = nfs4_handle_exception(server, err, &exception);
  5967. }
  5968. } while (exception.retry);
  5969. out:
  5970. return err;
  5971. }
  5972. static int
  5973. nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
  5974. struct nfs_fsinfo *info)
  5975. {
  5976. int err;
  5977. struct page *page;
  5978. rpc_authflavor_t flavor;
  5979. struct nfs4_secinfo_flavors *flavors;
  5980. page = alloc_page(GFP_KERNEL);
  5981. if (!page) {
  5982. err = -ENOMEM;
  5983. goto out;
  5984. }
  5985. flavors = page_address(page);
  5986. err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
  5987. /*
  5988. * Fall back on "guess and check" method if
  5989. * the server doesn't support SECINFO_NO_NAME
  5990. */
  5991. if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
  5992. err = nfs4_find_root_sec(server, fhandle, info);
  5993. goto out_freepage;
  5994. }
  5995. if (err)
  5996. goto out_freepage;
  5997. flavor = nfs_find_best_sec(flavors);
  5998. if (err == 0)
  5999. err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
  6000. out_freepage:
  6001. put_page(page);
  6002. if (err == -EACCES)
  6003. return -EPERM;
  6004. out:
  6005. return err;
  6006. }
  6007. static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
  6008. {
  6009. int status;
  6010. struct nfs41_test_stateid_args args = {
  6011. .stateid = stateid,
  6012. };
  6013. struct nfs41_test_stateid_res res;
  6014. struct rpc_message msg = {
  6015. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
  6016. .rpc_argp = &args,
  6017. .rpc_resp = &res,
  6018. };
  6019. dprintk("NFS call test_stateid %p\n", stateid);
  6020. nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
  6021. nfs4_set_sequence_privileged(&args.seq_args);
  6022. status = nfs4_call_sync_sequence(server->client, server, &msg,
  6023. &args.seq_args, &res.seq_res);
  6024. if (status != NFS_OK) {
  6025. dprintk("NFS reply test_stateid: failed, %d\n", status);
  6026. return status;
  6027. }
  6028. dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
  6029. return -res.status;
  6030. }
  6031. /**
  6032. * nfs41_test_stateid - perform a TEST_STATEID operation
  6033. *
  6034. * @server: server / transport on which to perform the operation
  6035. * @stateid: state ID to test
  6036. *
  6037. * Returns NFS_OK if the server recognizes that "stateid" is valid.
  6038. * Otherwise a negative NFS4ERR value is returned if the operation
  6039. * failed or the state ID is not currently valid.
  6040. */
  6041. static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
  6042. {
  6043. struct nfs4_exception exception = { };
  6044. int err;
  6045. do {
  6046. err = _nfs41_test_stateid(server, stateid);
  6047. if (err != -NFS4ERR_DELAY)
  6048. break;
  6049. nfs4_handle_exception(server, err, &exception);
  6050. } while (exception.retry);
  6051. return err;
  6052. }
  6053. static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
  6054. {
  6055. struct nfs41_free_stateid_args args = {
  6056. .stateid = stateid,
  6057. };
  6058. struct nfs41_free_stateid_res res;
  6059. struct rpc_message msg = {
  6060. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
  6061. .rpc_argp = &args,
  6062. .rpc_resp = &res,
  6063. };
  6064. int status;
  6065. dprintk("NFS call free_stateid %p\n", stateid);
  6066. nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
  6067. nfs4_set_sequence_privileged(&args.seq_args);
  6068. status = nfs4_call_sync_sequence(server->client, server, &msg,
  6069. &args.seq_args, &res.seq_res);
  6070. dprintk("NFS reply free_stateid: %d\n", status);
  6071. return status;
  6072. }
  6073. /**
  6074. * nfs41_free_stateid - perform a FREE_STATEID operation
  6075. *
  6076. * @server: server / transport on which to perform the operation
  6077. * @stateid: state ID to release
  6078. *
  6079. * Returns NFS_OK if the server freed "stateid". Otherwise a
  6080. * negative NFS4ERR value is returned.
  6081. */
  6082. static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
  6083. {
  6084. struct nfs4_exception exception = { };
  6085. int err;
  6086. do {
  6087. err = _nfs4_free_stateid(server, stateid);
  6088. if (err != -NFS4ERR_DELAY)
  6089. break;
  6090. nfs4_handle_exception(server, err, &exception);
  6091. } while (exception.retry);
  6092. return err;
  6093. }
  6094. static bool nfs41_match_stateid(const nfs4_stateid *s1,
  6095. const nfs4_stateid *s2)
  6096. {
  6097. if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
  6098. return false;
  6099. if (s1->seqid == s2->seqid)
  6100. return true;
  6101. if (s1->seqid == 0 || s2->seqid == 0)
  6102. return true;
  6103. return false;
  6104. }
  6105. #endif /* CONFIG_NFS_V4_1 */
  6106. static bool nfs4_match_stateid(const nfs4_stateid *s1,
  6107. const nfs4_stateid *s2)
  6108. {
  6109. return nfs4_stateid_match(s1, s2);
  6110. }
  6111. static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
  6112. .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
  6113. .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
  6114. .recover_open = nfs4_open_reclaim,
  6115. .recover_lock = nfs4_lock_reclaim,
  6116. .establish_clid = nfs4_init_clientid,
  6117. .get_clid_cred = nfs4_get_setclientid_cred,
  6118. .detect_trunking = nfs40_discover_server_trunking,
  6119. };
  6120. #if defined(CONFIG_NFS_V4_1)
  6121. static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
  6122. .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
  6123. .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
  6124. .recover_open = nfs4_open_reclaim,
  6125. .recover_lock = nfs4_lock_reclaim,
  6126. .establish_clid = nfs41_init_clientid,
  6127. .get_clid_cred = nfs4_get_exchange_id_cred,
  6128. .reclaim_complete = nfs41_proc_reclaim_complete,
  6129. .detect_trunking = nfs41_discover_server_trunking,
  6130. };
  6131. #endif /* CONFIG_NFS_V4_1 */
  6132. static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
  6133. .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
  6134. .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
  6135. .recover_open = nfs4_open_expired,
  6136. .recover_lock = nfs4_lock_expired,
  6137. .establish_clid = nfs4_init_clientid,
  6138. .get_clid_cred = nfs4_get_setclientid_cred,
  6139. };
  6140. #if defined(CONFIG_NFS_V4_1)
  6141. static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
  6142. .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
  6143. .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
  6144. .recover_open = nfs41_open_expired,
  6145. .recover_lock = nfs41_lock_expired,
  6146. .establish_clid = nfs41_init_clientid,
  6147. .get_clid_cred = nfs4_get_exchange_id_cred,
  6148. };
  6149. #endif /* CONFIG_NFS_V4_1 */
  6150. static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
  6151. .sched_state_renewal = nfs4_proc_async_renew,
  6152. .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
  6153. .renew_lease = nfs4_proc_renew,
  6154. };
  6155. #if defined(CONFIG_NFS_V4_1)
  6156. static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
  6157. .sched_state_renewal = nfs41_proc_async_sequence,
  6158. .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
  6159. .renew_lease = nfs4_proc_sequence,
  6160. };
  6161. #endif
  6162. static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
  6163. .minor_version = 0,
  6164. .init_caps = NFS_CAP_READDIRPLUS
  6165. | NFS_CAP_ATOMIC_OPEN
  6166. | NFS_CAP_CHANGE_ATTR
  6167. | NFS_CAP_POSIX_LOCK,
  6168. .call_sync = _nfs4_call_sync,
  6169. .match_stateid = nfs4_match_stateid,
  6170. .find_root_sec = nfs4_find_root_sec,
  6171. .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
  6172. .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
  6173. .state_renewal_ops = &nfs40_state_renewal_ops,
  6174. };
  6175. #if defined(CONFIG_NFS_V4_1)
  6176. static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
  6177. .minor_version = 1,
  6178. .init_caps = NFS_CAP_READDIRPLUS
  6179. | NFS_CAP_ATOMIC_OPEN
  6180. | NFS_CAP_CHANGE_ATTR
  6181. | NFS_CAP_POSIX_LOCK
  6182. | NFS_CAP_STATEID_NFSV41
  6183. | NFS_CAP_ATOMIC_OPEN_V1,
  6184. .call_sync = nfs4_call_sync_sequence,
  6185. .match_stateid = nfs41_match_stateid,
  6186. .find_root_sec = nfs41_find_root_sec,
  6187. .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
  6188. .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
  6189. .state_renewal_ops = &nfs41_state_renewal_ops,
  6190. };
  6191. #endif
  6192. const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
  6193. [0] = &nfs_v4_0_minor_ops,
  6194. #if defined(CONFIG_NFS_V4_1)
  6195. [1] = &nfs_v4_1_minor_ops,
  6196. #endif
  6197. };
  6198. const struct inode_operations nfs4_dir_inode_operations = {
  6199. .create = nfs_create,
  6200. .lookup = nfs_lookup,
  6201. .atomic_open = nfs_atomic_open,
  6202. .link = nfs_link,
  6203. .unlink = nfs_unlink,
  6204. .symlink = nfs_symlink,
  6205. .mkdir = nfs_mkdir,
  6206. .rmdir = nfs_rmdir,
  6207. .mknod = nfs_mknod,
  6208. .rename = nfs_rename,
  6209. .permission = nfs_permission,
  6210. .getattr = nfs_getattr,
  6211. .setattr = nfs_setattr,
  6212. .getxattr = generic_getxattr,
  6213. .setxattr = generic_setxattr,
  6214. .listxattr = generic_listxattr,
  6215. .removexattr = generic_removexattr,
  6216. };
  6217. static const struct inode_operations nfs4_file_inode_operations = {
  6218. .permission = nfs_permission,
  6219. .getattr = nfs_getattr,
  6220. .setattr = nfs_setattr,
  6221. .getxattr = generic_getxattr,
  6222. .setxattr = generic_setxattr,
  6223. .listxattr = generic_listxattr,
  6224. .removexattr = generic_removexattr,
  6225. };
  6226. const struct nfs_rpc_ops nfs_v4_clientops = {
  6227. .version = 4, /* protocol version */
  6228. .dentry_ops = &nfs4_dentry_operations,
  6229. .dir_inode_ops = &nfs4_dir_inode_operations,
  6230. .file_inode_ops = &nfs4_file_inode_operations,
  6231. .file_ops = &nfs4_file_operations,
  6232. .getroot = nfs4_proc_get_root,
  6233. .submount = nfs4_submount,
  6234. .try_mount = nfs4_try_mount,
  6235. .getattr = nfs4_proc_getattr,
  6236. .setattr = nfs4_proc_setattr,
  6237. .lookup = nfs4_proc_lookup,
  6238. .access = nfs4_proc_access,
  6239. .readlink = nfs4_proc_readlink,
  6240. .create = nfs4_proc_create,
  6241. .remove = nfs4_proc_remove,
  6242. .unlink_setup = nfs4_proc_unlink_setup,
  6243. .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
  6244. .unlink_done = nfs4_proc_unlink_done,
  6245. .rename = nfs4_proc_rename,
  6246. .rename_setup = nfs4_proc_rename_setup,
  6247. .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
  6248. .rename_done = nfs4_proc_rename_done,
  6249. .link = nfs4_proc_link,
  6250. .symlink = nfs4_proc_symlink,
  6251. .mkdir = nfs4_proc_mkdir,
  6252. .rmdir = nfs4_proc_remove,
  6253. .readdir = nfs4_proc_readdir,
  6254. .mknod = nfs4_proc_mknod,
  6255. .statfs = nfs4_proc_statfs,
  6256. .fsinfo = nfs4_proc_fsinfo,
  6257. .pathconf = nfs4_proc_pathconf,
  6258. .set_capabilities = nfs4_server_capabilities,
  6259. .decode_dirent = nfs4_decode_dirent,
  6260. .read_setup = nfs4_proc_read_setup,
  6261. .read_pageio_init = pnfs_pageio_init_read,
  6262. .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
  6263. .read_done = nfs4_read_done,
  6264. .write_setup = nfs4_proc_write_setup,
  6265. .write_pageio_init = pnfs_pageio_init_write,
  6266. .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
  6267. .write_done = nfs4_write_done,
  6268. .commit_setup = nfs4_proc_commit_setup,
  6269. .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
  6270. .commit_done = nfs4_commit_done,
  6271. .lock = nfs4_proc_lock,
  6272. .clear_acl_cache = nfs4_zap_acl_attr,
  6273. .close_context = nfs4_close_context,
  6274. .open_context = nfs4_atomic_open,
  6275. .have_delegation = nfs4_have_delegation,
  6276. .return_delegation = nfs4_inode_return_delegation,
  6277. .alloc_client = nfs4_alloc_client,
  6278. .init_client = nfs4_init_client,
  6279. .free_client = nfs4_free_client,
  6280. .create_server = nfs4_create_server,
  6281. .clone_server = nfs_clone_server,
  6282. };
  6283. static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
  6284. .prefix = XATTR_NAME_NFSV4_ACL,
  6285. .list = nfs4_xattr_list_nfs4_acl,
  6286. .get = nfs4_xattr_get_nfs4_acl,
  6287. .set = nfs4_xattr_set_nfs4_acl,
  6288. };
  6289. const struct xattr_handler *nfs4_xattr_handlers[] = {
  6290. &nfs4_xattr_nfs4_acl_handler,
  6291. NULL
  6292. };
  6293. /*
  6294. * Local variables:
  6295. * c-basic-offset: 8
  6296. * End:
  6297. */