nfs4proc.c 259 KB

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