nfs4proc.c 230 KB

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