nfs4proc.c 260 KB

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