nfs4proc.c 230 KB

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