nfs4proc.c 259 KB

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