nfs4proc.c 253 KB

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