nfs4proc.c 229 KB

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