nfs4proc.c 259 KB

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