nfs4proc.c 232 KB

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