nfs4proc.c 238 KB

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