nfs4proc.c 234 KB

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