nfs4proc.c 259 KB

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