nfs4proc.c 179 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711
  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/sunrpc/gss_api.h>
  46. #include <linux/nfs.h>
  47. #include <linux/nfs4.h>
  48. #include <linux/nfs_fs.h>
  49. #include <linux/nfs_page.h>
  50. #include <linux/nfs_mount.h>
  51. #include <linux/namei.h>
  52. #include <linux/mount.h>
  53. #include <linux/module.h>
  54. #include <linux/nfs_idmap.h>
  55. #include <linux/sunrpc/bc_xprt.h>
  56. #include <linux/xattr.h>
  57. #include <linux/utsname.h>
  58. #include <linux/freezer.h>
  59. #include "nfs4_fs.h"
  60. #include "delegation.h"
  61. #include "internal.h"
  62. #include "iostat.h"
  63. #include "callback.h"
  64. #include "pnfs.h"
  65. #define NFSDBG_FACILITY NFSDBG_PROC
  66. #define NFS4_POLL_RETRY_MIN (HZ/10)
  67. #define NFS4_POLL_RETRY_MAX (15*HZ)
  68. #define NFS4_MAX_LOOP_ON_RECOVER (10)
  69. static unsigned short max_session_slots = NFS4_DEF_SLOT_TABLE_SIZE;
  70. struct nfs4_opendata;
  71. static int _nfs4_proc_open(struct nfs4_opendata *data);
  72. static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
  73. static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
  74. static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
  75. static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
  76. static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
  77. static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
  78. struct nfs_fattr *fattr, struct iattr *sattr,
  79. struct nfs4_state *state);
  80. #ifdef CONFIG_NFS_V4_1
  81. static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
  82. static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
  83. #endif
  84. /* Prevent leaks of NFSv4 errors into userland */
  85. static int nfs4_map_errors(int err)
  86. {
  87. if (err >= -1000)
  88. return err;
  89. switch (err) {
  90. case -NFS4ERR_RESOURCE:
  91. return -EREMOTEIO;
  92. case -NFS4ERR_WRONGSEC:
  93. return -EPERM;
  94. case -NFS4ERR_BADOWNER:
  95. case -NFS4ERR_BADNAME:
  96. return -EINVAL;
  97. case -NFS4ERR_SHARE_DENIED:
  98. return -EACCES;
  99. default:
  100. dprintk("%s could not handle NFSv4 error %d\n",
  101. __func__, -err);
  102. break;
  103. }
  104. return -EIO;
  105. }
  106. /*
  107. * This is our standard bitmap for GETATTR requests.
  108. */
  109. const u32 nfs4_fattr_bitmap[2] = {
  110. FATTR4_WORD0_TYPE
  111. | FATTR4_WORD0_CHANGE
  112. | FATTR4_WORD0_SIZE
  113. | FATTR4_WORD0_FSID
  114. | FATTR4_WORD0_FILEID,
  115. FATTR4_WORD1_MODE
  116. | FATTR4_WORD1_NUMLINKS
  117. | FATTR4_WORD1_OWNER
  118. | FATTR4_WORD1_OWNER_GROUP
  119. | FATTR4_WORD1_RAWDEV
  120. | FATTR4_WORD1_SPACE_USED
  121. | FATTR4_WORD1_TIME_ACCESS
  122. | FATTR4_WORD1_TIME_METADATA
  123. | FATTR4_WORD1_TIME_MODIFY
  124. };
  125. const u32 nfs4_statfs_bitmap[2] = {
  126. FATTR4_WORD0_FILES_AVAIL
  127. | FATTR4_WORD0_FILES_FREE
  128. | FATTR4_WORD0_FILES_TOTAL,
  129. FATTR4_WORD1_SPACE_AVAIL
  130. | FATTR4_WORD1_SPACE_FREE
  131. | FATTR4_WORD1_SPACE_TOTAL
  132. };
  133. const u32 nfs4_pathconf_bitmap[2] = {
  134. FATTR4_WORD0_MAXLINK
  135. | FATTR4_WORD0_MAXNAME,
  136. 0
  137. };
  138. const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
  139. | FATTR4_WORD0_MAXREAD
  140. | FATTR4_WORD0_MAXWRITE
  141. | FATTR4_WORD0_LEASE_TIME,
  142. FATTR4_WORD1_TIME_DELTA
  143. | FATTR4_WORD1_FS_LAYOUT_TYPES,
  144. FATTR4_WORD2_LAYOUT_BLKSIZE
  145. };
  146. const u32 nfs4_fs_locations_bitmap[2] = {
  147. FATTR4_WORD0_TYPE
  148. | FATTR4_WORD0_CHANGE
  149. | FATTR4_WORD0_SIZE
  150. | FATTR4_WORD0_FSID
  151. | FATTR4_WORD0_FILEID
  152. | FATTR4_WORD0_FS_LOCATIONS,
  153. FATTR4_WORD1_MODE
  154. | FATTR4_WORD1_NUMLINKS
  155. | FATTR4_WORD1_OWNER
  156. | FATTR4_WORD1_OWNER_GROUP
  157. | FATTR4_WORD1_RAWDEV
  158. | FATTR4_WORD1_SPACE_USED
  159. | FATTR4_WORD1_TIME_ACCESS
  160. | FATTR4_WORD1_TIME_METADATA
  161. | FATTR4_WORD1_TIME_MODIFY
  162. | FATTR4_WORD1_MOUNTED_ON_FILEID
  163. };
  164. static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
  165. struct nfs4_readdir_arg *readdir)
  166. {
  167. __be32 *start, *p;
  168. BUG_ON(readdir->count < 80);
  169. if (cookie > 2) {
  170. readdir->cookie = cookie;
  171. memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
  172. return;
  173. }
  174. readdir->cookie = 0;
  175. memset(&readdir->verifier, 0, sizeof(readdir->verifier));
  176. if (cookie == 2)
  177. return;
  178. /*
  179. * NFSv4 servers do not return entries for '.' and '..'
  180. * Therefore, we fake these entries here. We let '.'
  181. * have cookie 0 and '..' have cookie 1. Note that
  182. * when talking to the server, we always send cookie 0
  183. * instead of 1 or 2.
  184. */
  185. start = p = kmap_atomic(*readdir->pages);
  186. if (cookie == 0) {
  187. *p++ = xdr_one; /* next */
  188. *p++ = xdr_zero; /* cookie, first word */
  189. *p++ = xdr_one; /* cookie, second word */
  190. *p++ = xdr_one; /* entry len */
  191. memcpy(p, ".\0\0\0", 4); /* entry */
  192. p++;
  193. *p++ = xdr_one; /* bitmap length */
  194. *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
  195. *p++ = htonl(8); /* attribute buffer length */
  196. p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
  197. }
  198. *p++ = xdr_one; /* next */
  199. *p++ = xdr_zero; /* cookie, first word */
  200. *p++ = xdr_two; /* cookie, second word */
  201. *p++ = xdr_two; /* entry len */
  202. memcpy(p, "..\0\0", 4); /* entry */
  203. p++;
  204. *p++ = xdr_one; /* bitmap length */
  205. *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
  206. *p++ = htonl(8); /* attribute buffer length */
  207. p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
  208. readdir->pgbase = (char *)p - (char *)start;
  209. readdir->count -= readdir->pgbase;
  210. kunmap_atomic(start);
  211. }
  212. static int nfs4_wait_clnt_recover(struct nfs_client *clp)
  213. {
  214. int res;
  215. might_sleep();
  216. res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
  217. nfs_wait_bit_killable, TASK_KILLABLE);
  218. return res;
  219. }
  220. static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
  221. {
  222. int res = 0;
  223. might_sleep();
  224. if (*timeout <= 0)
  225. *timeout = NFS4_POLL_RETRY_MIN;
  226. if (*timeout > NFS4_POLL_RETRY_MAX)
  227. *timeout = NFS4_POLL_RETRY_MAX;
  228. freezable_schedule_timeout_killable_unsafe(*timeout);
  229. if (fatal_signal_pending(current))
  230. res = -ERESTARTSYS;
  231. *timeout <<= 1;
  232. return res;
  233. }
  234. /* This is the error handling routine for processes that are allowed
  235. * to sleep.
  236. */
  237. static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
  238. {
  239. struct nfs_client *clp = server->nfs_client;
  240. struct nfs4_state *state = exception->state;
  241. struct inode *inode = exception->inode;
  242. int ret = errorcode;
  243. exception->retry = 0;
  244. switch(errorcode) {
  245. case 0:
  246. return 0;
  247. case -NFS4ERR_OPENMODE:
  248. if (inode && nfs_have_delegation(inode, FMODE_READ)) {
  249. nfs_inode_return_delegation(inode);
  250. exception->retry = 1;
  251. return 0;
  252. }
  253. if (state == NULL)
  254. break;
  255. nfs4_schedule_stateid_recovery(server, state);
  256. goto wait_on_recovery;
  257. case -NFS4ERR_DELEG_REVOKED:
  258. case -NFS4ERR_ADMIN_REVOKED:
  259. case -NFS4ERR_BAD_STATEID:
  260. if (state == NULL)
  261. break;
  262. nfs_remove_bad_delegation(state->inode);
  263. nfs4_schedule_stateid_recovery(server, state);
  264. goto wait_on_recovery;
  265. case -NFS4ERR_EXPIRED:
  266. if (state != NULL)
  267. nfs4_schedule_stateid_recovery(server, state);
  268. case -NFS4ERR_STALE_STATEID:
  269. case -NFS4ERR_STALE_CLIENTID:
  270. nfs4_schedule_lease_recovery(clp);
  271. goto wait_on_recovery;
  272. #if defined(CONFIG_NFS_V4_1)
  273. case -NFS4ERR_BADSESSION:
  274. case -NFS4ERR_BADSLOT:
  275. case -NFS4ERR_BAD_HIGH_SLOT:
  276. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  277. case -NFS4ERR_DEADSESSION:
  278. case -NFS4ERR_SEQ_FALSE_RETRY:
  279. case -NFS4ERR_SEQ_MISORDERED:
  280. dprintk("%s ERROR: %d Reset session\n", __func__,
  281. errorcode);
  282. nfs4_schedule_session_recovery(clp->cl_session);
  283. goto wait_on_recovery;
  284. #endif /* defined(CONFIG_NFS_V4_1) */
  285. case -NFS4ERR_FILE_OPEN:
  286. if (exception->timeout > HZ) {
  287. /* We have retried a decent amount, time to
  288. * fail
  289. */
  290. ret = -EBUSY;
  291. break;
  292. }
  293. case -NFS4ERR_GRACE:
  294. case -NFS4ERR_DELAY:
  295. ret = nfs4_delay(server->client, &exception->timeout);
  296. if (ret != 0)
  297. break;
  298. case -NFS4ERR_RETRY_UNCACHED_REP:
  299. case -NFS4ERR_OLD_STATEID:
  300. exception->retry = 1;
  301. break;
  302. case -NFS4ERR_BADOWNER:
  303. /* The following works around a Linux server bug! */
  304. case -NFS4ERR_BADNAME:
  305. if (server->caps & NFS_CAP_UIDGID_NOMAP) {
  306. server->caps &= ~NFS_CAP_UIDGID_NOMAP;
  307. exception->retry = 1;
  308. printk(KERN_WARNING "NFS: v4 server %s "
  309. "does not accept raw "
  310. "uid/gids. "
  311. "Reenabling the idmapper.\n",
  312. server->nfs_client->cl_hostname);
  313. }
  314. }
  315. /* We failed to handle the error */
  316. return nfs4_map_errors(ret);
  317. wait_on_recovery:
  318. ret = nfs4_wait_clnt_recover(clp);
  319. if (ret == 0)
  320. exception->retry = 1;
  321. return ret;
  322. }
  323. static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
  324. {
  325. spin_lock(&clp->cl_lock);
  326. if (time_before(clp->cl_last_renewal,timestamp))
  327. clp->cl_last_renewal = timestamp;
  328. spin_unlock(&clp->cl_lock);
  329. }
  330. static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
  331. {
  332. do_renew_lease(server->nfs_client, timestamp);
  333. }
  334. #if defined(CONFIG_NFS_V4_1)
  335. /*
  336. * nfs4_free_slot - free a slot and efficiently update slot table.
  337. *
  338. * freeing a slot is trivially done by clearing its respective bit
  339. * in the bitmap.
  340. * If the freed slotid equals highest_used_slotid we want to update it
  341. * so that the server would be able to size down the slot table if needed,
  342. * otherwise we know that the highest_used_slotid is still in use.
  343. * When updating highest_used_slotid there may be "holes" in the bitmap
  344. * so we need to scan down from highest_used_slotid to 0 looking for the now
  345. * highest slotid in use.
  346. * If none found, highest_used_slotid is set to NFS4_NO_SLOT.
  347. *
  348. * Must be called while holding tbl->slot_tbl_lock
  349. */
  350. static void
  351. nfs4_free_slot(struct nfs4_slot_table *tbl, u32 slotid)
  352. {
  353. BUG_ON(slotid >= NFS4_MAX_SLOT_TABLE);
  354. /* clear used bit in bitmap */
  355. __clear_bit(slotid, tbl->used_slots);
  356. /* update highest_used_slotid when it is freed */
  357. if (slotid == tbl->highest_used_slotid) {
  358. slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
  359. if (slotid < tbl->max_slots)
  360. tbl->highest_used_slotid = slotid;
  361. else
  362. tbl->highest_used_slotid = NFS4_NO_SLOT;
  363. }
  364. dprintk("%s: slotid %u highest_used_slotid %d\n", __func__,
  365. slotid, tbl->highest_used_slotid);
  366. }
  367. bool nfs4_set_task_privileged(struct rpc_task *task, void *dummy)
  368. {
  369. rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
  370. return true;
  371. }
  372. /*
  373. * Signal state manager thread if session fore channel is drained
  374. */
  375. static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
  376. {
  377. if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
  378. rpc_wake_up_first(&ses->fc_slot_table.slot_tbl_waitq,
  379. nfs4_set_task_privileged, NULL);
  380. return;
  381. }
  382. if (ses->fc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
  383. return;
  384. dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
  385. complete(&ses->fc_slot_table.complete);
  386. }
  387. /*
  388. * Signal state manager thread if session back channel is drained
  389. */
  390. void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
  391. {
  392. if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
  393. ses->bc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
  394. return;
  395. dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
  396. complete(&ses->bc_slot_table.complete);
  397. }
  398. static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
  399. {
  400. struct nfs4_slot_table *tbl;
  401. tbl = &res->sr_session->fc_slot_table;
  402. if (!res->sr_slot) {
  403. /* just wake up the next guy waiting since
  404. * we may have not consumed a slot after all */
  405. dprintk("%s: No slot\n", __func__);
  406. return;
  407. }
  408. spin_lock(&tbl->slot_tbl_lock);
  409. nfs4_free_slot(tbl, res->sr_slot - tbl->slots);
  410. nfs4_check_drain_fc_complete(res->sr_session);
  411. spin_unlock(&tbl->slot_tbl_lock);
  412. res->sr_slot = NULL;
  413. }
  414. static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
  415. {
  416. unsigned long timestamp;
  417. struct nfs_client *clp;
  418. /*
  419. * sr_status remains 1 if an RPC level error occurred. The server
  420. * may or may not have processed the sequence operation..
  421. * Proceed as if the server received and processed the sequence
  422. * operation.
  423. */
  424. if (res->sr_status == 1)
  425. res->sr_status = NFS_OK;
  426. /* don't increment the sequence number if the task wasn't sent */
  427. if (!RPC_WAS_SENT(task))
  428. goto out;
  429. /* Check the SEQUENCE operation status */
  430. switch (res->sr_status) {
  431. case 0:
  432. /* Update the slot's sequence and clientid lease timer */
  433. ++res->sr_slot->seq_nr;
  434. timestamp = res->sr_renewal_time;
  435. clp = res->sr_session->clp;
  436. do_renew_lease(clp, timestamp);
  437. /* Check sequence flags */
  438. if (res->sr_status_flags != 0)
  439. nfs4_schedule_lease_recovery(clp);
  440. break;
  441. case -NFS4ERR_DELAY:
  442. /* The server detected a resend of the RPC call and
  443. * returned NFS4ERR_DELAY as per Section 2.10.6.2
  444. * of RFC5661.
  445. */
  446. dprintk("%s: slot=%td seq=%d: Operation in progress\n",
  447. __func__,
  448. res->sr_slot - res->sr_session->fc_slot_table.slots,
  449. res->sr_slot->seq_nr);
  450. goto out_retry;
  451. default:
  452. /* Just update the slot sequence no. */
  453. ++res->sr_slot->seq_nr;
  454. }
  455. out:
  456. /* The session may be reset by one of the error handlers. */
  457. dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
  458. nfs41_sequence_free_slot(res);
  459. return 1;
  460. out_retry:
  461. if (!rpc_restart_call(task))
  462. goto out;
  463. rpc_delay(task, NFS4_POLL_RETRY_MAX);
  464. return 0;
  465. }
  466. static int nfs4_sequence_done(struct rpc_task *task,
  467. struct nfs4_sequence_res *res)
  468. {
  469. if (res->sr_session == NULL)
  470. return 1;
  471. return nfs41_sequence_done(task, res);
  472. }
  473. /*
  474. * nfs4_find_slot - efficiently look for a free slot
  475. *
  476. * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
  477. * If found, we mark the slot as used, update the highest_used_slotid,
  478. * and respectively set up the sequence operation args.
  479. * The slot number is returned if found, or NFS4_NO_SLOT otherwise.
  480. *
  481. * Note: must be called with under the slot_tbl_lock.
  482. */
  483. static u32
  484. nfs4_find_slot(struct nfs4_slot_table *tbl)
  485. {
  486. u32 slotid;
  487. u32 ret_id = NFS4_NO_SLOT;
  488. dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
  489. __func__, tbl->used_slots[0], tbl->highest_used_slotid,
  490. tbl->max_slots);
  491. slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
  492. if (slotid >= tbl->max_slots)
  493. goto out;
  494. __set_bit(slotid, tbl->used_slots);
  495. if (slotid > tbl->highest_used_slotid ||
  496. tbl->highest_used_slotid == NFS4_NO_SLOT)
  497. tbl->highest_used_slotid = slotid;
  498. ret_id = slotid;
  499. out:
  500. dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
  501. __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
  502. return ret_id;
  503. }
  504. static void nfs41_init_sequence(struct nfs4_sequence_args *args,
  505. struct nfs4_sequence_res *res, int cache_reply)
  506. {
  507. args->sa_session = NULL;
  508. args->sa_cache_this = 0;
  509. if (cache_reply)
  510. args->sa_cache_this = 1;
  511. res->sr_session = NULL;
  512. res->sr_slot = NULL;
  513. }
  514. int nfs41_setup_sequence(struct nfs4_session *session,
  515. struct nfs4_sequence_args *args,
  516. struct nfs4_sequence_res *res,
  517. struct rpc_task *task)
  518. {
  519. struct nfs4_slot *slot;
  520. struct nfs4_slot_table *tbl;
  521. u32 slotid;
  522. dprintk("--> %s\n", __func__);
  523. /* slot already allocated? */
  524. if (res->sr_slot != NULL)
  525. return 0;
  526. tbl = &session->fc_slot_table;
  527. spin_lock(&tbl->slot_tbl_lock);
  528. if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
  529. !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
  530. /* The state manager will wait until the slot table is empty */
  531. rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
  532. spin_unlock(&tbl->slot_tbl_lock);
  533. dprintk("%s session is draining\n", __func__);
  534. return -EAGAIN;
  535. }
  536. if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
  537. !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
  538. rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
  539. spin_unlock(&tbl->slot_tbl_lock);
  540. dprintk("%s enforce FIFO order\n", __func__);
  541. return -EAGAIN;
  542. }
  543. slotid = nfs4_find_slot(tbl);
  544. if (slotid == NFS4_NO_SLOT) {
  545. rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
  546. spin_unlock(&tbl->slot_tbl_lock);
  547. dprintk("<-- %s: no free slots\n", __func__);
  548. return -EAGAIN;
  549. }
  550. spin_unlock(&tbl->slot_tbl_lock);
  551. rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
  552. slot = tbl->slots + slotid;
  553. args->sa_session = session;
  554. args->sa_slotid = slotid;
  555. dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
  556. res->sr_session = session;
  557. res->sr_slot = slot;
  558. res->sr_renewal_time = jiffies;
  559. res->sr_status_flags = 0;
  560. /*
  561. * sr_status is only set in decode_sequence, and so will remain
  562. * set to 1 if an rpc level failure occurs.
  563. */
  564. res->sr_status = 1;
  565. return 0;
  566. }
  567. EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
  568. int nfs4_setup_sequence(const struct nfs_server *server,
  569. struct nfs4_sequence_args *args,
  570. struct nfs4_sequence_res *res,
  571. struct rpc_task *task)
  572. {
  573. struct nfs4_session *session = nfs4_get_session(server);
  574. int ret = 0;
  575. if (session == NULL)
  576. goto out;
  577. dprintk("--> %s clp %p session %p sr_slot %td\n",
  578. __func__, session->clp, session, res->sr_slot ?
  579. res->sr_slot - session->fc_slot_table.slots : -1);
  580. ret = nfs41_setup_sequence(session, args, res, task);
  581. out:
  582. dprintk("<-- %s status=%d\n", __func__, ret);
  583. return ret;
  584. }
  585. struct nfs41_call_sync_data {
  586. const struct nfs_server *seq_server;
  587. struct nfs4_sequence_args *seq_args;
  588. struct nfs4_sequence_res *seq_res;
  589. };
  590. static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
  591. {
  592. struct nfs41_call_sync_data *data = calldata;
  593. dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
  594. if (nfs4_setup_sequence(data->seq_server, data->seq_args,
  595. data->seq_res, task))
  596. return;
  597. rpc_call_start(task);
  598. }
  599. static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
  600. {
  601. rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
  602. nfs41_call_sync_prepare(task, calldata);
  603. }
  604. static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
  605. {
  606. struct nfs41_call_sync_data *data = calldata;
  607. nfs41_sequence_done(task, data->seq_res);
  608. }
  609. static const struct rpc_call_ops nfs41_call_sync_ops = {
  610. .rpc_call_prepare = nfs41_call_sync_prepare,
  611. .rpc_call_done = nfs41_call_sync_done,
  612. };
  613. static const struct rpc_call_ops nfs41_call_priv_sync_ops = {
  614. .rpc_call_prepare = nfs41_call_priv_sync_prepare,
  615. .rpc_call_done = nfs41_call_sync_done,
  616. };
  617. static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
  618. struct nfs_server *server,
  619. struct rpc_message *msg,
  620. struct nfs4_sequence_args *args,
  621. struct nfs4_sequence_res *res,
  622. int privileged)
  623. {
  624. int ret;
  625. struct rpc_task *task;
  626. struct nfs41_call_sync_data data = {
  627. .seq_server = server,
  628. .seq_args = args,
  629. .seq_res = res,
  630. };
  631. struct rpc_task_setup task_setup = {
  632. .rpc_client = clnt,
  633. .rpc_message = msg,
  634. .callback_ops = &nfs41_call_sync_ops,
  635. .callback_data = &data
  636. };
  637. if (privileged)
  638. task_setup.callback_ops = &nfs41_call_priv_sync_ops;
  639. task = rpc_run_task(&task_setup);
  640. if (IS_ERR(task))
  641. ret = PTR_ERR(task);
  642. else {
  643. ret = task->tk_status;
  644. rpc_put_task(task);
  645. }
  646. return ret;
  647. }
  648. int _nfs4_call_sync_session(struct rpc_clnt *clnt,
  649. struct nfs_server *server,
  650. struct rpc_message *msg,
  651. struct nfs4_sequence_args *args,
  652. struct nfs4_sequence_res *res,
  653. int cache_reply)
  654. {
  655. nfs41_init_sequence(args, res, cache_reply);
  656. return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
  657. }
  658. #else
  659. static inline
  660. void nfs41_init_sequence(struct nfs4_sequence_args *args,
  661. struct nfs4_sequence_res *res, int cache_reply)
  662. {
  663. }
  664. static int nfs4_sequence_done(struct rpc_task *task,
  665. struct nfs4_sequence_res *res)
  666. {
  667. return 1;
  668. }
  669. #endif /* CONFIG_NFS_V4_1 */
  670. int _nfs4_call_sync(struct rpc_clnt *clnt,
  671. struct nfs_server *server,
  672. struct rpc_message *msg,
  673. struct nfs4_sequence_args *args,
  674. struct nfs4_sequence_res *res,
  675. int cache_reply)
  676. {
  677. nfs41_init_sequence(args, res, cache_reply);
  678. return rpc_call_sync(clnt, msg, 0);
  679. }
  680. static inline
  681. int nfs4_call_sync(struct rpc_clnt *clnt,
  682. struct nfs_server *server,
  683. struct rpc_message *msg,
  684. struct nfs4_sequence_args *args,
  685. struct nfs4_sequence_res *res,
  686. int cache_reply)
  687. {
  688. return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
  689. args, res, cache_reply);
  690. }
  691. static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
  692. {
  693. struct nfs_inode *nfsi = NFS_I(dir);
  694. spin_lock(&dir->i_lock);
  695. nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
  696. if (!cinfo->atomic || cinfo->before != dir->i_version)
  697. nfs_force_lookup_revalidate(dir);
  698. dir->i_version = cinfo->after;
  699. spin_unlock(&dir->i_lock);
  700. }
  701. struct nfs4_opendata {
  702. struct kref kref;
  703. struct nfs_openargs o_arg;
  704. struct nfs_openres o_res;
  705. struct nfs_open_confirmargs c_arg;
  706. struct nfs_open_confirmres c_res;
  707. struct nfs4_string owner_name;
  708. struct nfs4_string group_name;
  709. struct nfs_fattr f_attr;
  710. struct nfs_fattr dir_attr;
  711. struct dentry *dir;
  712. struct dentry *dentry;
  713. struct nfs4_state_owner *owner;
  714. struct nfs4_state *state;
  715. struct iattr attrs;
  716. unsigned long timestamp;
  717. unsigned int rpc_done : 1;
  718. int rpc_status;
  719. int cancelled;
  720. };
  721. static void nfs4_init_opendata_res(struct nfs4_opendata *p)
  722. {
  723. p->o_res.f_attr = &p->f_attr;
  724. p->o_res.dir_attr = &p->dir_attr;
  725. p->o_res.seqid = p->o_arg.seqid;
  726. p->c_res.seqid = p->c_arg.seqid;
  727. p->o_res.server = p->o_arg.server;
  728. nfs_fattr_init(&p->f_attr);
  729. nfs_fattr_init(&p->dir_attr);
  730. nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
  731. }
  732. static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
  733. struct nfs4_state_owner *sp, fmode_t fmode, int flags,
  734. const struct iattr *attrs,
  735. gfp_t gfp_mask)
  736. {
  737. struct dentry *parent = dget_parent(dentry);
  738. struct inode *dir = parent->d_inode;
  739. struct nfs_server *server = NFS_SERVER(dir);
  740. struct nfs4_opendata *p;
  741. p = kzalloc(sizeof(*p), gfp_mask);
  742. if (p == NULL)
  743. goto err;
  744. p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
  745. if (p->o_arg.seqid == NULL)
  746. goto err_free;
  747. nfs_sb_active(dentry->d_sb);
  748. p->dentry = dget(dentry);
  749. p->dir = parent;
  750. p->owner = sp;
  751. atomic_inc(&sp->so_count);
  752. p->o_arg.fh = NFS_FH(dir);
  753. p->o_arg.open_flags = flags;
  754. p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
  755. p->o_arg.clientid = server->nfs_client->cl_clientid;
  756. p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
  757. p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
  758. p->o_arg.name = &dentry->d_name;
  759. p->o_arg.server = server;
  760. p->o_arg.bitmask = server->attr_bitmask;
  761. p->o_arg.dir_bitmask = server->cache_consistency_bitmask;
  762. p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
  763. if (attrs != NULL && attrs->ia_valid != 0) {
  764. __be32 verf[2];
  765. p->o_arg.u.attrs = &p->attrs;
  766. memcpy(&p->attrs, attrs, sizeof(p->attrs));
  767. verf[0] = jiffies;
  768. verf[1] = current->pid;
  769. memcpy(p->o_arg.u.verifier.data, verf,
  770. sizeof(p->o_arg.u.verifier.data));
  771. }
  772. p->c_arg.fh = &p->o_res.fh;
  773. p->c_arg.stateid = &p->o_res.stateid;
  774. p->c_arg.seqid = p->o_arg.seqid;
  775. nfs4_init_opendata_res(p);
  776. kref_init(&p->kref);
  777. return p;
  778. err_free:
  779. kfree(p);
  780. err:
  781. dput(parent);
  782. return NULL;
  783. }
  784. static void nfs4_opendata_free(struct kref *kref)
  785. {
  786. struct nfs4_opendata *p = container_of(kref,
  787. struct nfs4_opendata, kref);
  788. struct super_block *sb = p->dentry->d_sb;
  789. nfs_free_seqid(p->o_arg.seqid);
  790. if (p->state != NULL)
  791. nfs4_put_open_state(p->state);
  792. nfs4_put_state_owner(p->owner);
  793. dput(p->dir);
  794. dput(p->dentry);
  795. nfs_sb_deactive(sb);
  796. nfs_fattr_free_names(&p->f_attr);
  797. kfree(p);
  798. }
  799. static void nfs4_opendata_put(struct nfs4_opendata *p)
  800. {
  801. if (p != NULL)
  802. kref_put(&p->kref, nfs4_opendata_free);
  803. }
  804. static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
  805. {
  806. int ret;
  807. ret = rpc_wait_for_completion_task(task);
  808. return ret;
  809. }
  810. static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
  811. {
  812. int ret = 0;
  813. if (open_mode & (O_EXCL|O_TRUNC))
  814. goto out;
  815. switch (mode & (FMODE_READ|FMODE_WRITE)) {
  816. case FMODE_READ:
  817. ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
  818. && state->n_rdonly != 0;
  819. break;
  820. case FMODE_WRITE:
  821. ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
  822. && state->n_wronly != 0;
  823. break;
  824. case FMODE_READ|FMODE_WRITE:
  825. ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
  826. && state->n_rdwr != 0;
  827. }
  828. out:
  829. return ret;
  830. }
  831. static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
  832. {
  833. if (delegation == NULL)
  834. return 0;
  835. if ((delegation->type & fmode) != fmode)
  836. return 0;
  837. if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
  838. return 0;
  839. nfs_mark_delegation_referenced(delegation);
  840. return 1;
  841. }
  842. static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
  843. {
  844. switch (fmode) {
  845. case FMODE_WRITE:
  846. state->n_wronly++;
  847. break;
  848. case FMODE_READ:
  849. state->n_rdonly++;
  850. break;
  851. case FMODE_READ|FMODE_WRITE:
  852. state->n_rdwr++;
  853. }
  854. nfs4_state_set_mode_locked(state, state->state | fmode);
  855. }
  856. static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
  857. {
  858. if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
  859. nfs4_stateid_copy(&state->stateid, stateid);
  860. nfs4_stateid_copy(&state->open_stateid, stateid);
  861. switch (fmode) {
  862. case FMODE_READ:
  863. set_bit(NFS_O_RDONLY_STATE, &state->flags);
  864. break;
  865. case FMODE_WRITE:
  866. set_bit(NFS_O_WRONLY_STATE, &state->flags);
  867. break;
  868. case FMODE_READ|FMODE_WRITE:
  869. set_bit(NFS_O_RDWR_STATE, &state->flags);
  870. }
  871. }
  872. static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
  873. {
  874. write_seqlock(&state->seqlock);
  875. nfs_set_open_stateid_locked(state, stateid, fmode);
  876. write_sequnlock(&state->seqlock);
  877. }
  878. static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
  879. {
  880. /*
  881. * Protect the call to nfs4_state_set_mode_locked and
  882. * serialise the stateid update
  883. */
  884. write_seqlock(&state->seqlock);
  885. if (deleg_stateid != NULL) {
  886. nfs4_stateid_copy(&state->stateid, deleg_stateid);
  887. set_bit(NFS_DELEGATED_STATE, &state->flags);
  888. }
  889. if (open_stateid != NULL)
  890. nfs_set_open_stateid_locked(state, open_stateid, fmode);
  891. write_sequnlock(&state->seqlock);
  892. spin_lock(&state->owner->so_lock);
  893. update_open_stateflags(state, fmode);
  894. spin_unlock(&state->owner->so_lock);
  895. }
  896. static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
  897. {
  898. struct nfs_inode *nfsi = NFS_I(state->inode);
  899. struct nfs_delegation *deleg_cur;
  900. int ret = 0;
  901. fmode &= (FMODE_READ|FMODE_WRITE);
  902. rcu_read_lock();
  903. deleg_cur = rcu_dereference(nfsi->delegation);
  904. if (deleg_cur == NULL)
  905. goto no_delegation;
  906. spin_lock(&deleg_cur->lock);
  907. if (nfsi->delegation != deleg_cur ||
  908. (deleg_cur->type & fmode) != fmode)
  909. goto no_delegation_unlock;
  910. if (delegation == NULL)
  911. delegation = &deleg_cur->stateid;
  912. else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
  913. goto no_delegation_unlock;
  914. nfs_mark_delegation_referenced(deleg_cur);
  915. __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
  916. ret = 1;
  917. no_delegation_unlock:
  918. spin_unlock(&deleg_cur->lock);
  919. no_delegation:
  920. rcu_read_unlock();
  921. if (!ret && open_stateid != NULL) {
  922. __update_open_stateid(state, open_stateid, NULL, fmode);
  923. ret = 1;
  924. }
  925. return ret;
  926. }
  927. static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
  928. {
  929. struct nfs_delegation *delegation;
  930. rcu_read_lock();
  931. delegation = rcu_dereference(NFS_I(inode)->delegation);
  932. if (delegation == NULL || (delegation->type & fmode) == fmode) {
  933. rcu_read_unlock();
  934. return;
  935. }
  936. rcu_read_unlock();
  937. nfs_inode_return_delegation(inode);
  938. }
  939. static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
  940. {
  941. struct nfs4_state *state = opendata->state;
  942. struct nfs_inode *nfsi = NFS_I(state->inode);
  943. struct nfs_delegation *delegation;
  944. int open_mode = opendata->o_arg.open_flags;
  945. fmode_t fmode = opendata->o_arg.fmode;
  946. nfs4_stateid stateid;
  947. int ret = -EAGAIN;
  948. for (;;) {
  949. if (can_open_cached(state, fmode, open_mode)) {
  950. spin_lock(&state->owner->so_lock);
  951. if (can_open_cached(state, fmode, open_mode)) {
  952. update_open_stateflags(state, fmode);
  953. spin_unlock(&state->owner->so_lock);
  954. goto out_return_state;
  955. }
  956. spin_unlock(&state->owner->so_lock);
  957. }
  958. rcu_read_lock();
  959. delegation = rcu_dereference(nfsi->delegation);
  960. if (!can_open_delegated(delegation, fmode)) {
  961. rcu_read_unlock();
  962. break;
  963. }
  964. /* Save the delegation */
  965. nfs4_stateid_copy(&stateid, &delegation->stateid);
  966. rcu_read_unlock();
  967. ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
  968. if (ret != 0)
  969. goto out;
  970. ret = -EAGAIN;
  971. /* Try to update the stateid using the delegation */
  972. if (update_open_stateid(state, NULL, &stateid, fmode))
  973. goto out_return_state;
  974. }
  975. out:
  976. return ERR_PTR(ret);
  977. out_return_state:
  978. atomic_inc(&state->count);
  979. return state;
  980. }
  981. static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
  982. {
  983. struct inode *inode;
  984. struct nfs4_state *state = NULL;
  985. struct nfs_delegation *delegation;
  986. int ret;
  987. if (!data->rpc_done) {
  988. state = nfs4_try_open_cached(data);
  989. goto out;
  990. }
  991. ret = -EAGAIN;
  992. if (!(data->f_attr.valid & NFS_ATTR_FATTR))
  993. goto err;
  994. inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
  995. ret = PTR_ERR(inode);
  996. if (IS_ERR(inode))
  997. goto err;
  998. ret = -ENOMEM;
  999. state = nfs4_get_open_state(inode, data->owner);
  1000. if (state == NULL)
  1001. goto err_put_inode;
  1002. if (data->o_res.delegation_type != 0) {
  1003. struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
  1004. int delegation_flags = 0;
  1005. rcu_read_lock();
  1006. delegation = rcu_dereference(NFS_I(inode)->delegation);
  1007. if (delegation)
  1008. delegation_flags = delegation->flags;
  1009. rcu_read_unlock();
  1010. if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
  1011. pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
  1012. "returning a delegation for "
  1013. "OPEN(CLAIM_DELEGATE_CUR)\n",
  1014. clp->cl_hostname);
  1015. } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
  1016. nfs_inode_set_delegation(state->inode,
  1017. data->owner->so_cred,
  1018. &data->o_res);
  1019. else
  1020. nfs_inode_reclaim_delegation(state->inode,
  1021. data->owner->so_cred,
  1022. &data->o_res);
  1023. }
  1024. update_open_stateid(state, &data->o_res.stateid, NULL,
  1025. data->o_arg.fmode);
  1026. iput(inode);
  1027. out:
  1028. return state;
  1029. err_put_inode:
  1030. iput(inode);
  1031. err:
  1032. return ERR_PTR(ret);
  1033. }
  1034. static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
  1035. {
  1036. struct nfs_inode *nfsi = NFS_I(state->inode);
  1037. struct nfs_open_context *ctx;
  1038. spin_lock(&state->inode->i_lock);
  1039. list_for_each_entry(ctx, &nfsi->open_files, list) {
  1040. if (ctx->state != state)
  1041. continue;
  1042. get_nfs_open_context(ctx);
  1043. spin_unlock(&state->inode->i_lock);
  1044. return ctx;
  1045. }
  1046. spin_unlock(&state->inode->i_lock);
  1047. return ERR_PTR(-ENOENT);
  1048. }
  1049. static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
  1050. {
  1051. struct nfs4_opendata *opendata;
  1052. opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
  1053. if (opendata == NULL)
  1054. return ERR_PTR(-ENOMEM);
  1055. opendata->state = state;
  1056. atomic_inc(&state->count);
  1057. return opendata;
  1058. }
  1059. static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
  1060. {
  1061. struct nfs4_state *newstate;
  1062. int ret;
  1063. opendata->o_arg.open_flags = 0;
  1064. opendata->o_arg.fmode = fmode;
  1065. memset(&opendata->o_res, 0, sizeof(opendata->o_res));
  1066. memset(&opendata->c_res, 0, sizeof(opendata->c_res));
  1067. nfs4_init_opendata_res(opendata);
  1068. ret = _nfs4_recover_proc_open(opendata);
  1069. if (ret != 0)
  1070. return ret;
  1071. newstate = nfs4_opendata_to_nfs4_state(opendata);
  1072. if (IS_ERR(newstate))
  1073. return PTR_ERR(newstate);
  1074. nfs4_close_state(newstate, fmode);
  1075. *res = newstate;
  1076. return 0;
  1077. }
  1078. static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
  1079. {
  1080. struct nfs4_state *newstate;
  1081. int ret;
  1082. /* memory barrier prior to reading state->n_* */
  1083. clear_bit(NFS_DELEGATED_STATE, &state->flags);
  1084. smp_rmb();
  1085. if (state->n_rdwr != 0) {
  1086. clear_bit(NFS_O_RDWR_STATE, &state->flags);
  1087. ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
  1088. if (ret != 0)
  1089. return ret;
  1090. if (newstate != state)
  1091. return -ESTALE;
  1092. }
  1093. if (state->n_wronly != 0) {
  1094. clear_bit(NFS_O_WRONLY_STATE, &state->flags);
  1095. ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
  1096. if (ret != 0)
  1097. return ret;
  1098. if (newstate != state)
  1099. return -ESTALE;
  1100. }
  1101. if (state->n_rdonly != 0) {
  1102. clear_bit(NFS_O_RDONLY_STATE, &state->flags);
  1103. ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
  1104. if (ret != 0)
  1105. return ret;
  1106. if (newstate != state)
  1107. return -ESTALE;
  1108. }
  1109. /*
  1110. * We may have performed cached opens for all three recoveries.
  1111. * Check if we need to update the current stateid.
  1112. */
  1113. if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
  1114. !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
  1115. write_seqlock(&state->seqlock);
  1116. if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
  1117. nfs4_stateid_copy(&state->stateid, &state->open_stateid);
  1118. write_sequnlock(&state->seqlock);
  1119. }
  1120. return 0;
  1121. }
  1122. /*
  1123. * OPEN_RECLAIM:
  1124. * reclaim state on the server after a reboot.
  1125. */
  1126. static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
  1127. {
  1128. struct nfs_delegation *delegation;
  1129. struct nfs4_opendata *opendata;
  1130. fmode_t delegation_type = 0;
  1131. int status;
  1132. opendata = nfs4_open_recoverdata_alloc(ctx, state);
  1133. if (IS_ERR(opendata))
  1134. return PTR_ERR(opendata);
  1135. opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
  1136. opendata->o_arg.fh = NFS_FH(state->inode);
  1137. rcu_read_lock();
  1138. delegation = rcu_dereference(NFS_I(state->inode)->delegation);
  1139. if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
  1140. delegation_type = delegation->type;
  1141. rcu_read_unlock();
  1142. opendata->o_arg.u.delegation_type = delegation_type;
  1143. status = nfs4_open_recover(opendata, state);
  1144. nfs4_opendata_put(opendata);
  1145. return status;
  1146. }
  1147. static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
  1148. {
  1149. struct nfs_server *server = NFS_SERVER(state->inode);
  1150. struct nfs4_exception exception = { };
  1151. int err;
  1152. do {
  1153. err = _nfs4_do_open_reclaim(ctx, state);
  1154. if (err != -NFS4ERR_DELAY)
  1155. break;
  1156. nfs4_handle_exception(server, err, &exception);
  1157. } while (exception.retry);
  1158. return err;
  1159. }
  1160. static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
  1161. {
  1162. struct nfs_open_context *ctx;
  1163. int ret;
  1164. ctx = nfs4_state_find_open_context(state);
  1165. if (IS_ERR(ctx))
  1166. return PTR_ERR(ctx);
  1167. ret = nfs4_do_open_reclaim(ctx, state);
  1168. put_nfs_open_context(ctx);
  1169. return ret;
  1170. }
  1171. static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
  1172. {
  1173. struct nfs4_opendata *opendata;
  1174. int ret;
  1175. opendata = nfs4_open_recoverdata_alloc(ctx, state);
  1176. if (IS_ERR(opendata))
  1177. return PTR_ERR(opendata);
  1178. opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
  1179. nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
  1180. ret = nfs4_open_recover(opendata, state);
  1181. nfs4_opendata_put(opendata);
  1182. return ret;
  1183. }
  1184. int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
  1185. {
  1186. struct nfs4_exception exception = { };
  1187. struct nfs_server *server = NFS_SERVER(state->inode);
  1188. int err;
  1189. do {
  1190. err = _nfs4_open_delegation_recall(ctx, state, stateid);
  1191. switch (err) {
  1192. case 0:
  1193. case -ENOENT:
  1194. case -ESTALE:
  1195. goto out;
  1196. case -NFS4ERR_BADSESSION:
  1197. case -NFS4ERR_BADSLOT:
  1198. case -NFS4ERR_BAD_HIGH_SLOT:
  1199. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  1200. case -NFS4ERR_DEADSESSION:
  1201. nfs4_schedule_session_recovery(server->nfs_client->cl_session);
  1202. goto out;
  1203. case -NFS4ERR_STALE_CLIENTID:
  1204. case -NFS4ERR_STALE_STATEID:
  1205. case -NFS4ERR_EXPIRED:
  1206. /* Don't recall a delegation if it was lost */
  1207. nfs4_schedule_lease_recovery(server->nfs_client);
  1208. goto out;
  1209. case -ERESTARTSYS:
  1210. /*
  1211. * The show must go on: exit, but mark the
  1212. * stateid as needing recovery.
  1213. */
  1214. case -NFS4ERR_DELEG_REVOKED:
  1215. case -NFS4ERR_ADMIN_REVOKED:
  1216. case -NFS4ERR_BAD_STATEID:
  1217. nfs_inode_find_state_and_recover(state->inode,
  1218. stateid);
  1219. nfs4_schedule_stateid_recovery(server, state);
  1220. case -ENOMEM:
  1221. err = 0;
  1222. goto out;
  1223. case -NFS4ERR_DELAY:
  1224. case -NFS4ERR_GRACE:
  1225. set_bit(NFS_DELEGATED_STATE, &state->flags);
  1226. ssleep(1);
  1227. err = -EAGAIN;
  1228. goto out;
  1229. }
  1230. err = nfs4_handle_exception(server, err, &exception);
  1231. } while (exception.retry);
  1232. out:
  1233. return err;
  1234. }
  1235. static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
  1236. {
  1237. struct nfs4_opendata *data = calldata;
  1238. data->rpc_status = task->tk_status;
  1239. if (data->rpc_status == 0) {
  1240. nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
  1241. nfs_confirm_seqid(&data->owner->so_seqid, 0);
  1242. renew_lease(data->o_res.server, data->timestamp);
  1243. data->rpc_done = 1;
  1244. }
  1245. }
  1246. static void nfs4_open_confirm_release(void *calldata)
  1247. {
  1248. struct nfs4_opendata *data = calldata;
  1249. struct nfs4_state *state = NULL;
  1250. /* If this request hasn't been cancelled, do nothing */
  1251. if (data->cancelled == 0)
  1252. goto out_free;
  1253. /* In case of error, no cleanup! */
  1254. if (!data->rpc_done)
  1255. goto out_free;
  1256. state = nfs4_opendata_to_nfs4_state(data);
  1257. if (!IS_ERR(state))
  1258. nfs4_close_state(state, data->o_arg.fmode);
  1259. out_free:
  1260. nfs4_opendata_put(data);
  1261. }
  1262. static const struct rpc_call_ops nfs4_open_confirm_ops = {
  1263. .rpc_call_done = nfs4_open_confirm_done,
  1264. .rpc_release = nfs4_open_confirm_release,
  1265. };
  1266. /*
  1267. * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
  1268. */
  1269. static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
  1270. {
  1271. struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
  1272. struct rpc_task *task;
  1273. struct rpc_message msg = {
  1274. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
  1275. .rpc_argp = &data->c_arg,
  1276. .rpc_resp = &data->c_res,
  1277. .rpc_cred = data->owner->so_cred,
  1278. };
  1279. struct rpc_task_setup task_setup_data = {
  1280. .rpc_client = server->client,
  1281. .rpc_message = &msg,
  1282. .callback_ops = &nfs4_open_confirm_ops,
  1283. .callback_data = data,
  1284. .workqueue = nfsiod_workqueue,
  1285. .flags = RPC_TASK_ASYNC,
  1286. };
  1287. int status;
  1288. kref_get(&data->kref);
  1289. data->rpc_done = 0;
  1290. data->rpc_status = 0;
  1291. data->timestamp = jiffies;
  1292. task = rpc_run_task(&task_setup_data);
  1293. if (IS_ERR(task))
  1294. return PTR_ERR(task);
  1295. status = nfs4_wait_for_completion_rpc_task(task);
  1296. if (status != 0) {
  1297. data->cancelled = 1;
  1298. smp_wmb();
  1299. } else
  1300. status = data->rpc_status;
  1301. rpc_put_task(task);
  1302. return status;
  1303. }
  1304. static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
  1305. {
  1306. struct nfs4_opendata *data = calldata;
  1307. struct nfs4_state_owner *sp = data->owner;
  1308. if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
  1309. return;
  1310. /*
  1311. * Check if we still need to send an OPEN call, or if we can use
  1312. * a delegation instead.
  1313. */
  1314. if (data->state != NULL) {
  1315. struct nfs_delegation *delegation;
  1316. if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
  1317. goto out_no_action;
  1318. rcu_read_lock();
  1319. delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
  1320. if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
  1321. can_open_delegated(delegation, data->o_arg.fmode))
  1322. goto unlock_no_action;
  1323. rcu_read_unlock();
  1324. }
  1325. /* Update client id. */
  1326. data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
  1327. if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
  1328. task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
  1329. nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
  1330. }
  1331. data->timestamp = jiffies;
  1332. if (nfs4_setup_sequence(data->o_arg.server,
  1333. &data->o_arg.seq_args,
  1334. &data->o_res.seq_res,
  1335. task) != 0)
  1336. nfs_release_seqid(data->o_arg.seqid);
  1337. else
  1338. rpc_call_start(task);
  1339. return;
  1340. unlock_no_action:
  1341. rcu_read_unlock();
  1342. out_no_action:
  1343. task->tk_action = NULL;
  1344. }
  1345. static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
  1346. {
  1347. rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
  1348. nfs4_open_prepare(task, calldata);
  1349. }
  1350. static void nfs4_open_done(struct rpc_task *task, void *calldata)
  1351. {
  1352. struct nfs4_opendata *data = calldata;
  1353. data->rpc_status = task->tk_status;
  1354. if (!nfs4_sequence_done(task, &data->o_res.seq_res))
  1355. return;
  1356. if (task->tk_status == 0) {
  1357. switch (data->o_res.f_attr->mode & S_IFMT) {
  1358. case S_IFREG:
  1359. break;
  1360. case S_IFLNK:
  1361. data->rpc_status = -ELOOP;
  1362. break;
  1363. case S_IFDIR:
  1364. data->rpc_status = -EISDIR;
  1365. break;
  1366. default:
  1367. data->rpc_status = -ENOTDIR;
  1368. }
  1369. renew_lease(data->o_res.server, data->timestamp);
  1370. if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
  1371. nfs_confirm_seqid(&data->owner->so_seqid, 0);
  1372. }
  1373. data->rpc_done = 1;
  1374. }
  1375. static void nfs4_open_release(void *calldata)
  1376. {
  1377. struct nfs4_opendata *data = calldata;
  1378. struct nfs4_state *state = NULL;
  1379. /* If this request hasn't been cancelled, do nothing */
  1380. if (data->cancelled == 0)
  1381. goto out_free;
  1382. /* In case of error, no cleanup! */
  1383. if (data->rpc_status != 0 || !data->rpc_done)
  1384. goto out_free;
  1385. /* In case we need an open_confirm, no cleanup! */
  1386. if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
  1387. goto out_free;
  1388. state = nfs4_opendata_to_nfs4_state(data);
  1389. if (!IS_ERR(state))
  1390. nfs4_close_state(state, data->o_arg.fmode);
  1391. out_free:
  1392. nfs4_opendata_put(data);
  1393. }
  1394. static const struct rpc_call_ops nfs4_open_ops = {
  1395. .rpc_call_prepare = nfs4_open_prepare,
  1396. .rpc_call_done = nfs4_open_done,
  1397. .rpc_release = nfs4_open_release,
  1398. };
  1399. static const struct rpc_call_ops nfs4_recover_open_ops = {
  1400. .rpc_call_prepare = nfs4_recover_open_prepare,
  1401. .rpc_call_done = nfs4_open_done,
  1402. .rpc_release = nfs4_open_release,
  1403. };
  1404. static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
  1405. {
  1406. struct inode *dir = data->dir->d_inode;
  1407. struct nfs_server *server = NFS_SERVER(dir);
  1408. struct nfs_openargs *o_arg = &data->o_arg;
  1409. struct nfs_openres *o_res = &data->o_res;
  1410. struct rpc_task *task;
  1411. struct rpc_message msg = {
  1412. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
  1413. .rpc_argp = o_arg,
  1414. .rpc_resp = o_res,
  1415. .rpc_cred = data->owner->so_cred,
  1416. };
  1417. struct rpc_task_setup task_setup_data = {
  1418. .rpc_client = server->client,
  1419. .rpc_message = &msg,
  1420. .callback_ops = &nfs4_open_ops,
  1421. .callback_data = data,
  1422. .workqueue = nfsiod_workqueue,
  1423. .flags = RPC_TASK_ASYNC,
  1424. };
  1425. int status;
  1426. nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
  1427. kref_get(&data->kref);
  1428. data->rpc_done = 0;
  1429. data->rpc_status = 0;
  1430. data->cancelled = 0;
  1431. if (isrecover)
  1432. task_setup_data.callback_ops = &nfs4_recover_open_ops;
  1433. task = rpc_run_task(&task_setup_data);
  1434. if (IS_ERR(task))
  1435. return PTR_ERR(task);
  1436. status = nfs4_wait_for_completion_rpc_task(task);
  1437. if (status != 0) {
  1438. data->cancelled = 1;
  1439. smp_wmb();
  1440. } else
  1441. status = data->rpc_status;
  1442. rpc_put_task(task);
  1443. return status;
  1444. }
  1445. static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
  1446. {
  1447. struct inode *dir = data->dir->d_inode;
  1448. struct nfs_openres *o_res = &data->o_res;
  1449. int status;
  1450. status = nfs4_run_open_task(data, 1);
  1451. if (status != 0 || !data->rpc_done)
  1452. return status;
  1453. nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
  1454. nfs_refresh_inode(dir, o_res->dir_attr);
  1455. if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
  1456. status = _nfs4_proc_open_confirm(data);
  1457. if (status != 0)
  1458. return status;
  1459. }
  1460. return status;
  1461. }
  1462. /*
  1463. * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
  1464. */
  1465. static int _nfs4_proc_open(struct nfs4_opendata *data)
  1466. {
  1467. struct inode *dir = data->dir->d_inode;
  1468. struct nfs_server *server = NFS_SERVER(dir);
  1469. struct nfs_openargs *o_arg = &data->o_arg;
  1470. struct nfs_openres *o_res = &data->o_res;
  1471. int status;
  1472. status = nfs4_run_open_task(data, 0);
  1473. if (!data->rpc_done)
  1474. return status;
  1475. if (status != 0) {
  1476. if (status == -NFS4ERR_BADNAME &&
  1477. !(o_arg->open_flags & O_CREAT))
  1478. return -ENOENT;
  1479. return status;
  1480. }
  1481. nfs_fattr_map_and_free_names(server, &data->f_attr);
  1482. if (o_arg->open_flags & O_CREAT) {
  1483. update_changeattr(dir, &o_res->cinfo);
  1484. nfs_post_op_update_inode(dir, o_res->dir_attr);
  1485. } else
  1486. nfs_refresh_inode(dir, o_res->dir_attr);
  1487. if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
  1488. server->caps &= ~NFS_CAP_POSIX_LOCK;
  1489. if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
  1490. status = _nfs4_proc_open_confirm(data);
  1491. if (status != 0)
  1492. return status;
  1493. }
  1494. if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
  1495. _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
  1496. return 0;
  1497. }
  1498. static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
  1499. {
  1500. unsigned int loop;
  1501. int ret;
  1502. for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
  1503. ret = nfs4_wait_clnt_recover(clp);
  1504. if (ret != 0)
  1505. break;
  1506. if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
  1507. !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
  1508. break;
  1509. nfs4_schedule_state_manager(clp);
  1510. ret = -EIO;
  1511. }
  1512. return ret;
  1513. }
  1514. static int nfs4_recover_expired_lease(struct nfs_server *server)
  1515. {
  1516. return nfs4_client_recover_expired_lease(server->nfs_client);
  1517. }
  1518. /*
  1519. * OPEN_EXPIRED:
  1520. * reclaim state on the server after a network partition.
  1521. * Assumes caller holds the appropriate lock
  1522. */
  1523. static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
  1524. {
  1525. struct nfs4_opendata *opendata;
  1526. int ret;
  1527. opendata = nfs4_open_recoverdata_alloc(ctx, state);
  1528. if (IS_ERR(opendata))
  1529. return PTR_ERR(opendata);
  1530. ret = nfs4_open_recover(opendata, state);
  1531. if (ret == -ESTALE)
  1532. d_drop(ctx->dentry);
  1533. nfs4_opendata_put(opendata);
  1534. return ret;
  1535. }
  1536. static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
  1537. {
  1538. struct nfs_server *server = NFS_SERVER(state->inode);
  1539. struct nfs4_exception exception = { };
  1540. int err;
  1541. do {
  1542. err = _nfs4_open_expired(ctx, state);
  1543. switch (err) {
  1544. default:
  1545. goto out;
  1546. case -NFS4ERR_GRACE:
  1547. case -NFS4ERR_DELAY:
  1548. nfs4_handle_exception(server, err, &exception);
  1549. err = 0;
  1550. }
  1551. } while (exception.retry);
  1552. out:
  1553. return err;
  1554. }
  1555. static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
  1556. {
  1557. struct nfs_open_context *ctx;
  1558. int ret;
  1559. ctx = nfs4_state_find_open_context(state);
  1560. if (IS_ERR(ctx))
  1561. return PTR_ERR(ctx);
  1562. ret = nfs4_do_open_expired(ctx, state);
  1563. put_nfs_open_context(ctx);
  1564. return ret;
  1565. }
  1566. static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state)
  1567. {
  1568. nfs_remove_bad_delegation(state->inode);
  1569. write_seqlock(&state->seqlock);
  1570. nfs4_stateid_copy(&state->stateid, &state->open_stateid);
  1571. write_sequnlock(&state->seqlock);
  1572. clear_bit(NFS_DELEGATED_STATE, &state->flags);
  1573. }
  1574. static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
  1575. {
  1576. if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
  1577. nfs_finish_clear_delegation_stateid(state);
  1578. }
  1579. static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
  1580. {
  1581. /* NFSv4.0 doesn't allow for delegation recovery on open expire */
  1582. nfs40_clear_delegation_stateid(state);
  1583. return nfs4_open_expired(sp, state);
  1584. }
  1585. #if defined(CONFIG_NFS_V4_1)
  1586. static int nfs41_check_expired_stateid(struct nfs4_state *state, nfs4_stateid *stateid, unsigned int flags)
  1587. {
  1588. int status = NFS_OK;
  1589. struct nfs_server *server = NFS_SERVER(state->inode);
  1590. if (state->flags & flags) {
  1591. status = nfs41_test_stateid(server, stateid);
  1592. if (status != NFS_OK) {
  1593. nfs41_free_stateid(server, stateid);
  1594. state->flags &= ~flags;
  1595. }
  1596. }
  1597. return status;
  1598. }
  1599. static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
  1600. {
  1601. int deleg_status, open_status;
  1602. int deleg_flags = 1 << NFS_DELEGATED_STATE;
  1603. int open_flags = (1 << NFS_O_RDONLY_STATE) | (1 << NFS_O_WRONLY_STATE) | (1 << NFS_O_RDWR_STATE);
  1604. deleg_status = nfs41_check_expired_stateid(state, &state->stateid, deleg_flags);
  1605. open_status = nfs41_check_expired_stateid(state, &state->open_stateid, open_flags);
  1606. if ((deleg_status == NFS_OK) && (open_status == NFS_OK))
  1607. return NFS_OK;
  1608. return nfs4_open_expired(sp, state);
  1609. }
  1610. #endif
  1611. /*
  1612. * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
  1613. * fields corresponding to attributes that were used to store the verifier.
  1614. * Make sure we clobber those fields in the later setattr call
  1615. */
  1616. static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
  1617. {
  1618. if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
  1619. !(sattr->ia_valid & ATTR_ATIME_SET))
  1620. sattr->ia_valid |= ATTR_ATIME;
  1621. if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
  1622. !(sattr->ia_valid & ATTR_MTIME_SET))
  1623. sattr->ia_valid |= ATTR_MTIME;
  1624. }
  1625. /*
  1626. * Returns a referenced nfs4_state
  1627. */
  1628. static int _nfs4_do_open(struct inode *dir, struct dentry *dentry, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
  1629. {
  1630. struct nfs4_state_owner *sp;
  1631. struct nfs4_state *state = NULL;
  1632. struct nfs_server *server = NFS_SERVER(dir);
  1633. struct nfs4_opendata *opendata;
  1634. int status;
  1635. /* Protect against reboot recovery conflicts */
  1636. status = -ENOMEM;
  1637. sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
  1638. if (sp == NULL) {
  1639. dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
  1640. goto out_err;
  1641. }
  1642. status = nfs4_recover_expired_lease(server);
  1643. if (status != 0)
  1644. goto err_put_state_owner;
  1645. if (dentry->d_inode != NULL)
  1646. nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
  1647. status = -ENOMEM;
  1648. opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
  1649. if (opendata == NULL)
  1650. goto err_put_state_owner;
  1651. if (dentry->d_inode != NULL)
  1652. opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
  1653. status = _nfs4_proc_open(opendata);
  1654. if (status != 0)
  1655. goto err_opendata_put;
  1656. state = nfs4_opendata_to_nfs4_state(opendata);
  1657. status = PTR_ERR(state);
  1658. if (IS_ERR(state))
  1659. goto err_opendata_put;
  1660. if (server->caps & NFS_CAP_POSIX_LOCK)
  1661. set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
  1662. if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL)) {
  1663. nfs4_exclusive_attrset(opendata, sattr);
  1664. nfs_fattr_init(opendata->o_res.f_attr);
  1665. status = nfs4_do_setattr(state->inode, cred,
  1666. opendata->o_res.f_attr, sattr,
  1667. state);
  1668. if (status == 0)
  1669. nfs_setattr_update_inode(state->inode, sattr);
  1670. nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
  1671. }
  1672. nfs4_opendata_put(opendata);
  1673. nfs4_put_state_owner(sp);
  1674. *res = state;
  1675. return 0;
  1676. err_opendata_put:
  1677. nfs4_opendata_put(opendata);
  1678. err_put_state_owner:
  1679. nfs4_put_state_owner(sp);
  1680. out_err:
  1681. *res = NULL;
  1682. return status;
  1683. }
  1684. static struct nfs4_state *nfs4_do_open(struct inode *dir, struct dentry *dentry, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred)
  1685. {
  1686. struct nfs4_exception exception = { };
  1687. struct nfs4_state *res;
  1688. int status;
  1689. fmode &= FMODE_READ|FMODE_WRITE;
  1690. do {
  1691. status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred, &res);
  1692. if (status == 0)
  1693. break;
  1694. /* NOTE: BAD_SEQID means the server and client disagree about the
  1695. * book-keeping w.r.t. state-changing operations
  1696. * (OPEN/CLOSE/LOCK/LOCKU...)
  1697. * It is actually a sign of a bug on the client or on the server.
  1698. *
  1699. * If we receive a BAD_SEQID error in the particular case of
  1700. * doing an OPEN, we assume that nfs_increment_open_seqid() will
  1701. * have unhashed the old state_owner for us, and that we can
  1702. * therefore safely retry using a new one. We should still warn
  1703. * the user though...
  1704. */
  1705. if (status == -NFS4ERR_BAD_SEQID) {
  1706. pr_warn_ratelimited("NFS: v4 server %s "
  1707. " returned a bad sequence-id error!\n",
  1708. NFS_SERVER(dir)->nfs_client->cl_hostname);
  1709. exception.retry = 1;
  1710. continue;
  1711. }
  1712. /*
  1713. * BAD_STATEID on OPEN means that the server cancelled our
  1714. * state before it received the OPEN_CONFIRM.
  1715. * Recover by retrying the request as per the discussion
  1716. * on Page 181 of RFC3530.
  1717. */
  1718. if (status == -NFS4ERR_BAD_STATEID) {
  1719. exception.retry = 1;
  1720. continue;
  1721. }
  1722. if (status == -EAGAIN) {
  1723. /* We must have found a delegation */
  1724. exception.retry = 1;
  1725. continue;
  1726. }
  1727. res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
  1728. status, &exception));
  1729. } while (exception.retry);
  1730. return res;
  1731. }
  1732. static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
  1733. struct nfs_fattr *fattr, struct iattr *sattr,
  1734. struct nfs4_state *state)
  1735. {
  1736. struct nfs_server *server = NFS_SERVER(inode);
  1737. struct nfs_setattrargs arg = {
  1738. .fh = NFS_FH(inode),
  1739. .iap = sattr,
  1740. .server = server,
  1741. .bitmask = server->attr_bitmask,
  1742. };
  1743. struct nfs_setattrres res = {
  1744. .fattr = fattr,
  1745. .server = server,
  1746. };
  1747. struct rpc_message msg = {
  1748. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
  1749. .rpc_argp = &arg,
  1750. .rpc_resp = &res,
  1751. .rpc_cred = cred,
  1752. };
  1753. unsigned long timestamp = jiffies;
  1754. int status;
  1755. nfs_fattr_init(fattr);
  1756. if (state != NULL) {
  1757. nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
  1758. current->files, current->tgid);
  1759. } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
  1760. FMODE_WRITE)) {
  1761. /* Use that stateid */
  1762. } else
  1763. nfs4_stateid_copy(&arg.stateid, &zero_stateid);
  1764. status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
  1765. if (status == 0 && state != NULL)
  1766. renew_lease(server, timestamp);
  1767. return status;
  1768. }
  1769. static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
  1770. struct nfs_fattr *fattr, struct iattr *sattr,
  1771. struct nfs4_state *state)
  1772. {
  1773. struct nfs_server *server = NFS_SERVER(inode);
  1774. struct nfs4_exception exception = {
  1775. .state = state,
  1776. .inode = inode,
  1777. };
  1778. int err;
  1779. do {
  1780. err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
  1781. switch (err) {
  1782. case -NFS4ERR_OPENMODE:
  1783. if (state && !(state->state & FMODE_WRITE)) {
  1784. err = -EBADF;
  1785. if (sattr->ia_valid & ATTR_OPEN)
  1786. err = -EACCES;
  1787. goto out;
  1788. }
  1789. }
  1790. err = nfs4_handle_exception(server, err, &exception);
  1791. } while (exception.retry);
  1792. out:
  1793. return err;
  1794. }
  1795. struct nfs4_closedata {
  1796. struct inode *inode;
  1797. struct nfs4_state *state;
  1798. struct nfs_closeargs arg;
  1799. struct nfs_closeres res;
  1800. struct nfs_fattr fattr;
  1801. unsigned long timestamp;
  1802. bool roc;
  1803. u32 roc_barrier;
  1804. };
  1805. static void nfs4_free_closedata(void *data)
  1806. {
  1807. struct nfs4_closedata *calldata = data;
  1808. struct nfs4_state_owner *sp = calldata->state->owner;
  1809. struct super_block *sb = calldata->state->inode->i_sb;
  1810. if (calldata->roc)
  1811. pnfs_roc_release(calldata->state->inode);
  1812. nfs4_put_open_state(calldata->state);
  1813. nfs_free_seqid(calldata->arg.seqid);
  1814. nfs4_put_state_owner(sp);
  1815. nfs_sb_deactive(sb);
  1816. kfree(calldata);
  1817. }
  1818. static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
  1819. fmode_t fmode)
  1820. {
  1821. spin_lock(&state->owner->so_lock);
  1822. if (!(fmode & FMODE_READ))
  1823. clear_bit(NFS_O_RDONLY_STATE, &state->flags);
  1824. if (!(fmode & FMODE_WRITE))
  1825. clear_bit(NFS_O_WRONLY_STATE, &state->flags);
  1826. clear_bit(NFS_O_RDWR_STATE, &state->flags);
  1827. spin_unlock(&state->owner->so_lock);
  1828. }
  1829. static void nfs4_close_done(struct rpc_task *task, void *data)
  1830. {
  1831. struct nfs4_closedata *calldata = data;
  1832. struct nfs4_state *state = calldata->state;
  1833. struct nfs_server *server = NFS_SERVER(calldata->inode);
  1834. dprintk("%s: begin!\n", __func__);
  1835. if (!nfs4_sequence_done(task, &calldata->res.seq_res))
  1836. return;
  1837. /* hmm. we are done with the inode, and in the process of freeing
  1838. * the state_owner. we keep this around to process errors
  1839. */
  1840. switch (task->tk_status) {
  1841. case 0:
  1842. if (calldata->roc)
  1843. pnfs_roc_set_barrier(state->inode,
  1844. calldata->roc_barrier);
  1845. nfs_set_open_stateid(state, &calldata->res.stateid, 0);
  1846. renew_lease(server, calldata->timestamp);
  1847. nfs4_close_clear_stateid_flags(state,
  1848. calldata->arg.fmode);
  1849. break;
  1850. case -NFS4ERR_STALE_STATEID:
  1851. case -NFS4ERR_OLD_STATEID:
  1852. case -NFS4ERR_BAD_STATEID:
  1853. case -NFS4ERR_EXPIRED:
  1854. if (calldata->arg.fmode == 0)
  1855. break;
  1856. default:
  1857. if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
  1858. rpc_restart_call_prepare(task);
  1859. }
  1860. nfs_release_seqid(calldata->arg.seqid);
  1861. nfs_refresh_inode(calldata->inode, calldata->res.fattr);
  1862. dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
  1863. }
  1864. static void nfs4_close_prepare(struct rpc_task *task, void *data)
  1865. {
  1866. struct nfs4_closedata *calldata = data;
  1867. struct nfs4_state *state = calldata->state;
  1868. bool is_rdonly, is_wronly, is_rdwr;
  1869. int call_close = 0;
  1870. dprintk("%s: begin!\n", __func__);
  1871. if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
  1872. return;
  1873. task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
  1874. spin_lock(&state->owner->so_lock);
  1875. is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
  1876. is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
  1877. is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
  1878. /* Calculate the change in open mode */
  1879. calldata->arg.fmode = 0;
  1880. if (state->n_rdwr == 0) {
  1881. if (state->n_rdonly == 0)
  1882. call_close |= is_rdonly;
  1883. else if (is_rdonly)
  1884. calldata->arg.fmode |= FMODE_READ;
  1885. if (state->n_wronly == 0)
  1886. call_close |= is_wronly;
  1887. else if (is_wronly)
  1888. calldata->arg.fmode |= FMODE_WRITE;
  1889. } else if (is_rdwr)
  1890. calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
  1891. if (calldata->arg.fmode == 0)
  1892. call_close |= is_rdwr;
  1893. spin_unlock(&state->owner->so_lock);
  1894. if (!call_close) {
  1895. /* Note: exit _without_ calling nfs4_close_done */
  1896. task->tk_action = NULL;
  1897. goto out;
  1898. }
  1899. if (calldata->arg.fmode == 0) {
  1900. task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
  1901. if (calldata->roc &&
  1902. pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
  1903. rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
  1904. task, NULL);
  1905. goto out;
  1906. }
  1907. }
  1908. nfs_fattr_init(calldata->res.fattr);
  1909. calldata->timestamp = jiffies;
  1910. if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
  1911. &calldata->arg.seq_args,
  1912. &calldata->res.seq_res,
  1913. task) != 0)
  1914. nfs_release_seqid(calldata->arg.seqid);
  1915. else
  1916. rpc_call_start(task);
  1917. out:
  1918. dprintk("%s: done!\n", __func__);
  1919. }
  1920. static const struct rpc_call_ops nfs4_close_ops = {
  1921. .rpc_call_prepare = nfs4_close_prepare,
  1922. .rpc_call_done = nfs4_close_done,
  1923. .rpc_release = nfs4_free_closedata,
  1924. };
  1925. /*
  1926. * It is possible for data to be read/written from a mem-mapped file
  1927. * after the sys_close call (which hits the vfs layer as a flush).
  1928. * This means that we can't safely call nfsv4 close on a file until
  1929. * the inode is cleared. This in turn means that we are not good
  1930. * NFSv4 citizens - we do not indicate to the server to update the file's
  1931. * share state even when we are done with one of the three share
  1932. * stateid's in the inode.
  1933. *
  1934. * NOTE: Caller must be holding the sp->so_owner semaphore!
  1935. */
  1936. int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
  1937. {
  1938. struct nfs_server *server = NFS_SERVER(state->inode);
  1939. struct nfs4_closedata *calldata;
  1940. struct nfs4_state_owner *sp = state->owner;
  1941. struct rpc_task *task;
  1942. struct rpc_message msg = {
  1943. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
  1944. .rpc_cred = state->owner->so_cred,
  1945. };
  1946. struct rpc_task_setup task_setup_data = {
  1947. .rpc_client = server->client,
  1948. .rpc_message = &msg,
  1949. .callback_ops = &nfs4_close_ops,
  1950. .workqueue = nfsiod_workqueue,
  1951. .flags = RPC_TASK_ASYNC,
  1952. };
  1953. int status = -ENOMEM;
  1954. calldata = kzalloc(sizeof(*calldata), gfp_mask);
  1955. if (calldata == NULL)
  1956. goto out;
  1957. nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
  1958. calldata->inode = state->inode;
  1959. calldata->state = state;
  1960. calldata->arg.fh = NFS_FH(state->inode);
  1961. calldata->arg.stateid = &state->open_stateid;
  1962. /* Serialization for the sequence id */
  1963. calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
  1964. if (calldata->arg.seqid == NULL)
  1965. goto out_free_calldata;
  1966. calldata->arg.fmode = 0;
  1967. calldata->arg.bitmask = server->cache_consistency_bitmask;
  1968. calldata->res.fattr = &calldata->fattr;
  1969. calldata->res.seqid = calldata->arg.seqid;
  1970. calldata->res.server = server;
  1971. calldata->roc = roc;
  1972. nfs_sb_active(calldata->inode->i_sb);
  1973. msg.rpc_argp = &calldata->arg;
  1974. msg.rpc_resp = &calldata->res;
  1975. task_setup_data.callback_data = calldata;
  1976. task = rpc_run_task(&task_setup_data);
  1977. if (IS_ERR(task))
  1978. return PTR_ERR(task);
  1979. status = 0;
  1980. if (wait)
  1981. status = rpc_wait_for_completion_task(task);
  1982. rpc_put_task(task);
  1983. return status;
  1984. out_free_calldata:
  1985. kfree(calldata);
  1986. out:
  1987. if (roc)
  1988. pnfs_roc_release(state->inode);
  1989. nfs4_put_open_state(state);
  1990. nfs4_put_state_owner(sp);
  1991. return status;
  1992. }
  1993. static struct inode *
  1994. nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
  1995. {
  1996. struct nfs4_state *state;
  1997. /* Protect against concurrent sillydeletes */
  1998. state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr, ctx->cred);
  1999. if (IS_ERR(state))
  2000. return ERR_CAST(state);
  2001. ctx->state = state;
  2002. return igrab(state->inode);
  2003. }
  2004. static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
  2005. {
  2006. if (ctx->state == NULL)
  2007. return;
  2008. if (is_sync)
  2009. nfs4_close_sync(ctx->state, ctx->mode);
  2010. else
  2011. nfs4_close_state(ctx->state, ctx->mode);
  2012. }
  2013. static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
  2014. {
  2015. struct nfs4_server_caps_arg args = {
  2016. .fhandle = fhandle,
  2017. };
  2018. struct nfs4_server_caps_res res = {};
  2019. struct rpc_message msg = {
  2020. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
  2021. .rpc_argp = &args,
  2022. .rpc_resp = &res,
  2023. };
  2024. int status;
  2025. status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  2026. if (status == 0) {
  2027. memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
  2028. server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
  2029. NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
  2030. NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
  2031. NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
  2032. NFS_CAP_CTIME|NFS_CAP_MTIME);
  2033. if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
  2034. server->caps |= NFS_CAP_ACLS;
  2035. if (res.has_links != 0)
  2036. server->caps |= NFS_CAP_HARDLINKS;
  2037. if (res.has_symlinks != 0)
  2038. server->caps |= NFS_CAP_SYMLINKS;
  2039. if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
  2040. server->caps |= NFS_CAP_FILEID;
  2041. if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
  2042. server->caps |= NFS_CAP_MODE;
  2043. if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
  2044. server->caps |= NFS_CAP_NLINK;
  2045. if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
  2046. server->caps |= NFS_CAP_OWNER;
  2047. if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
  2048. server->caps |= NFS_CAP_OWNER_GROUP;
  2049. if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
  2050. server->caps |= NFS_CAP_ATIME;
  2051. if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
  2052. server->caps |= NFS_CAP_CTIME;
  2053. if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
  2054. server->caps |= NFS_CAP_MTIME;
  2055. memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
  2056. server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
  2057. server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
  2058. server->acl_bitmask = res.acl_bitmask;
  2059. server->fh_expire_type = res.fh_expire_type;
  2060. }
  2061. return status;
  2062. }
  2063. int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
  2064. {
  2065. struct nfs4_exception exception = { };
  2066. int err;
  2067. do {
  2068. err = nfs4_handle_exception(server,
  2069. _nfs4_server_capabilities(server, fhandle),
  2070. &exception);
  2071. } while (exception.retry);
  2072. return err;
  2073. }
  2074. static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
  2075. struct nfs_fsinfo *info)
  2076. {
  2077. struct nfs4_lookup_root_arg args = {
  2078. .bitmask = nfs4_fattr_bitmap,
  2079. };
  2080. struct nfs4_lookup_res res = {
  2081. .server = server,
  2082. .fattr = info->fattr,
  2083. .fh = fhandle,
  2084. };
  2085. struct rpc_message msg = {
  2086. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
  2087. .rpc_argp = &args,
  2088. .rpc_resp = &res,
  2089. };
  2090. nfs_fattr_init(info->fattr);
  2091. return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  2092. }
  2093. static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
  2094. struct nfs_fsinfo *info)
  2095. {
  2096. struct nfs4_exception exception = { };
  2097. int err;
  2098. do {
  2099. err = _nfs4_lookup_root(server, fhandle, info);
  2100. switch (err) {
  2101. case 0:
  2102. case -NFS4ERR_WRONGSEC:
  2103. goto out;
  2104. default:
  2105. err = nfs4_handle_exception(server, err, &exception);
  2106. }
  2107. } while (exception.retry);
  2108. out:
  2109. return err;
  2110. }
  2111. static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
  2112. struct nfs_fsinfo *info, rpc_authflavor_t flavor)
  2113. {
  2114. struct rpc_auth *auth;
  2115. int ret;
  2116. auth = rpcauth_create(flavor, server->client);
  2117. if (!auth) {
  2118. ret = -EIO;
  2119. goto out;
  2120. }
  2121. ret = nfs4_lookup_root(server, fhandle, info);
  2122. out:
  2123. return ret;
  2124. }
  2125. static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
  2126. struct nfs_fsinfo *info)
  2127. {
  2128. int i, len, status = 0;
  2129. rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
  2130. len = gss_mech_list_pseudoflavors(&flav_array[0]);
  2131. flav_array[len] = RPC_AUTH_NULL;
  2132. len += 1;
  2133. for (i = 0; i < len; i++) {
  2134. status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
  2135. if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
  2136. continue;
  2137. break;
  2138. }
  2139. /*
  2140. * -EACCESS could mean that the user doesn't have correct permissions
  2141. * to access the mount. It could also mean that we tried to mount
  2142. * with a gss auth flavor, but rpc.gssd isn't running. Either way,
  2143. * existing mount programs don't handle -EACCES very well so it should
  2144. * be mapped to -EPERM instead.
  2145. */
  2146. if (status == -EACCES)
  2147. status = -EPERM;
  2148. return status;
  2149. }
  2150. /*
  2151. * get the file handle for the "/" directory on the server
  2152. */
  2153. static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
  2154. struct nfs_fsinfo *info)
  2155. {
  2156. int minor_version = server->nfs_client->cl_minorversion;
  2157. int status = nfs4_lookup_root(server, fhandle, info);
  2158. if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
  2159. /*
  2160. * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
  2161. * by nfs4_map_errors() as this function exits.
  2162. */
  2163. status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
  2164. if (status == 0)
  2165. status = nfs4_server_capabilities(server, fhandle);
  2166. if (status == 0)
  2167. status = nfs4_do_fsinfo(server, fhandle, info);
  2168. return nfs4_map_errors(status);
  2169. }
  2170. /*
  2171. * Get locations and (maybe) other attributes of a referral.
  2172. * Note that we'll actually follow the referral later when
  2173. * we detect fsid mismatch in inode revalidation
  2174. */
  2175. static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
  2176. const struct qstr *name, struct nfs_fattr *fattr,
  2177. struct nfs_fh *fhandle)
  2178. {
  2179. int status = -ENOMEM;
  2180. struct page *page = NULL;
  2181. struct nfs4_fs_locations *locations = NULL;
  2182. page = alloc_page(GFP_KERNEL);
  2183. if (page == NULL)
  2184. goto out;
  2185. locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
  2186. if (locations == NULL)
  2187. goto out;
  2188. status = nfs4_proc_fs_locations(client, dir, name, locations, page);
  2189. if (status != 0)
  2190. goto out;
  2191. /* Make sure server returned a different fsid for the referral */
  2192. if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
  2193. dprintk("%s: server did not return a different fsid for"
  2194. " a referral at %s\n", __func__, name->name);
  2195. status = -EIO;
  2196. goto out;
  2197. }
  2198. /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
  2199. nfs_fixup_referral_attributes(&locations->fattr);
  2200. /* replace the lookup nfs_fattr with the locations nfs_fattr */
  2201. memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
  2202. memset(fhandle, 0, sizeof(struct nfs_fh));
  2203. out:
  2204. if (page)
  2205. __free_page(page);
  2206. kfree(locations);
  2207. return status;
  2208. }
  2209. static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
  2210. {
  2211. struct nfs4_getattr_arg args = {
  2212. .fh = fhandle,
  2213. .bitmask = server->attr_bitmask,
  2214. };
  2215. struct nfs4_getattr_res res = {
  2216. .fattr = fattr,
  2217. .server = server,
  2218. };
  2219. struct rpc_message msg = {
  2220. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
  2221. .rpc_argp = &args,
  2222. .rpc_resp = &res,
  2223. };
  2224. nfs_fattr_init(fattr);
  2225. return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  2226. }
  2227. static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
  2228. {
  2229. struct nfs4_exception exception = { };
  2230. int err;
  2231. do {
  2232. err = nfs4_handle_exception(server,
  2233. _nfs4_proc_getattr(server, fhandle, fattr),
  2234. &exception);
  2235. } while (exception.retry);
  2236. return err;
  2237. }
  2238. /*
  2239. * The file is not closed if it is opened due to the a request to change
  2240. * the size of the file. The open call will not be needed once the
  2241. * VFS layer lookup-intents are implemented.
  2242. *
  2243. * Close is called when the inode is destroyed.
  2244. * If we haven't opened the file for O_WRONLY, we
  2245. * need to in the size_change case to obtain a stateid.
  2246. *
  2247. * Got race?
  2248. * Because OPEN is always done by name in nfsv4, it is
  2249. * possible that we opened a different file by the same
  2250. * name. We can recognize this race condition, but we
  2251. * can't do anything about it besides returning an error.
  2252. *
  2253. * This will be fixed with VFS changes (lookup-intent).
  2254. */
  2255. static int
  2256. nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
  2257. struct iattr *sattr)
  2258. {
  2259. struct inode *inode = dentry->d_inode;
  2260. struct rpc_cred *cred = NULL;
  2261. struct nfs4_state *state = NULL;
  2262. int status;
  2263. if (pnfs_ld_layoutret_on_setattr(inode))
  2264. pnfs_return_layout(inode);
  2265. nfs_fattr_init(fattr);
  2266. /* Search for an existing open(O_WRITE) file */
  2267. if (sattr->ia_valid & ATTR_FILE) {
  2268. struct nfs_open_context *ctx;
  2269. ctx = nfs_file_open_context(sattr->ia_file);
  2270. if (ctx) {
  2271. cred = ctx->cred;
  2272. state = ctx->state;
  2273. }
  2274. }
  2275. /* Deal with open(O_TRUNC) */
  2276. if (sattr->ia_valid & ATTR_OPEN)
  2277. sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
  2278. status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
  2279. if (status == 0)
  2280. nfs_setattr_update_inode(inode, sattr);
  2281. return status;
  2282. }
  2283. static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
  2284. const struct qstr *name, struct nfs_fh *fhandle,
  2285. struct nfs_fattr *fattr)
  2286. {
  2287. struct nfs_server *server = NFS_SERVER(dir);
  2288. int status;
  2289. struct nfs4_lookup_arg args = {
  2290. .bitmask = server->attr_bitmask,
  2291. .dir_fh = NFS_FH(dir),
  2292. .name = name,
  2293. };
  2294. struct nfs4_lookup_res res = {
  2295. .server = server,
  2296. .fattr = fattr,
  2297. .fh = fhandle,
  2298. };
  2299. struct rpc_message msg = {
  2300. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
  2301. .rpc_argp = &args,
  2302. .rpc_resp = &res,
  2303. };
  2304. nfs_fattr_init(fattr);
  2305. dprintk("NFS call lookup %s\n", name->name);
  2306. status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
  2307. dprintk("NFS reply lookup: %d\n", status);
  2308. return status;
  2309. }
  2310. static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
  2311. {
  2312. fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
  2313. NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
  2314. fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
  2315. fattr->nlink = 2;
  2316. }
  2317. static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
  2318. struct qstr *name, struct nfs_fh *fhandle,
  2319. struct nfs_fattr *fattr)
  2320. {
  2321. struct nfs4_exception exception = { };
  2322. struct rpc_clnt *client = *clnt;
  2323. int err;
  2324. do {
  2325. err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
  2326. switch (err) {
  2327. case -NFS4ERR_BADNAME:
  2328. err = -ENOENT;
  2329. goto out;
  2330. case -NFS4ERR_MOVED:
  2331. err = nfs4_get_referral(client, dir, name, fattr, fhandle);
  2332. goto out;
  2333. case -NFS4ERR_WRONGSEC:
  2334. err = -EPERM;
  2335. if (client != *clnt)
  2336. goto out;
  2337. client = nfs4_create_sec_client(client, dir, name);
  2338. if (IS_ERR(client))
  2339. return PTR_ERR(client);
  2340. exception.retry = 1;
  2341. break;
  2342. default:
  2343. err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
  2344. }
  2345. } while (exception.retry);
  2346. out:
  2347. if (err == 0)
  2348. *clnt = client;
  2349. else if (client != *clnt)
  2350. rpc_shutdown_client(client);
  2351. return err;
  2352. }
  2353. static int nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, struct qstr *name,
  2354. struct nfs_fh *fhandle, struct nfs_fattr *fattr)
  2355. {
  2356. int status;
  2357. struct rpc_clnt *client = NFS_CLIENT(dir);
  2358. status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
  2359. if (client != NFS_CLIENT(dir)) {
  2360. rpc_shutdown_client(client);
  2361. nfs_fixup_secinfo_attributes(fattr);
  2362. }
  2363. return status;
  2364. }
  2365. struct rpc_clnt *
  2366. nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
  2367. struct nfs_fh *fhandle, struct nfs_fattr *fattr)
  2368. {
  2369. int status;
  2370. struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
  2371. status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
  2372. if (status < 0) {
  2373. rpc_shutdown_client(client);
  2374. return ERR_PTR(status);
  2375. }
  2376. return client;
  2377. }
  2378. static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
  2379. {
  2380. struct nfs_server *server = NFS_SERVER(inode);
  2381. struct nfs4_accessargs args = {
  2382. .fh = NFS_FH(inode),
  2383. .bitmask = server->cache_consistency_bitmask,
  2384. };
  2385. struct nfs4_accessres res = {
  2386. .server = server,
  2387. };
  2388. struct rpc_message msg = {
  2389. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
  2390. .rpc_argp = &args,
  2391. .rpc_resp = &res,
  2392. .rpc_cred = entry->cred,
  2393. };
  2394. int mode = entry->mask;
  2395. int status;
  2396. /*
  2397. * Determine which access bits we want to ask for...
  2398. */
  2399. if (mode & MAY_READ)
  2400. args.access |= NFS4_ACCESS_READ;
  2401. if (S_ISDIR(inode->i_mode)) {
  2402. if (mode & MAY_WRITE)
  2403. args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
  2404. if (mode & MAY_EXEC)
  2405. args.access |= NFS4_ACCESS_LOOKUP;
  2406. } else {
  2407. if (mode & MAY_WRITE)
  2408. args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
  2409. if (mode & MAY_EXEC)
  2410. args.access |= NFS4_ACCESS_EXECUTE;
  2411. }
  2412. res.fattr = nfs_alloc_fattr();
  2413. if (res.fattr == NULL)
  2414. return -ENOMEM;
  2415. status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  2416. if (!status) {
  2417. entry->mask = 0;
  2418. if (res.access & NFS4_ACCESS_READ)
  2419. entry->mask |= MAY_READ;
  2420. if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
  2421. entry->mask |= MAY_WRITE;
  2422. if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
  2423. entry->mask |= MAY_EXEC;
  2424. nfs_refresh_inode(inode, res.fattr);
  2425. }
  2426. nfs_free_fattr(res.fattr);
  2427. return status;
  2428. }
  2429. static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
  2430. {
  2431. struct nfs4_exception exception = { };
  2432. int err;
  2433. do {
  2434. err = nfs4_handle_exception(NFS_SERVER(inode),
  2435. _nfs4_proc_access(inode, entry),
  2436. &exception);
  2437. } while (exception.retry);
  2438. return err;
  2439. }
  2440. /*
  2441. * TODO: For the time being, we don't try to get any attributes
  2442. * along with any of the zero-copy operations READ, READDIR,
  2443. * READLINK, WRITE.
  2444. *
  2445. * In the case of the first three, we want to put the GETATTR
  2446. * after the read-type operation -- this is because it is hard
  2447. * to predict the length of a GETATTR response in v4, and thus
  2448. * align the READ data correctly. This means that the GETATTR
  2449. * may end up partially falling into the page cache, and we should
  2450. * shift it into the 'tail' of the xdr_buf before processing.
  2451. * To do this efficiently, we need to know the total length
  2452. * of data received, which doesn't seem to be available outside
  2453. * of the RPC layer.
  2454. *
  2455. * In the case of WRITE, we also want to put the GETATTR after
  2456. * the operation -- in this case because we want to make sure
  2457. * we get the post-operation mtime and size. This means that
  2458. * we can't use xdr_encode_pages() as written: we need a variant
  2459. * of it which would leave room in the 'tail' iovec.
  2460. *
  2461. * Both of these changes to the XDR layer would in fact be quite
  2462. * minor, but I decided to leave them for a subsequent patch.
  2463. */
  2464. static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
  2465. unsigned int pgbase, unsigned int pglen)
  2466. {
  2467. struct nfs4_readlink args = {
  2468. .fh = NFS_FH(inode),
  2469. .pgbase = pgbase,
  2470. .pglen = pglen,
  2471. .pages = &page,
  2472. };
  2473. struct nfs4_readlink_res res;
  2474. struct rpc_message msg = {
  2475. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
  2476. .rpc_argp = &args,
  2477. .rpc_resp = &res,
  2478. };
  2479. return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
  2480. }
  2481. static int nfs4_proc_readlink(struct inode *inode, struct page *page,
  2482. unsigned int pgbase, unsigned int pglen)
  2483. {
  2484. struct nfs4_exception exception = { };
  2485. int err;
  2486. do {
  2487. err = nfs4_handle_exception(NFS_SERVER(inode),
  2488. _nfs4_proc_readlink(inode, page, pgbase, pglen),
  2489. &exception);
  2490. } while (exception.retry);
  2491. return err;
  2492. }
  2493. /*
  2494. * Got race?
  2495. * We will need to arrange for the VFS layer to provide an atomic open.
  2496. * Until then, this create/open method is prone to inefficiency and race
  2497. * conditions due to the lookup, create, and open VFS calls from sys_open()
  2498. * placed on the wire.
  2499. *
  2500. * Given the above sorry state of affairs, I'm simply sending an OPEN.
  2501. * The file will be opened again in the subsequent VFS open call
  2502. * (nfs4_proc_file_open).
  2503. *
  2504. * The open for read will just hang around to be used by any process that
  2505. * opens the file O_RDONLY. This will all be resolved with the VFS changes.
  2506. */
  2507. static int
  2508. nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
  2509. int flags, struct nfs_open_context *ctx)
  2510. {
  2511. struct dentry *de = dentry;
  2512. struct nfs4_state *state;
  2513. struct rpc_cred *cred = NULL;
  2514. fmode_t fmode = 0;
  2515. int status = 0;
  2516. if (ctx != NULL) {
  2517. cred = ctx->cred;
  2518. de = ctx->dentry;
  2519. fmode = ctx->mode;
  2520. }
  2521. sattr->ia_mode &= ~current_umask();
  2522. state = nfs4_do_open(dir, de, fmode, flags, sattr, cred);
  2523. d_drop(dentry);
  2524. if (IS_ERR(state)) {
  2525. status = PTR_ERR(state);
  2526. goto out;
  2527. }
  2528. d_add(dentry, igrab(state->inode));
  2529. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  2530. if (ctx != NULL)
  2531. ctx->state = state;
  2532. else
  2533. nfs4_close_sync(state, fmode);
  2534. out:
  2535. return status;
  2536. }
  2537. static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
  2538. {
  2539. struct nfs_server *server = NFS_SERVER(dir);
  2540. struct nfs_removeargs args = {
  2541. .fh = NFS_FH(dir),
  2542. .name = *name,
  2543. .bitmask = server->attr_bitmask,
  2544. };
  2545. struct nfs_removeres res = {
  2546. .server = server,
  2547. };
  2548. struct rpc_message msg = {
  2549. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
  2550. .rpc_argp = &args,
  2551. .rpc_resp = &res,
  2552. };
  2553. int status = -ENOMEM;
  2554. res.dir_attr = nfs_alloc_fattr();
  2555. if (res.dir_attr == NULL)
  2556. goto out;
  2557. status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
  2558. if (status == 0) {
  2559. update_changeattr(dir, &res.cinfo);
  2560. nfs_post_op_update_inode(dir, res.dir_attr);
  2561. }
  2562. nfs_free_fattr(res.dir_attr);
  2563. out:
  2564. return status;
  2565. }
  2566. static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
  2567. {
  2568. struct nfs4_exception exception = { };
  2569. int err;
  2570. do {
  2571. err = nfs4_handle_exception(NFS_SERVER(dir),
  2572. _nfs4_proc_remove(dir, name),
  2573. &exception);
  2574. } while (exception.retry);
  2575. return err;
  2576. }
  2577. static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
  2578. {
  2579. struct nfs_server *server = NFS_SERVER(dir);
  2580. struct nfs_removeargs *args = msg->rpc_argp;
  2581. struct nfs_removeres *res = msg->rpc_resp;
  2582. args->bitmask = server->cache_consistency_bitmask;
  2583. res->server = server;
  2584. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
  2585. nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
  2586. }
  2587. static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
  2588. {
  2589. if (nfs4_setup_sequence(NFS_SERVER(data->dir),
  2590. &data->args.seq_args,
  2591. &data->res.seq_res,
  2592. task))
  2593. return;
  2594. rpc_call_start(task);
  2595. }
  2596. static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
  2597. {
  2598. struct nfs_removeres *res = task->tk_msg.rpc_resp;
  2599. if (!nfs4_sequence_done(task, &res->seq_res))
  2600. return 0;
  2601. if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
  2602. return 0;
  2603. update_changeattr(dir, &res->cinfo);
  2604. nfs_post_op_update_inode(dir, res->dir_attr);
  2605. return 1;
  2606. }
  2607. static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
  2608. {
  2609. struct nfs_server *server = NFS_SERVER(dir);
  2610. struct nfs_renameargs *arg = msg->rpc_argp;
  2611. struct nfs_renameres *res = msg->rpc_resp;
  2612. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
  2613. arg->bitmask = server->attr_bitmask;
  2614. res->server = server;
  2615. nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
  2616. }
  2617. static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
  2618. {
  2619. if (nfs4_setup_sequence(NFS_SERVER(data->old_dir),
  2620. &data->args.seq_args,
  2621. &data->res.seq_res,
  2622. task))
  2623. return;
  2624. rpc_call_start(task);
  2625. }
  2626. static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
  2627. struct inode *new_dir)
  2628. {
  2629. struct nfs_renameres *res = task->tk_msg.rpc_resp;
  2630. if (!nfs4_sequence_done(task, &res->seq_res))
  2631. return 0;
  2632. if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
  2633. return 0;
  2634. update_changeattr(old_dir, &res->old_cinfo);
  2635. nfs_post_op_update_inode(old_dir, res->old_fattr);
  2636. update_changeattr(new_dir, &res->new_cinfo);
  2637. nfs_post_op_update_inode(new_dir, res->new_fattr);
  2638. return 1;
  2639. }
  2640. static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
  2641. struct inode *new_dir, struct qstr *new_name)
  2642. {
  2643. struct nfs_server *server = NFS_SERVER(old_dir);
  2644. struct nfs_renameargs arg = {
  2645. .old_dir = NFS_FH(old_dir),
  2646. .new_dir = NFS_FH(new_dir),
  2647. .old_name = old_name,
  2648. .new_name = new_name,
  2649. .bitmask = server->attr_bitmask,
  2650. };
  2651. struct nfs_renameres res = {
  2652. .server = server,
  2653. };
  2654. struct rpc_message msg = {
  2655. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
  2656. .rpc_argp = &arg,
  2657. .rpc_resp = &res,
  2658. };
  2659. int status = -ENOMEM;
  2660. res.old_fattr = nfs_alloc_fattr();
  2661. res.new_fattr = nfs_alloc_fattr();
  2662. if (res.old_fattr == NULL || res.new_fattr == NULL)
  2663. goto out;
  2664. status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
  2665. if (!status) {
  2666. update_changeattr(old_dir, &res.old_cinfo);
  2667. nfs_post_op_update_inode(old_dir, res.old_fattr);
  2668. update_changeattr(new_dir, &res.new_cinfo);
  2669. nfs_post_op_update_inode(new_dir, res.new_fattr);
  2670. }
  2671. out:
  2672. nfs_free_fattr(res.new_fattr);
  2673. nfs_free_fattr(res.old_fattr);
  2674. return status;
  2675. }
  2676. static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
  2677. struct inode *new_dir, struct qstr *new_name)
  2678. {
  2679. struct nfs4_exception exception = { };
  2680. int err;
  2681. do {
  2682. err = nfs4_handle_exception(NFS_SERVER(old_dir),
  2683. _nfs4_proc_rename(old_dir, old_name,
  2684. new_dir, new_name),
  2685. &exception);
  2686. } while (exception.retry);
  2687. return err;
  2688. }
  2689. static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
  2690. {
  2691. struct nfs_server *server = NFS_SERVER(inode);
  2692. struct nfs4_link_arg arg = {
  2693. .fh = NFS_FH(inode),
  2694. .dir_fh = NFS_FH(dir),
  2695. .name = name,
  2696. .bitmask = server->attr_bitmask,
  2697. };
  2698. struct nfs4_link_res res = {
  2699. .server = server,
  2700. };
  2701. struct rpc_message msg = {
  2702. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
  2703. .rpc_argp = &arg,
  2704. .rpc_resp = &res,
  2705. };
  2706. int status = -ENOMEM;
  2707. res.fattr = nfs_alloc_fattr();
  2708. res.dir_attr = nfs_alloc_fattr();
  2709. if (res.fattr == NULL || res.dir_attr == NULL)
  2710. goto out;
  2711. status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
  2712. if (!status) {
  2713. update_changeattr(dir, &res.cinfo);
  2714. nfs_post_op_update_inode(dir, res.dir_attr);
  2715. nfs_post_op_update_inode(inode, res.fattr);
  2716. }
  2717. out:
  2718. nfs_free_fattr(res.dir_attr);
  2719. nfs_free_fattr(res.fattr);
  2720. return status;
  2721. }
  2722. static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
  2723. {
  2724. struct nfs4_exception exception = { };
  2725. int err;
  2726. do {
  2727. err = nfs4_handle_exception(NFS_SERVER(inode),
  2728. _nfs4_proc_link(inode, dir, name),
  2729. &exception);
  2730. } while (exception.retry);
  2731. return err;
  2732. }
  2733. struct nfs4_createdata {
  2734. struct rpc_message msg;
  2735. struct nfs4_create_arg arg;
  2736. struct nfs4_create_res res;
  2737. struct nfs_fh fh;
  2738. struct nfs_fattr fattr;
  2739. struct nfs_fattr dir_fattr;
  2740. };
  2741. static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
  2742. struct qstr *name, struct iattr *sattr, u32 ftype)
  2743. {
  2744. struct nfs4_createdata *data;
  2745. data = kzalloc(sizeof(*data), GFP_KERNEL);
  2746. if (data != NULL) {
  2747. struct nfs_server *server = NFS_SERVER(dir);
  2748. data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
  2749. data->msg.rpc_argp = &data->arg;
  2750. data->msg.rpc_resp = &data->res;
  2751. data->arg.dir_fh = NFS_FH(dir);
  2752. data->arg.server = server;
  2753. data->arg.name = name;
  2754. data->arg.attrs = sattr;
  2755. data->arg.ftype = ftype;
  2756. data->arg.bitmask = server->attr_bitmask;
  2757. data->res.server = server;
  2758. data->res.fh = &data->fh;
  2759. data->res.fattr = &data->fattr;
  2760. data->res.dir_fattr = &data->dir_fattr;
  2761. nfs_fattr_init(data->res.fattr);
  2762. nfs_fattr_init(data->res.dir_fattr);
  2763. }
  2764. return data;
  2765. }
  2766. static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
  2767. {
  2768. int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
  2769. &data->arg.seq_args, &data->res.seq_res, 1);
  2770. if (status == 0) {
  2771. update_changeattr(dir, &data->res.dir_cinfo);
  2772. nfs_post_op_update_inode(dir, data->res.dir_fattr);
  2773. status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
  2774. }
  2775. return status;
  2776. }
  2777. static void nfs4_free_createdata(struct nfs4_createdata *data)
  2778. {
  2779. kfree(data);
  2780. }
  2781. static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
  2782. struct page *page, unsigned int len, struct iattr *sattr)
  2783. {
  2784. struct nfs4_createdata *data;
  2785. int status = -ENAMETOOLONG;
  2786. if (len > NFS4_MAXPATHLEN)
  2787. goto out;
  2788. status = -ENOMEM;
  2789. data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
  2790. if (data == NULL)
  2791. goto out;
  2792. data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
  2793. data->arg.u.symlink.pages = &page;
  2794. data->arg.u.symlink.len = len;
  2795. status = nfs4_do_create(dir, dentry, data);
  2796. nfs4_free_createdata(data);
  2797. out:
  2798. return status;
  2799. }
  2800. static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
  2801. struct page *page, unsigned int len, struct iattr *sattr)
  2802. {
  2803. struct nfs4_exception exception = { };
  2804. int err;
  2805. do {
  2806. err = nfs4_handle_exception(NFS_SERVER(dir),
  2807. _nfs4_proc_symlink(dir, dentry, page,
  2808. len, sattr),
  2809. &exception);
  2810. } while (exception.retry);
  2811. return err;
  2812. }
  2813. static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
  2814. struct iattr *sattr)
  2815. {
  2816. struct nfs4_createdata *data;
  2817. int status = -ENOMEM;
  2818. data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
  2819. if (data == NULL)
  2820. goto out;
  2821. status = nfs4_do_create(dir, dentry, data);
  2822. nfs4_free_createdata(data);
  2823. out:
  2824. return status;
  2825. }
  2826. static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
  2827. struct iattr *sattr)
  2828. {
  2829. struct nfs4_exception exception = { };
  2830. int err;
  2831. sattr->ia_mode &= ~current_umask();
  2832. do {
  2833. err = nfs4_handle_exception(NFS_SERVER(dir),
  2834. _nfs4_proc_mkdir(dir, dentry, sattr),
  2835. &exception);
  2836. } while (exception.retry);
  2837. return err;
  2838. }
  2839. static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
  2840. u64 cookie, struct page **pages, unsigned int count, int plus)
  2841. {
  2842. struct inode *dir = dentry->d_inode;
  2843. struct nfs4_readdir_arg args = {
  2844. .fh = NFS_FH(dir),
  2845. .pages = pages,
  2846. .pgbase = 0,
  2847. .count = count,
  2848. .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
  2849. .plus = plus,
  2850. };
  2851. struct nfs4_readdir_res res;
  2852. struct rpc_message msg = {
  2853. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
  2854. .rpc_argp = &args,
  2855. .rpc_resp = &res,
  2856. .rpc_cred = cred,
  2857. };
  2858. int status;
  2859. dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
  2860. dentry->d_parent->d_name.name,
  2861. dentry->d_name.name,
  2862. (unsigned long long)cookie);
  2863. nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
  2864. res.pgbase = args.pgbase;
  2865. status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
  2866. if (status >= 0) {
  2867. memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
  2868. status += args.pgbase;
  2869. }
  2870. nfs_invalidate_atime(dir);
  2871. dprintk("%s: returns %d\n", __func__, status);
  2872. return status;
  2873. }
  2874. static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
  2875. u64 cookie, struct page **pages, unsigned int count, int plus)
  2876. {
  2877. struct nfs4_exception exception = { };
  2878. int err;
  2879. do {
  2880. err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
  2881. _nfs4_proc_readdir(dentry, cred, cookie,
  2882. pages, count, plus),
  2883. &exception);
  2884. } while (exception.retry);
  2885. return err;
  2886. }
  2887. static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
  2888. struct iattr *sattr, dev_t rdev)
  2889. {
  2890. struct nfs4_createdata *data;
  2891. int mode = sattr->ia_mode;
  2892. int status = -ENOMEM;
  2893. BUG_ON(!(sattr->ia_valid & ATTR_MODE));
  2894. BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
  2895. data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
  2896. if (data == NULL)
  2897. goto out;
  2898. if (S_ISFIFO(mode))
  2899. data->arg.ftype = NF4FIFO;
  2900. else if (S_ISBLK(mode)) {
  2901. data->arg.ftype = NF4BLK;
  2902. data->arg.u.device.specdata1 = MAJOR(rdev);
  2903. data->arg.u.device.specdata2 = MINOR(rdev);
  2904. }
  2905. else if (S_ISCHR(mode)) {
  2906. data->arg.ftype = NF4CHR;
  2907. data->arg.u.device.specdata1 = MAJOR(rdev);
  2908. data->arg.u.device.specdata2 = MINOR(rdev);
  2909. }
  2910. status = nfs4_do_create(dir, dentry, data);
  2911. nfs4_free_createdata(data);
  2912. out:
  2913. return status;
  2914. }
  2915. static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
  2916. struct iattr *sattr, dev_t rdev)
  2917. {
  2918. struct nfs4_exception exception = { };
  2919. int err;
  2920. sattr->ia_mode &= ~current_umask();
  2921. do {
  2922. err = nfs4_handle_exception(NFS_SERVER(dir),
  2923. _nfs4_proc_mknod(dir, dentry, sattr, rdev),
  2924. &exception);
  2925. } while (exception.retry);
  2926. return err;
  2927. }
  2928. static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
  2929. struct nfs_fsstat *fsstat)
  2930. {
  2931. struct nfs4_statfs_arg args = {
  2932. .fh = fhandle,
  2933. .bitmask = server->attr_bitmask,
  2934. };
  2935. struct nfs4_statfs_res res = {
  2936. .fsstat = fsstat,
  2937. };
  2938. struct rpc_message msg = {
  2939. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
  2940. .rpc_argp = &args,
  2941. .rpc_resp = &res,
  2942. };
  2943. nfs_fattr_init(fsstat->fattr);
  2944. return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  2945. }
  2946. static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
  2947. {
  2948. struct nfs4_exception exception = { };
  2949. int err;
  2950. do {
  2951. err = nfs4_handle_exception(server,
  2952. _nfs4_proc_statfs(server, fhandle, fsstat),
  2953. &exception);
  2954. } while (exception.retry);
  2955. return err;
  2956. }
  2957. static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
  2958. struct nfs_fsinfo *fsinfo)
  2959. {
  2960. struct nfs4_fsinfo_arg args = {
  2961. .fh = fhandle,
  2962. .bitmask = server->attr_bitmask,
  2963. };
  2964. struct nfs4_fsinfo_res res = {
  2965. .fsinfo = fsinfo,
  2966. };
  2967. struct rpc_message msg = {
  2968. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
  2969. .rpc_argp = &args,
  2970. .rpc_resp = &res,
  2971. };
  2972. return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  2973. }
  2974. static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
  2975. {
  2976. struct nfs4_exception exception = { };
  2977. int err;
  2978. do {
  2979. err = nfs4_handle_exception(server,
  2980. _nfs4_do_fsinfo(server, fhandle, fsinfo),
  2981. &exception);
  2982. } while (exception.retry);
  2983. return err;
  2984. }
  2985. static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
  2986. {
  2987. nfs_fattr_init(fsinfo->fattr);
  2988. return nfs4_do_fsinfo(server, fhandle, fsinfo);
  2989. }
  2990. static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
  2991. struct nfs_pathconf *pathconf)
  2992. {
  2993. struct nfs4_pathconf_arg args = {
  2994. .fh = fhandle,
  2995. .bitmask = server->attr_bitmask,
  2996. };
  2997. struct nfs4_pathconf_res res = {
  2998. .pathconf = pathconf,
  2999. };
  3000. struct rpc_message msg = {
  3001. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
  3002. .rpc_argp = &args,
  3003. .rpc_resp = &res,
  3004. };
  3005. /* None of the pathconf attributes are mandatory to implement */
  3006. if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
  3007. memset(pathconf, 0, sizeof(*pathconf));
  3008. return 0;
  3009. }
  3010. nfs_fattr_init(pathconf->fattr);
  3011. return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  3012. }
  3013. static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
  3014. struct nfs_pathconf *pathconf)
  3015. {
  3016. struct nfs4_exception exception = { };
  3017. int err;
  3018. do {
  3019. err = nfs4_handle_exception(server,
  3020. _nfs4_proc_pathconf(server, fhandle, pathconf),
  3021. &exception);
  3022. } while (exception.retry);
  3023. return err;
  3024. }
  3025. void __nfs4_read_done_cb(struct nfs_read_data *data)
  3026. {
  3027. nfs_invalidate_atime(data->inode);
  3028. }
  3029. static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
  3030. {
  3031. struct nfs_server *server = NFS_SERVER(data->inode);
  3032. if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
  3033. rpc_restart_call_prepare(task);
  3034. return -EAGAIN;
  3035. }
  3036. __nfs4_read_done_cb(data);
  3037. if (task->tk_status > 0)
  3038. renew_lease(server, data->timestamp);
  3039. return 0;
  3040. }
  3041. static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
  3042. {
  3043. dprintk("--> %s\n", __func__);
  3044. if (!nfs4_sequence_done(task, &data->res.seq_res))
  3045. return -EAGAIN;
  3046. return data->read_done_cb ? data->read_done_cb(task, data) :
  3047. nfs4_read_done_cb(task, data);
  3048. }
  3049. static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
  3050. {
  3051. data->timestamp = jiffies;
  3052. data->read_done_cb = nfs4_read_done_cb;
  3053. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
  3054. nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
  3055. }
  3056. static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
  3057. {
  3058. if (nfs4_setup_sequence(NFS_SERVER(data->inode),
  3059. &data->args.seq_args,
  3060. &data->res.seq_res,
  3061. task))
  3062. return;
  3063. rpc_call_start(task);
  3064. }
  3065. /* Reset the the nfs_read_data to send the read to the MDS. */
  3066. void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
  3067. {
  3068. dprintk("%s Reset task for i/o through\n", __func__);
  3069. put_lseg(data->lseg);
  3070. data->lseg = NULL;
  3071. /* offsets will differ in the dense stripe case */
  3072. data->args.offset = data->mds_offset;
  3073. data->ds_clp = NULL;
  3074. data->args.fh = NFS_FH(data->inode);
  3075. data->read_done_cb = nfs4_read_done_cb;
  3076. task->tk_ops = data->mds_ops;
  3077. rpc_task_reset_client(task, NFS_CLIENT(data->inode));
  3078. }
  3079. EXPORT_SYMBOL_GPL(nfs4_reset_read);
  3080. static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
  3081. {
  3082. struct inode *inode = data->inode;
  3083. if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
  3084. rpc_restart_call_prepare(task);
  3085. return -EAGAIN;
  3086. }
  3087. if (task->tk_status >= 0) {
  3088. renew_lease(NFS_SERVER(inode), data->timestamp);
  3089. nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
  3090. }
  3091. return 0;
  3092. }
  3093. static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
  3094. {
  3095. if (!nfs4_sequence_done(task, &data->res.seq_res))
  3096. return -EAGAIN;
  3097. return data->write_done_cb ? data->write_done_cb(task, data) :
  3098. nfs4_write_done_cb(task, data);
  3099. }
  3100. /* Reset the the nfs_write_data to send the write to the MDS. */
  3101. void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
  3102. {
  3103. dprintk("%s Reset task for i/o through\n", __func__);
  3104. put_lseg(data->lseg);
  3105. data->lseg = NULL;
  3106. data->ds_clp = NULL;
  3107. data->write_done_cb = nfs4_write_done_cb;
  3108. data->args.fh = NFS_FH(data->inode);
  3109. data->args.bitmask = data->res.server->cache_consistency_bitmask;
  3110. data->args.offset = data->mds_offset;
  3111. data->res.fattr = &data->fattr;
  3112. task->tk_ops = data->mds_ops;
  3113. rpc_task_reset_client(task, NFS_CLIENT(data->inode));
  3114. }
  3115. EXPORT_SYMBOL_GPL(nfs4_reset_write);
  3116. static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
  3117. {
  3118. struct nfs_server *server = NFS_SERVER(data->inode);
  3119. if (data->lseg) {
  3120. data->args.bitmask = NULL;
  3121. data->res.fattr = NULL;
  3122. } else
  3123. data->args.bitmask = server->cache_consistency_bitmask;
  3124. if (!data->write_done_cb)
  3125. data->write_done_cb = nfs4_write_done_cb;
  3126. data->res.server = server;
  3127. data->timestamp = jiffies;
  3128. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
  3129. nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
  3130. }
  3131. static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
  3132. {
  3133. if (nfs4_setup_sequence(NFS_SERVER(data->inode),
  3134. &data->args.seq_args,
  3135. &data->res.seq_res,
  3136. task))
  3137. return;
  3138. rpc_call_start(task);
  3139. }
  3140. static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_write_data *data)
  3141. {
  3142. struct inode *inode = data->inode;
  3143. if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
  3144. rpc_restart_call_prepare(task);
  3145. return -EAGAIN;
  3146. }
  3147. nfs_refresh_inode(inode, data->res.fattr);
  3148. return 0;
  3149. }
  3150. static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
  3151. {
  3152. if (!nfs4_sequence_done(task, &data->res.seq_res))
  3153. return -EAGAIN;
  3154. return data->write_done_cb(task, data);
  3155. }
  3156. static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
  3157. {
  3158. struct nfs_server *server = NFS_SERVER(data->inode);
  3159. if (data->lseg) {
  3160. data->args.bitmask = NULL;
  3161. data->res.fattr = NULL;
  3162. } else
  3163. data->args.bitmask = server->cache_consistency_bitmask;
  3164. if (!data->write_done_cb)
  3165. data->write_done_cb = nfs4_commit_done_cb;
  3166. data->res.server = server;
  3167. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
  3168. nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
  3169. }
  3170. struct nfs4_renewdata {
  3171. struct nfs_client *client;
  3172. unsigned long timestamp;
  3173. };
  3174. /*
  3175. * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
  3176. * standalone procedure for queueing an asynchronous RENEW.
  3177. */
  3178. static void nfs4_renew_release(void *calldata)
  3179. {
  3180. struct nfs4_renewdata *data = calldata;
  3181. struct nfs_client *clp = data->client;
  3182. if (atomic_read(&clp->cl_count) > 1)
  3183. nfs4_schedule_state_renewal(clp);
  3184. nfs_put_client(clp);
  3185. kfree(data);
  3186. }
  3187. static void nfs4_renew_done(struct rpc_task *task, void *calldata)
  3188. {
  3189. struct nfs4_renewdata *data = calldata;
  3190. struct nfs_client *clp = data->client;
  3191. unsigned long timestamp = data->timestamp;
  3192. if (task->tk_status < 0) {
  3193. /* Unless we're shutting down, schedule state recovery! */
  3194. if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
  3195. return;
  3196. if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
  3197. nfs4_schedule_lease_recovery(clp);
  3198. return;
  3199. }
  3200. nfs4_schedule_path_down_recovery(clp);
  3201. }
  3202. do_renew_lease(clp, timestamp);
  3203. }
  3204. static const struct rpc_call_ops nfs4_renew_ops = {
  3205. .rpc_call_done = nfs4_renew_done,
  3206. .rpc_release = nfs4_renew_release,
  3207. };
  3208. static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
  3209. {
  3210. struct rpc_message msg = {
  3211. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
  3212. .rpc_argp = clp,
  3213. .rpc_cred = cred,
  3214. };
  3215. struct nfs4_renewdata *data;
  3216. if (renew_flags == 0)
  3217. return 0;
  3218. if (!atomic_inc_not_zero(&clp->cl_count))
  3219. return -EIO;
  3220. data = kmalloc(sizeof(*data), GFP_NOFS);
  3221. if (data == NULL)
  3222. return -ENOMEM;
  3223. data->client = clp;
  3224. data->timestamp = jiffies;
  3225. return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
  3226. &nfs4_renew_ops, data);
  3227. }
  3228. static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
  3229. {
  3230. struct rpc_message msg = {
  3231. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
  3232. .rpc_argp = clp,
  3233. .rpc_cred = cred,
  3234. };
  3235. unsigned long now = jiffies;
  3236. int status;
  3237. status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
  3238. if (status < 0)
  3239. return status;
  3240. do_renew_lease(clp, now);
  3241. return 0;
  3242. }
  3243. static inline int nfs4_server_supports_acls(struct nfs_server *server)
  3244. {
  3245. return (server->caps & NFS_CAP_ACLS)
  3246. && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
  3247. && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
  3248. }
  3249. /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
  3250. * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
  3251. * the stack.
  3252. */
  3253. #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
  3254. static int buf_to_pages_noslab(const void *buf, size_t buflen,
  3255. struct page **pages, unsigned int *pgbase)
  3256. {
  3257. struct page *newpage, **spages;
  3258. int rc = 0;
  3259. size_t len;
  3260. spages = pages;
  3261. do {
  3262. len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
  3263. newpage = alloc_page(GFP_KERNEL);
  3264. if (newpage == NULL)
  3265. goto unwind;
  3266. memcpy(page_address(newpage), buf, len);
  3267. buf += len;
  3268. buflen -= len;
  3269. *pages++ = newpage;
  3270. rc++;
  3271. } while (buflen != 0);
  3272. return rc;
  3273. unwind:
  3274. for(; rc > 0; rc--)
  3275. __free_page(spages[rc-1]);
  3276. return -ENOMEM;
  3277. }
  3278. struct nfs4_cached_acl {
  3279. int cached;
  3280. size_t len;
  3281. char data[0];
  3282. };
  3283. static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
  3284. {
  3285. struct nfs_inode *nfsi = NFS_I(inode);
  3286. spin_lock(&inode->i_lock);
  3287. kfree(nfsi->nfs4_acl);
  3288. nfsi->nfs4_acl = acl;
  3289. spin_unlock(&inode->i_lock);
  3290. }
  3291. static void nfs4_zap_acl_attr(struct inode *inode)
  3292. {
  3293. nfs4_set_cached_acl(inode, NULL);
  3294. }
  3295. static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
  3296. {
  3297. struct nfs_inode *nfsi = NFS_I(inode);
  3298. struct nfs4_cached_acl *acl;
  3299. int ret = -ENOENT;
  3300. spin_lock(&inode->i_lock);
  3301. acl = nfsi->nfs4_acl;
  3302. if (acl == NULL)
  3303. goto out;
  3304. if (buf == NULL) /* user is just asking for length */
  3305. goto out_len;
  3306. if (acl->cached == 0)
  3307. goto out;
  3308. ret = -ERANGE; /* see getxattr(2) man page */
  3309. if (acl->len > buflen)
  3310. goto out;
  3311. memcpy(buf, acl->data, acl->len);
  3312. out_len:
  3313. ret = acl->len;
  3314. out:
  3315. spin_unlock(&inode->i_lock);
  3316. return ret;
  3317. }
  3318. static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
  3319. {
  3320. struct nfs4_cached_acl *acl;
  3321. if (pages && acl_len <= PAGE_SIZE) {
  3322. acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
  3323. if (acl == NULL)
  3324. goto out;
  3325. acl->cached = 1;
  3326. _copy_from_pages(acl->data, pages, pgbase, acl_len);
  3327. } else {
  3328. acl = kmalloc(sizeof(*acl), GFP_KERNEL);
  3329. if (acl == NULL)
  3330. goto out;
  3331. acl->cached = 0;
  3332. }
  3333. acl->len = acl_len;
  3334. out:
  3335. nfs4_set_cached_acl(inode, acl);
  3336. }
  3337. /*
  3338. * The getxattr API returns the required buffer length when called with a
  3339. * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
  3340. * the required buf. On a NULL buf, we send a page of data to the server
  3341. * guessing that the ACL request can be serviced by a page. If so, we cache
  3342. * up to the page of ACL data, and the 2nd call to getxattr is serviced by
  3343. * the cache. If not so, we throw away the page, and cache the required
  3344. * length. The next getxattr call will then produce another round trip to
  3345. * the server, this time with the input buf of the required size.
  3346. */
  3347. static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
  3348. {
  3349. struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
  3350. struct nfs_getaclargs args = {
  3351. .fh = NFS_FH(inode),
  3352. .acl_pages = pages,
  3353. .acl_len = buflen,
  3354. };
  3355. struct nfs_getaclres res = {
  3356. .acl_len = buflen,
  3357. };
  3358. struct rpc_message msg = {
  3359. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
  3360. .rpc_argp = &args,
  3361. .rpc_resp = &res,
  3362. };
  3363. int ret = -ENOMEM, npages, i;
  3364. size_t acl_len = 0;
  3365. npages = (buflen + PAGE_SIZE - 1) >> PAGE_SHIFT;
  3366. /* As long as we're doing a round trip to the server anyway,
  3367. * let's be prepared for a page of acl data. */
  3368. if (npages == 0)
  3369. npages = 1;
  3370. /* Add an extra page to handle the bitmap returned */
  3371. npages++;
  3372. for (i = 0; i < npages; i++) {
  3373. pages[i] = alloc_page(GFP_KERNEL);
  3374. if (!pages[i])
  3375. goto out_free;
  3376. }
  3377. /* for decoding across pages */
  3378. res.acl_scratch = alloc_page(GFP_KERNEL);
  3379. if (!res.acl_scratch)
  3380. goto out_free;
  3381. args.acl_len = npages * PAGE_SIZE;
  3382. args.acl_pgbase = 0;
  3383. /* Let decode_getfacl know not to fail if the ACL data is larger than
  3384. * the page we send as a guess */
  3385. if (buf == NULL)
  3386. res.acl_flags |= NFS4_ACL_LEN_REQUEST;
  3387. dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
  3388. __func__, buf, buflen, npages, args.acl_len);
  3389. ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
  3390. &msg, &args.seq_args, &res.seq_res, 0);
  3391. if (ret)
  3392. goto out_free;
  3393. acl_len = res.acl_len - res.acl_data_offset;
  3394. if (acl_len > args.acl_len)
  3395. nfs4_write_cached_acl(inode, NULL, 0, acl_len);
  3396. else
  3397. nfs4_write_cached_acl(inode, pages, res.acl_data_offset,
  3398. acl_len);
  3399. if (buf) {
  3400. ret = -ERANGE;
  3401. if (acl_len > buflen)
  3402. goto out_free;
  3403. _copy_from_pages(buf, pages, res.acl_data_offset,
  3404. acl_len);
  3405. }
  3406. ret = acl_len;
  3407. out_free:
  3408. for (i = 0; i < npages; i++)
  3409. if (pages[i])
  3410. __free_page(pages[i]);
  3411. if (res.acl_scratch)
  3412. __free_page(res.acl_scratch);
  3413. return ret;
  3414. }
  3415. static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
  3416. {
  3417. struct nfs4_exception exception = { };
  3418. ssize_t ret;
  3419. do {
  3420. ret = __nfs4_get_acl_uncached(inode, buf, buflen);
  3421. if (ret >= 0)
  3422. break;
  3423. ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
  3424. } while (exception.retry);
  3425. return ret;
  3426. }
  3427. static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
  3428. {
  3429. struct nfs_server *server = NFS_SERVER(inode);
  3430. int ret;
  3431. if (!nfs4_server_supports_acls(server))
  3432. return -EOPNOTSUPP;
  3433. ret = nfs_revalidate_inode(server, inode);
  3434. if (ret < 0)
  3435. return ret;
  3436. if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
  3437. nfs_zap_acl_cache(inode);
  3438. ret = nfs4_read_cached_acl(inode, buf, buflen);
  3439. if (ret != -ENOENT)
  3440. /* -ENOENT is returned if there is no ACL or if there is an ACL
  3441. * but no cached acl data, just the acl length */
  3442. return ret;
  3443. return nfs4_get_acl_uncached(inode, buf, buflen);
  3444. }
  3445. static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
  3446. {
  3447. struct nfs_server *server = NFS_SERVER(inode);
  3448. struct page *pages[NFS4ACL_MAXPAGES];
  3449. struct nfs_setaclargs arg = {
  3450. .fh = NFS_FH(inode),
  3451. .acl_pages = pages,
  3452. .acl_len = buflen,
  3453. };
  3454. struct nfs_setaclres res;
  3455. struct rpc_message msg = {
  3456. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
  3457. .rpc_argp = &arg,
  3458. .rpc_resp = &res,
  3459. };
  3460. int ret, i;
  3461. if (!nfs4_server_supports_acls(server))
  3462. return -EOPNOTSUPP;
  3463. i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
  3464. if (i < 0)
  3465. return i;
  3466. nfs_inode_return_delegation(inode);
  3467. ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
  3468. /*
  3469. * Free each page after tx, so the only ref left is
  3470. * held by the network stack
  3471. */
  3472. for (; i > 0; i--)
  3473. put_page(pages[i-1]);
  3474. /*
  3475. * Acl update can result in inode attribute update.
  3476. * so mark the attribute cache invalid.
  3477. */
  3478. spin_lock(&inode->i_lock);
  3479. NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
  3480. spin_unlock(&inode->i_lock);
  3481. nfs_access_zap_cache(inode);
  3482. nfs_zap_acl_cache(inode);
  3483. return ret;
  3484. }
  3485. static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
  3486. {
  3487. struct nfs4_exception exception = { };
  3488. int err;
  3489. do {
  3490. err = nfs4_handle_exception(NFS_SERVER(inode),
  3491. __nfs4_proc_set_acl(inode, buf, buflen),
  3492. &exception);
  3493. } while (exception.retry);
  3494. return err;
  3495. }
  3496. static int
  3497. nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
  3498. {
  3499. struct nfs_client *clp = server->nfs_client;
  3500. if (task->tk_status >= 0)
  3501. return 0;
  3502. switch(task->tk_status) {
  3503. case -NFS4ERR_DELEG_REVOKED:
  3504. case -NFS4ERR_ADMIN_REVOKED:
  3505. case -NFS4ERR_BAD_STATEID:
  3506. if (state == NULL)
  3507. break;
  3508. nfs_remove_bad_delegation(state->inode);
  3509. case -NFS4ERR_OPENMODE:
  3510. if (state == NULL)
  3511. break;
  3512. nfs4_schedule_stateid_recovery(server, state);
  3513. goto wait_on_recovery;
  3514. case -NFS4ERR_EXPIRED:
  3515. if (state != NULL)
  3516. nfs4_schedule_stateid_recovery(server, state);
  3517. case -NFS4ERR_STALE_STATEID:
  3518. case -NFS4ERR_STALE_CLIENTID:
  3519. nfs4_schedule_lease_recovery(clp);
  3520. goto wait_on_recovery;
  3521. #if defined(CONFIG_NFS_V4_1)
  3522. case -NFS4ERR_BADSESSION:
  3523. case -NFS4ERR_BADSLOT:
  3524. case -NFS4ERR_BAD_HIGH_SLOT:
  3525. case -NFS4ERR_DEADSESSION:
  3526. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  3527. case -NFS4ERR_SEQ_FALSE_RETRY:
  3528. case -NFS4ERR_SEQ_MISORDERED:
  3529. dprintk("%s ERROR %d, Reset session\n", __func__,
  3530. task->tk_status);
  3531. nfs4_schedule_session_recovery(clp->cl_session);
  3532. goto wait_on_recovery;
  3533. #endif /* CONFIG_NFS_V4_1 */
  3534. case -NFS4ERR_DELAY:
  3535. nfs_inc_server_stats(server, NFSIOS_DELAY);
  3536. case -NFS4ERR_GRACE:
  3537. rpc_delay(task, NFS4_POLL_RETRY_MAX);
  3538. task->tk_status = 0;
  3539. return -EAGAIN;
  3540. case -NFS4ERR_RETRY_UNCACHED_REP:
  3541. case -NFS4ERR_OLD_STATEID:
  3542. task->tk_status = 0;
  3543. return -EAGAIN;
  3544. }
  3545. task->tk_status = nfs4_map_errors(task->tk_status);
  3546. return 0;
  3547. wait_on_recovery:
  3548. rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
  3549. if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
  3550. rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
  3551. task->tk_status = 0;
  3552. return -EAGAIN;
  3553. }
  3554. static void nfs4_construct_boot_verifier(struct nfs_client *clp,
  3555. nfs4_verifier *bootverf)
  3556. {
  3557. __be32 verf[2];
  3558. verf[0] = htonl((u32)clp->cl_boot_time.tv_sec);
  3559. verf[1] = htonl((u32)clp->cl_boot_time.tv_nsec);
  3560. memcpy(bootverf->data, verf, sizeof(bootverf->data));
  3561. }
  3562. int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
  3563. unsigned short port, struct rpc_cred *cred,
  3564. struct nfs4_setclientid_res *res)
  3565. {
  3566. nfs4_verifier sc_verifier;
  3567. struct nfs4_setclientid setclientid = {
  3568. .sc_verifier = &sc_verifier,
  3569. .sc_prog = program,
  3570. .sc_cb_ident = clp->cl_cb_ident,
  3571. };
  3572. struct rpc_message msg = {
  3573. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
  3574. .rpc_argp = &setclientid,
  3575. .rpc_resp = res,
  3576. .rpc_cred = cred,
  3577. };
  3578. int loop = 0;
  3579. int status;
  3580. nfs4_construct_boot_verifier(clp, &sc_verifier);
  3581. for(;;) {
  3582. rcu_read_lock();
  3583. setclientid.sc_name_len = scnprintf(setclientid.sc_name,
  3584. sizeof(setclientid.sc_name), "%s/%s %s %s %u",
  3585. clp->cl_ipaddr,
  3586. rpc_peeraddr2str(clp->cl_rpcclient,
  3587. RPC_DISPLAY_ADDR),
  3588. rpc_peeraddr2str(clp->cl_rpcclient,
  3589. RPC_DISPLAY_PROTO),
  3590. clp->cl_rpcclient->cl_auth->au_ops->au_name,
  3591. clp->cl_id_uniquifier);
  3592. setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
  3593. sizeof(setclientid.sc_netid),
  3594. rpc_peeraddr2str(clp->cl_rpcclient,
  3595. RPC_DISPLAY_NETID));
  3596. setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
  3597. sizeof(setclientid.sc_uaddr), "%s.%u.%u",
  3598. clp->cl_ipaddr, port >> 8, port & 255);
  3599. rcu_read_unlock();
  3600. status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
  3601. if (status != -NFS4ERR_CLID_INUSE)
  3602. break;
  3603. if (loop != 0) {
  3604. ++clp->cl_id_uniquifier;
  3605. break;
  3606. }
  3607. ++loop;
  3608. ssleep(clp->cl_lease_time / HZ + 1);
  3609. }
  3610. return status;
  3611. }
  3612. int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
  3613. struct nfs4_setclientid_res *arg,
  3614. struct rpc_cred *cred)
  3615. {
  3616. struct nfs_fsinfo fsinfo;
  3617. struct rpc_message msg = {
  3618. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
  3619. .rpc_argp = arg,
  3620. .rpc_resp = &fsinfo,
  3621. .rpc_cred = cred,
  3622. };
  3623. unsigned long now;
  3624. int status;
  3625. now = jiffies;
  3626. status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
  3627. if (status == 0) {
  3628. spin_lock(&clp->cl_lock);
  3629. clp->cl_lease_time = fsinfo.lease_time * HZ;
  3630. clp->cl_last_renewal = now;
  3631. spin_unlock(&clp->cl_lock);
  3632. }
  3633. return status;
  3634. }
  3635. struct nfs4_delegreturndata {
  3636. struct nfs4_delegreturnargs args;
  3637. struct nfs4_delegreturnres res;
  3638. struct nfs_fh fh;
  3639. nfs4_stateid stateid;
  3640. unsigned long timestamp;
  3641. struct nfs_fattr fattr;
  3642. int rpc_status;
  3643. };
  3644. static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
  3645. {
  3646. struct nfs4_delegreturndata *data = calldata;
  3647. if (!nfs4_sequence_done(task, &data->res.seq_res))
  3648. return;
  3649. switch (task->tk_status) {
  3650. case 0:
  3651. renew_lease(data->res.server, data->timestamp);
  3652. break;
  3653. case -NFS4ERR_ADMIN_REVOKED:
  3654. case -NFS4ERR_DELEG_REVOKED:
  3655. case -NFS4ERR_BAD_STATEID:
  3656. case -NFS4ERR_OLD_STATEID:
  3657. case -NFS4ERR_STALE_STATEID:
  3658. case -NFS4ERR_EXPIRED:
  3659. task->tk_status = 0;
  3660. break;
  3661. default:
  3662. if (nfs4_async_handle_error(task, data->res.server, NULL) ==
  3663. -EAGAIN) {
  3664. rpc_restart_call_prepare(task);
  3665. return;
  3666. }
  3667. }
  3668. data->rpc_status = task->tk_status;
  3669. }
  3670. static void nfs4_delegreturn_release(void *calldata)
  3671. {
  3672. kfree(calldata);
  3673. }
  3674. #if defined(CONFIG_NFS_V4_1)
  3675. static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
  3676. {
  3677. struct nfs4_delegreturndata *d_data;
  3678. d_data = (struct nfs4_delegreturndata *)data;
  3679. if (nfs4_setup_sequence(d_data->res.server,
  3680. &d_data->args.seq_args,
  3681. &d_data->res.seq_res, task))
  3682. return;
  3683. rpc_call_start(task);
  3684. }
  3685. #endif /* CONFIG_NFS_V4_1 */
  3686. static const struct rpc_call_ops nfs4_delegreturn_ops = {
  3687. #if defined(CONFIG_NFS_V4_1)
  3688. .rpc_call_prepare = nfs4_delegreturn_prepare,
  3689. #endif /* CONFIG_NFS_V4_1 */
  3690. .rpc_call_done = nfs4_delegreturn_done,
  3691. .rpc_release = nfs4_delegreturn_release,
  3692. };
  3693. static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
  3694. {
  3695. struct nfs4_delegreturndata *data;
  3696. struct nfs_server *server = NFS_SERVER(inode);
  3697. struct rpc_task *task;
  3698. struct rpc_message msg = {
  3699. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
  3700. .rpc_cred = cred,
  3701. };
  3702. struct rpc_task_setup task_setup_data = {
  3703. .rpc_client = server->client,
  3704. .rpc_message = &msg,
  3705. .callback_ops = &nfs4_delegreturn_ops,
  3706. .flags = RPC_TASK_ASYNC,
  3707. };
  3708. int status = 0;
  3709. data = kzalloc(sizeof(*data), GFP_NOFS);
  3710. if (data == NULL)
  3711. return -ENOMEM;
  3712. nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
  3713. data->args.fhandle = &data->fh;
  3714. data->args.stateid = &data->stateid;
  3715. data->args.bitmask = server->attr_bitmask;
  3716. nfs_copy_fh(&data->fh, NFS_FH(inode));
  3717. nfs4_stateid_copy(&data->stateid, stateid);
  3718. data->res.fattr = &data->fattr;
  3719. data->res.server = server;
  3720. nfs_fattr_init(data->res.fattr);
  3721. data->timestamp = jiffies;
  3722. data->rpc_status = 0;
  3723. task_setup_data.callback_data = data;
  3724. msg.rpc_argp = &data->args;
  3725. msg.rpc_resp = &data->res;
  3726. task = rpc_run_task(&task_setup_data);
  3727. if (IS_ERR(task))
  3728. return PTR_ERR(task);
  3729. if (!issync)
  3730. goto out;
  3731. status = nfs4_wait_for_completion_rpc_task(task);
  3732. if (status != 0)
  3733. goto out;
  3734. status = data->rpc_status;
  3735. if (status != 0)
  3736. goto out;
  3737. nfs_refresh_inode(inode, &data->fattr);
  3738. out:
  3739. rpc_put_task(task);
  3740. return status;
  3741. }
  3742. int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
  3743. {
  3744. struct nfs_server *server = NFS_SERVER(inode);
  3745. struct nfs4_exception exception = { };
  3746. int err;
  3747. do {
  3748. err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
  3749. switch (err) {
  3750. case -NFS4ERR_STALE_STATEID:
  3751. case -NFS4ERR_EXPIRED:
  3752. case 0:
  3753. return 0;
  3754. }
  3755. err = nfs4_handle_exception(server, err, &exception);
  3756. } while (exception.retry);
  3757. return err;
  3758. }
  3759. #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
  3760. #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
  3761. /*
  3762. * sleep, with exponential backoff, and retry the LOCK operation.
  3763. */
  3764. static unsigned long
  3765. nfs4_set_lock_task_retry(unsigned long timeout)
  3766. {
  3767. freezable_schedule_timeout_killable_unsafe(timeout);
  3768. timeout <<= 1;
  3769. if (timeout > NFS4_LOCK_MAXTIMEOUT)
  3770. return NFS4_LOCK_MAXTIMEOUT;
  3771. return timeout;
  3772. }
  3773. static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  3774. {
  3775. struct inode *inode = state->inode;
  3776. struct nfs_server *server = NFS_SERVER(inode);
  3777. struct nfs_client *clp = server->nfs_client;
  3778. struct nfs_lockt_args arg = {
  3779. .fh = NFS_FH(inode),
  3780. .fl = request,
  3781. };
  3782. struct nfs_lockt_res res = {
  3783. .denied = request,
  3784. };
  3785. struct rpc_message msg = {
  3786. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
  3787. .rpc_argp = &arg,
  3788. .rpc_resp = &res,
  3789. .rpc_cred = state->owner->so_cred,
  3790. };
  3791. struct nfs4_lock_state *lsp;
  3792. int status;
  3793. arg.lock_owner.clientid = clp->cl_clientid;
  3794. status = nfs4_set_lock_state(state, request);
  3795. if (status != 0)
  3796. goto out;
  3797. lsp = request->fl_u.nfs4_fl.owner;
  3798. arg.lock_owner.id = lsp->ls_seqid.owner_id;
  3799. arg.lock_owner.s_dev = server->s_dev;
  3800. status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
  3801. switch (status) {
  3802. case 0:
  3803. request->fl_type = F_UNLCK;
  3804. break;
  3805. case -NFS4ERR_DENIED:
  3806. status = 0;
  3807. }
  3808. request->fl_ops->fl_release_private(request);
  3809. request->fl_ops = NULL;
  3810. out:
  3811. return status;
  3812. }
  3813. static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  3814. {
  3815. struct nfs4_exception exception = { };
  3816. int err;
  3817. do {
  3818. err = nfs4_handle_exception(NFS_SERVER(state->inode),
  3819. _nfs4_proc_getlk(state, cmd, request),
  3820. &exception);
  3821. } while (exception.retry);
  3822. return err;
  3823. }
  3824. static int do_vfs_lock(struct file *file, struct file_lock *fl)
  3825. {
  3826. int res = 0;
  3827. switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
  3828. case FL_POSIX:
  3829. res = posix_lock_file_wait(file, fl);
  3830. break;
  3831. case FL_FLOCK:
  3832. res = flock_lock_file_wait(file, fl);
  3833. break;
  3834. default:
  3835. BUG();
  3836. }
  3837. return res;
  3838. }
  3839. struct nfs4_unlockdata {
  3840. struct nfs_locku_args arg;
  3841. struct nfs_locku_res res;
  3842. struct nfs4_lock_state *lsp;
  3843. struct nfs_open_context *ctx;
  3844. struct file_lock fl;
  3845. const struct nfs_server *server;
  3846. unsigned long timestamp;
  3847. };
  3848. static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
  3849. struct nfs_open_context *ctx,
  3850. struct nfs4_lock_state *lsp,
  3851. struct nfs_seqid *seqid)
  3852. {
  3853. struct nfs4_unlockdata *p;
  3854. struct inode *inode = lsp->ls_state->inode;
  3855. p = kzalloc(sizeof(*p), GFP_NOFS);
  3856. if (p == NULL)
  3857. return NULL;
  3858. p->arg.fh = NFS_FH(inode);
  3859. p->arg.fl = &p->fl;
  3860. p->arg.seqid = seqid;
  3861. p->res.seqid = seqid;
  3862. p->arg.stateid = &lsp->ls_stateid;
  3863. p->lsp = lsp;
  3864. atomic_inc(&lsp->ls_count);
  3865. /* Ensure we don't close file until we're done freeing locks! */
  3866. p->ctx = get_nfs_open_context(ctx);
  3867. memcpy(&p->fl, fl, sizeof(p->fl));
  3868. p->server = NFS_SERVER(inode);
  3869. return p;
  3870. }
  3871. static void nfs4_locku_release_calldata(void *data)
  3872. {
  3873. struct nfs4_unlockdata *calldata = data;
  3874. nfs_free_seqid(calldata->arg.seqid);
  3875. nfs4_put_lock_state(calldata->lsp);
  3876. put_nfs_open_context(calldata->ctx);
  3877. kfree(calldata);
  3878. }
  3879. static void nfs4_locku_done(struct rpc_task *task, void *data)
  3880. {
  3881. struct nfs4_unlockdata *calldata = data;
  3882. if (!nfs4_sequence_done(task, &calldata->res.seq_res))
  3883. return;
  3884. switch (task->tk_status) {
  3885. case 0:
  3886. nfs4_stateid_copy(&calldata->lsp->ls_stateid,
  3887. &calldata->res.stateid);
  3888. renew_lease(calldata->server, calldata->timestamp);
  3889. break;
  3890. case -NFS4ERR_BAD_STATEID:
  3891. case -NFS4ERR_OLD_STATEID:
  3892. case -NFS4ERR_STALE_STATEID:
  3893. case -NFS4ERR_EXPIRED:
  3894. break;
  3895. default:
  3896. if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
  3897. rpc_restart_call_prepare(task);
  3898. }
  3899. nfs_release_seqid(calldata->arg.seqid);
  3900. }
  3901. static void nfs4_locku_prepare(struct rpc_task *task, void *data)
  3902. {
  3903. struct nfs4_unlockdata *calldata = data;
  3904. if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
  3905. return;
  3906. if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
  3907. /* Note: exit _without_ running nfs4_locku_done */
  3908. task->tk_action = NULL;
  3909. return;
  3910. }
  3911. calldata->timestamp = jiffies;
  3912. if (nfs4_setup_sequence(calldata->server,
  3913. &calldata->arg.seq_args,
  3914. &calldata->res.seq_res,
  3915. task) != 0)
  3916. nfs_release_seqid(calldata->arg.seqid);
  3917. else
  3918. rpc_call_start(task);
  3919. }
  3920. static const struct rpc_call_ops nfs4_locku_ops = {
  3921. .rpc_call_prepare = nfs4_locku_prepare,
  3922. .rpc_call_done = nfs4_locku_done,
  3923. .rpc_release = nfs4_locku_release_calldata,
  3924. };
  3925. static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
  3926. struct nfs_open_context *ctx,
  3927. struct nfs4_lock_state *lsp,
  3928. struct nfs_seqid *seqid)
  3929. {
  3930. struct nfs4_unlockdata *data;
  3931. struct rpc_message msg = {
  3932. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
  3933. .rpc_cred = ctx->cred,
  3934. };
  3935. struct rpc_task_setup task_setup_data = {
  3936. .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
  3937. .rpc_message = &msg,
  3938. .callback_ops = &nfs4_locku_ops,
  3939. .workqueue = nfsiod_workqueue,
  3940. .flags = RPC_TASK_ASYNC,
  3941. };
  3942. /* Ensure this is an unlock - when canceling a lock, the
  3943. * canceled lock is passed in, and it won't be an unlock.
  3944. */
  3945. fl->fl_type = F_UNLCK;
  3946. data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
  3947. if (data == NULL) {
  3948. nfs_free_seqid(seqid);
  3949. return ERR_PTR(-ENOMEM);
  3950. }
  3951. nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
  3952. msg.rpc_argp = &data->arg;
  3953. msg.rpc_resp = &data->res;
  3954. task_setup_data.callback_data = data;
  3955. return rpc_run_task(&task_setup_data);
  3956. }
  3957. static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
  3958. {
  3959. struct nfs_inode *nfsi = NFS_I(state->inode);
  3960. struct nfs_seqid *seqid;
  3961. struct nfs4_lock_state *lsp;
  3962. struct rpc_task *task;
  3963. int status = 0;
  3964. unsigned char fl_flags = request->fl_flags;
  3965. status = nfs4_set_lock_state(state, request);
  3966. /* Unlock _before_ we do the RPC call */
  3967. request->fl_flags |= FL_EXISTS;
  3968. down_read(&nfsi->rwsem);
  3969. if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
  3970. up_read(&nfsi->rwsem);
  3971. goto out;
  3972. }
  3973. up_read(&nfsi->rwsem);
  3974. if (status != 0)
  3975. goto out;
  3976. /* Is this a delegated lock? */
  3977. if (test_bit(NFS_DELEGATED_STATE, &state->flags))
  3978. goto out;
  3979. lsp = request->fl_u.nfs4_fl.owner;
  3980. seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
  3981. status = -ENOMEM;
  3982. if (seqid == NULL)
  3983. goto out;
  3984. task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
  3985. status = PTR_ERR(task);
  3986. if (IS_ERR(task))
  3987. goto out;
  3988. status = nfs4_wait_for_completion_rpc_task(task);
  3989. rpc_put_task(task);
  3990. out:
  3991. request->fl_flags = fl_flags;
  3992. return status;
  3993. }
  3994. struct nfs4_lockdata {
  3995. struct nfs_lock_args arg;
  3996. struct nfs_lock_res res;
  3997. struct nfs4_lock_state *lsp;
  3998. struct nfs_open_context *ctx;
  3999. struct file_lock fl;
  4000. unsigned long timestamp;
  4001. int rpc_status;
  4002. int cancelled;
  4003. struct nfs_server *server;
  4004. };
  4005. static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
  4006. struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
  4007. gfp_t gfp_mask)
  4008. {
  4009. struct nfs4_lockdata *p;
  4010. struct inode *inode = lsp->ls_state->inode;
  4011. struct nfs_server *server = NFS_SERVER(inode);
  4012. p = kzalloc(sizeof(*p), gfp_mask);
  4013. if (p == NULL)
  4014. return NULL;
  4015. p->arg.fh = NFS_FH(inode);
  4016. p->arg.fl = &p->fl;
  4017. p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
  4018. if (p->arg.open_seqid == NULL)
  4019. goto out_free;
  4020. p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
  4021. if (p->arg.lock_seqid == NULL)
  4022. goto out_free_seqid;
  4023. p->arg.lock_stateid = &lsp->ls_stateid;
  4024. p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
  4025. p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
  4026. p->arg.lock_owner.s_dev = server->s_dev;
  4027. p->res.lock_seqid = p->arg.lock_seqid;
  4028. p->lsp = lsp;
  4029. p->server = server;
  4030. atomic_inc(&lsp->ls_count);
  4031. p->ctx = get_nfs_open_context(ctx);
  4032. memcpy(&p->fl, fl, sizeof(p->fl));
  4033. return p;
  4034. out_free_seqid:
  4035. nfs_free_seqid(p->arg.open_seqid);
  4036. out_free:
  4037. kfree(p);
  4038. return NULL;
  4039. }
  4040. static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
  4041. {
  4042. struct nfs4_lockdata *data = calldata;
  4043. struct nfs4_state *state = data->lsp->ls_state;
  4044. dprintk("%s: begin!\n", __func__);
  4045. if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
  4046. return;
  4047. /* Do we need to do an open_to_lock_owner? */
  4048. if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
  4049. if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
  4050. goto out_release_lock_seqid;
  4051. data->arg.open_stateid = &state->stateid;
  4052. data->arg.new_lock_owner = 1;
  4053. data->res.open_seqid = data->arg.open_seqid;
  4054. } else
  4055. data->arg.new_lock_owner = 0;
  4056. data->timestamp = jiffies;
  4057. if (nfs4_setup_sequence(data->server,
  4058. &data->arg.seq_args,
  4059. &data->res.seq_res,
  4060. task) == 0) {
  4061. rpc_call_start(task);
  4062. return;
  4063. }
  4064. nfs_release_seqid(data->arg.open_seqid);
  4065. out_release_lock_seqid:
  4066. nfs_release_seqid(data->arg.lock_seqid);
  4067. dprintk("%s: done!, ret = %d\n", __func__, task->tk_status);
  4068. }
  4069. static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
  4070. {
  4071. rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
  4072. nfs4_lock_prepare(task, calldata);
  4073. }
  4074. static void nfs4_lock_done(struct rpc_task *task, void *calldata)
  4075. {
  4076. struct nfs4_lockdata *data = calldata;
  4077. dprintk("%s: begin!\n", __func__);
  4078. if (!nfs4_sequence_done(task, &data->res.seq_res))
  4079. return;
  4080. data->rpc_status = task->tk_status;
  4081. if (data->arg.new_lock_owner != 0) {
  4082. if (data->rpc_status == 0)
  4083. nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
  4084. else
  4085. goto out;
  4086. }
  4087. if (data->rpc_status == 0) {
  4088. nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
  4089. data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
  4090. renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
  4091. }
  4092. out:
  4093. dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
  4094. }
  4095. static void nfs4_lock_release(void *calldata)
  4096. {
  4097. struct nfs4_lockdata *data = calldata;
  4098. dprintk("%s: begin!\n", __func__);
  4099. nfs_free_seqid(data->arg.open_seqid);
  4100. if (data->cancelled != 0) {
  4101. struct rpc_task *task;
  4102. task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
  4103. data->arg.lock_seqid);
  4104. if (!IS_ERR(task))
  4105. rpc_put_task_async(task);
  4106. dprintk("%s: cancelling lock!\n", __func__);
  4107. } else
  4108. nfs_free_seqid(data->arg.lock_seqid);
  4109. nfs4_put_lock_state(data->lsp);
  4110. put_nfs_open_context(data->ctx);
  4111. kfree(data);
  4112. dprintk("%s: done!\n", __func__);
  4113. }
  4114. static const struct rpc_call_ops nfs4_lock_ops = {
  4115. .rpc_call_prepare = nfs4_lock_prepare,
  4116. .rpc_call_done = nfs4_lock_done,
  4117. .rpc_release = nfs4_lock_release,
  4118. };
  4119. static const struct rpc_call_ops nfs4_recover_lock_ops = {
  4120. .rpc_call_prepare = nfs4_recover_lock_prepare,
  4121. .rpc_call_done = nfs4_lock_done,
  4122. .rpc_release = nfs4_lock_release,
  4123. };
  4124. static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
  4125. {
  4126. switch (error) {
  4127. case -NFS4ERR_ADMIN_REVOKED:
  4128. case -NFS4ERR_BAD_STATEID:
  4129. lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
  4130. if (new_lock_owner != 0 ||
  4131. (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
  4132. nfs4_schedule_stateid_recovery(server, lsp->ls_state);
  4133. break;
  4134. case -NFS4ERR_STALE_STATEID:
  4135. lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
  4136. case -NFS4ERR_EXPIRED:
  4137. nfs4_schedule_lease_recovery(server->nfs_client);
  4138. };
  4139. }
  4140. static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
  4141. {
  4142. struct nfs4_lockdata *data;
  4143. struct rpc_task *task;
  4144. struct rpc_message msg = {
  4145. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
  4146. .rpc_cred = state->owner->so_cred,
  4147. };
  4148. struct rpc_task_setup task_setup_data = {
  4149. .rpc_client = NFS_CLIENT(state->inode),
  4150. .rpc_message = &msg,
  4151. .callback_ops = &nfs4_lock_ops,
  4152. .workqueue = nfsiod_workqueue,
  4153. .flags = RPC_TASK_ASYNC,
  4154. };
  4155. int ret;
  4156. dprintk("%s: begin!\n", __func__);
  4157. data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
  4158. fl->fl_u.nfs4_fl.owner,
  4159. recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
  4160. if (data == NULL)
  4161. return -ENOMEM;
  4162. if (IS_SETLKW(cmd))
  4163. data->arg.block = 1;
  4164. if (recovery_type > NFS_LOCK_NEW) {
  4165. if (recovery_type == NFS_LOCK_RECLAIM)
  4166. data->arg.reclaim = NFS_LOCK_RECLAIM;
  4167. task_setup_data.callback_ops = &nfs4_recover_lock_ops;
  4168. }
  4169. nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
  4170. msg.rpc_argp = &data->arg;
  4171. msg.rpc_resp = &data->res;
  4172. task_setup_data.callback_data = data;
  4173. task = rpc_run_task(&task_setup_data);
  4174. if (IS_ERR(task))
  4175. return PTR_ERR(task);
  4176. ret = nfs4_wait_for_completion_rpc_task(task);
  4177. if (ret == 0) {
  4178. ret = data->rpc_status;
  4179. if (ret)
  4180. nfs4_handle_setlk_error(data->server, data->lsp,
  4181. data->arg.new_lock_owner, ret);
  4182. } else
  4183. data->cancelled = 1;
  4184. rpc_put_task(task);
  4185. dprintk("%s: done, ret = %d!\n", __func__, ret);
  4186. return ret;
  4187. }
  4188. static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
  4189. {
  4190. struct nfs_server *server = NFS_SERVER(state->inode);
  4191. struct nfs4_exception exception = {
  4192. .inode = state->inode,
  4193. };
  4194. int err;
  4195. do {
  4196. /* Cache the lock if possible... */
  4197. if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
  4198. return 0;
  4199. err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
  4200. if (err != -NFS4ERR_DELAY)
  4201. break;
  4202. nfs4_handle_exception(server, err, &exception);
  4203. } while (exception.retry);
  4204. return err;
  4205. }
  4206. static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
  4207. {
  4208. struct nfs_server *server = NFS_SERVER(state->inode);
  4209. struct nfs4_exception exception = {
  4210. .inode = state->inode,
  4211. };
  4212. int err;
  4213. err = nfs4_set_lock_state(state, request);
  4214. if (err != 0)
  4215. return err;
  4216. do {
  4217. if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
  4218. return 0;
  4219. err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
  4220. switch (err) {
  4221. default:
  4222. goto out;
  4223. case -NFS4ERR_GRACE:
  4224. case -NFS4ERR_DELAY:
  4225. nfs4_handle_exception(server, err, &exception);
  4226. err = 0;
  4227. }
  4228. } while (exception.retry);
  4229. out:
  4230. return err;
  4231. }
  4232. #if defined(CONFIG_NFS_V4_1)
  4233. static int nfs41_check_expired_locks(struct nfs4_state *state)
  4234. {
  4235. int status, ret = NFS_OK;
  4236. struct nfs4_lock_state *lsp;
  4237. struct nfs_server *server = NFS_SERVER(state->inode);
  4238. list_for_each_entry(lsp, &state->lock_states, ls_locks) {
  4239. if (lsp->ls_flags & NFS_LOCK_INITIALIZED) {
  4240. status = nfs41_test_stateid(server, &lsp->ls_stateid);
  4241. if (status != NFS_OK) {
  4242. nfs41_free_stateid(server, &lsp->ls_stateid);
  4243. lsp->ls_flags &= ~NFS_LOCK_INITIALIZED;
  4244. ret = status;
  4245. }
  4246. }
  4247. };
  4248. return ret;
  4249. }
  4250. static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
  4251. {
  4252. int status = NFS_OK;
  4253. if (test_bit(LK_STATE_IN_USE, &state->flags))
  4254. status = nfs41_check_expired_locks(state);
  4255. if (status == NFS_OK)
  4256. return status;
  4257. return nfs4_lock_expired(state, request);
  4258. }
  4259. #endif
  4260. static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  4261. {
  4262. struct nfs_inode *nfsi = NFS_I(state->inode);
  4263. unsigned char fl_flags = request->fl_flags;
  4264. int status = -ENOLCK;
  4265. if ((fl_flags & FL_POSIX) &&
  4266. !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
  4267. goto out;
  4268. /* Is this a delegated open? */
  4269. status = nfs4_set_lock_state(state, request);
  4270. if (status != 0)
  4271. goto out;
  4272. request->fl_flags |= FL_ACCESS;
  4273. status = do_vfs_lock(request->fl_file, request);
  4274. if (status < 0)
  4275. goto out;
  4276. down_read(&nfsi->rwsem);
  4277. if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
  4278. /* Yes: cache locks! */
  4279. /* ...but avoid races with delegation recall... */
  4280. request->fl_flags = fl_flags & ~FL_SLEEP;
  4281. status = do_vfs_lock(request->fl_file, request);
  4282. goto out_unlock;
  4283. }
  4284. status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
  4285. if (status != 0)
  4286. goto out_unlock;
  4287. /* Note: we always want to sleep here! */
  4288. request->fl_flags = fl_flags | FL_SLEEP;
  4289. if (do_vfs_lock(request->fl_file, request) < 0)
  4290. printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
  4291. "manager!\n", __func__);
  4292. out_unlock:
  4293. up_read(&nfsi->rwsem);
  4294. out:
  4295. request->fl_flags = fl_flags;
  4296. return status;
  4297. }
  4298. static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  4299. {
  4300. struct nfs4_exception exception = {
  4301. .state = state,
  4302. .inode = state->inode,
  4303. };
  4304. int err;
  4305. do {
  4306. err = _nfs4_proc_setlk(state, cmd, request);
  4307. if (err == -NFS4ERR_DENIED)
  4308. err = -EAGAIN;
  4309. err = nfs4_handle_exception(NFS_SERVER(state->inode),
  4310. err, &exception);
  4311. } while (exception.retry);
  4312. return err;
  4313. }
  4314. static int
  4315. nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
  4316. {
  4317. struct nfs_open_context *ctx;
  4318. struct nfs4_state *state;
  4319. unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
  4320. int status;
  4321. /* verify open state */
  4322. ctx = nfs_file_open_context(filp);
  4323. state = ctx->state;
  4324. if (request->fl_start < 0 || request->fl_end < 0)
  4325. return -EINVAL;
  4326. if (IS_GETLK(cmd)) {
  4327. if (state != NULL)
  4328. return nfs4_proc_getlk(state, F_GETLK, request);
  4329. return 0;
  4330. }
  4331. if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
  4332. return -EINVAL;
  4333. if (request->fl_type == F_UNLCK) {
  4334. if (state != NULL)
  4335. return nfs4_proc_unlck(state, cmd, request);
  4336. return 0;
  4337. }
  4338. if (state == NULL)
  4339. return -ENOLCK;
  4340. /*
  4341. * Don't rely on the VFS having checked the file open mode,
  4342. * since it won't do this for flock() locks.
  4343. */
  4344. switch (request->fl_type & (F_RDLCK|F_WRLCK|F_UNLCK)) {
  4345. case F_RDLCK:
  4346. if (!(filp->f_mode & FMODE_READ))
  4347. return -EBADF;
  4348. break;
  4349. case F_WRLCK:
  4350. if (!(filp->f_mode & FMODE_WRITE))
  4351. return -EBADF;
  4352. }
  4353. do {
  4354. status = nfs4_proc_setlk(state, cmd, request);
  4355. if ((status != -EAGAIN) || IS_SETLK(cmd))
  4356. break;
  4357. timeout = nfs4_set_lock_task_retry(timeout);
  4358. status = -ERESTARTSYS;
  4359. if (signalled())
  4360. break;
  4361. } while(status < 0);
  4362. return status;
  4363. }
  4364. int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
  4365. {
  4366. struct nfs_server *server = NFS_SERVER(state->inode);
  4367. struct nfs4_exception exception = { };
  4368. int err;
  4369. err = nfs4_set_lock_state(state, fl);
  4370. if (err != 0)
  4371. goto out;
  4372. do {
  4373. err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
  4374. switch (err) {
  4375. default:
  4376. printk(KERN_ERR "NFS: %s: unhandled error "
  4377. "%d.\n", __func__, err);
  4378. case 0:
  4379. case -ESTALE:
  4380. goto out;
  4381. case -NFS4ERR_EXPIRED:
  4382. nfs4_schedule_stateid_recovery(server, state);
  4383. case -NFS4ERR_STALE_CLIENTID:
  4384. case -NFS4ERR_STALE_STATEID:
  4385. nfs4_schedule_lease_recovery(server->nfs_client);
  4386. goto out;
  4387. case -NFS4ERR_BADSESSION:
  4388. case -NFS4ERR_BADSLOT:
  4389. case -NFS4ERR_BAD_HIGH_SLOT:
  4390. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  4391. case -NFS4ERR_DEADSESSION:
  4392. nfs4_schedule_session_recovery(server->nfs_client->cl_session);
  4393. goto out;
  4394. case -ERESTARTSYS:
  4395. /*
  4396. * The show must go on: exit, but mark the
  4397. * stateid as needing recovery.
  4398. */
  4399. case -NFS4ERR_DELEG_REVOKED:
  4400. case -NFS4ERR_ADMIN_REVOKED:
  4401. case -NFS4ERR_BAD_STATEID:
  4402. case -NFS4ERR_OPENMODE:
  4403. nfs4_schedule_stateid_recovery(server, state);
  4404. err = 0;
  4405. goto out;
  4406. case -ENOMEM:
  4407. case -NFS4ERR_DENIED:
  4408. /* kill_proc(fl->fl_pid, SIGLOST, 1); */
  4409. err = 0;
  4410. goto out;
  4411. case -NFS4ERR_DELAY:
  4412. break;
  4413. }
  4414. err = nfs4_handle_exception(server, err, &exception);
  4415. } while (exception.retry);
  4416. out:
  4417. return err;
  4418. }
  4419. struct nfs_release_lockowner_data {
  4420. struct nfs4_lock_state *lsp;
  4421. struct nfs_server *server;
  4422. struct nfs_release_lockowner_args args;
  4423. };
  4424. static void nfs4_release_lockowner_release(void *calldata)
  4425. {
  4426. struct nfs_release_lockowner_data *data = calldata;
  4427. nfs4_free_lock_state(data->server, data->lsp);
  4428. kfree(calldata);
  4429. }
  4430. static const struct rpc_call_ops nfs4_release_lockowner_ops = {
  4431. .rpc_release = nfs4_release_lockowner_release,
  4432. };
  4433. int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
  4434. {
  4435. struct nfs_server *server = lsp->ls_state->owner->so_server;
  4436. struct nfs_release_lockowner_data *data;
  4437. struct rpc_message msg = {
  4438. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
  4439. };
  4440. if (server->nfs_client->cl_mvops->minor_version != 0)
  4441. return -EINVAL;
  4442. data = kmalloc(sizeof(*data), GFP_NOFS);
  4443. if (!data)
  4444. return -ENOMEM;
  4445. data->lsp = lsp;
  4446. data->server = server;
  4447. data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
  4448. data->args.lock_owner.id = lsp->ls_seqid.owner_id;
  4449. data->args.lock_owner.s_dev = server->s_dev;
  4450. msg.rpc_argp = &data->args;
  4451. rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
  4452. return 0;
  4453. }
  4454. #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
  4455. static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
  4456. const void *buf, size_t buflen,
  4457. int flags, int type)
  4458. {
  4459. if (strcmp(key, "") != 0)
  4460. return -EINVAL;
  4461. return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
  4462. }
  4463. static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
  4464. void *buf, size_t buflen, int type)
  4465. {
  4466. if (strcmp(key, "") != 0)
  4467. return -EINVAL;
  4468. return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
  4469. }
  4470. static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
  4471. size_t list_len, const char *name,
  4472. size_t name_len, int type)
  4473. {
  4474. size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
  4475. if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
  4476. return 0;
  4477. if (list && len <= list_len)
  4478. memcpy(list, XATTR_NAME_NFSV4_ACL, len);
  4479. return len;
  4480. }
  4481. /*
  4482. * nfs_fhget will use either the mounted_on_fileid or the fileid
  4483. */
  4484. static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
  4485. {
  4486. if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
  4487. (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
  4488. (fattr->valid & NFS_ATTR_FATTR_FSID) &&
  4489. (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
  4490. return;
  4491. fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
  4492. NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
  4493. fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
  4494. fattr->nlink = 2;
  4495. }
  4496. static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
  4497. const struct qstr *name,
  4498. struct nfs4_fs_locations *fs_locations,
  4499. struct page *page)
  4500. {
  4501. struct nfs_server *server = NFS_SERVER(dir);
  4502. u32 bitmask[2] = {
  4503. [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
  4504. };
  4505. struct nfs4_fs_locations_arg args = {
  4506. .dir_fh = NFS_FH(dir),
  4507. .name = name,
  4508. .page = page,
  4509. .bitmask = bitmask,
  4510. };
  4511. struct nfs4_fs_locations_res res = {
  4512. .fs_locations = fs_locations,
  4513. };
  4514. struct rpc_message msg = {
  4515. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
  4516. .rpc_argp = &args,
  4517. .rpc_resp = &res,
  4518. };
  4519. int status;
  4520. dprintk("%s: start\n", __func__);
  4521. /* Ask for the fileid of the absent filesystem if mounted_on_fileid
  4522. * is not supported */
  4523. if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
  4524. bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
  4525. else
  4526. bitmask[0] |= FATTR4_WORD0_FILEID;
  4527. nfs_fattr_init(&fs_locations->fattr);
  4528. fs_locations->server = server;
  4529. fs_locations->nlocations = 0;
  4530. status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
  4531. dprintk("%s: returned status = %d\n", __func__, status);
  4532. return status;
  4533. }
  4534. int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
  4535. const struct qstr *name,
  4536. struct nfs4_fs_locations *fs_locations,
  4537. struct page *page)
  4538. {
  4539. struct nfs4_exception exception = { };
  4540. int err;
  4541. do {
  4542. err = nfs4_handle_exception(NFS_SERVER(dir),
  4543. _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
  4544. &exception);
  4545. } while (exception.retry);
  4546. return err;
  4547. }
  4548. static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
  4549. {
  4550. int status;
  4551. struct nfs4_secinfo_arg args = {
  4552. .dir_fh = NFS_FH(dir),
  4553. .name = name,
  4554. };
  4555. struct nfs4_secinfo_res res = {
  4556. .flavors = flavors,
  4557. };
  4558. struct rpc_message msg = {
  4559. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
  4560. .rpc_argp = &args,
  4561. .rpc_resp = &res,
  4562. };
  4563. dprintk("NFS call secinfo %s\n", name->name);
  4564. status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
  4565. dprintk("NFS reply secinfo: %d\n", status);
  4566. return status;
  4567. }
  4568. int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
  4569. struct nfs4_secinfo_flavors *flavors)
  4570. {
  4571. struct nfs4_exception exception = { };
  4572. int err;
  4573. do {
  4574. err = nfs4_handle_exception(NFS_SERVER(dir),
  4575. _nfs4_proc_secinfo(dir, name, flavors),
  4576. &exception);
  4577. } while (exception.retry);
  4578. return err;
  4579. }
  4580. #ifdef CONFIG_NFS_V4_1
  4581. /*
  4582. * Check the exchange flags returned by the server for invalid flags, having
  4583. * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
  4584. * DS flags set.
  4585. */
  4586. static int nfs4_check_cl_exchange_flags(u32 flags)
  4587. {
  4588. if (flags & ~EXCHGID4_FLAG_MASK_R)
  4589. goto out_inval;
  4590. if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
  4591. (flags & EXCHGID4_FLAG_USE_NON_PNFS))
  4592. goto out_inval;
  4593. if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
  4594. goto out_inval;
  4595. return NFS_OK;
  4596. out_inval:
  4597. return -NFS4ERR_INVAL;
  4598. }
  4599. static bool
  4600. nfs41_same_server_scope(struct server_scope *a, struct server_scope *b)
  4601. {
  4602. if (a->server_scope_sz == b->server_scope_sz &&
  4603. memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
  4604. return true;
  4605. return false;
  4606. }
  4607. /*
  4608. * nfs4_proc_exchange_id()
  4609. *
  4610. * Since the clientid has expired, all compounds using sessions
  4611. * associated with the stale clientid will be returning
  4612. * NFS4ERR_BADSESSION in the sequence operation, and will therefore
  4613. * be in some phase of session reset.
  4614. */
  4615. int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
  4616. {
  4617. nfs4_verifier verifier;
  4618. struct nfs41_exchange_id_args args = {
  4619. .verifier = &verifier,
  4620. .client = clp,
  4621. .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
  4622. };
  4623. struct nfs41_exchange_id_res res = {
  4624. .client = clp,
  4625. };
  4626. int status;
  4627. struct rpc_message msg = {
  4628. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
  4629. .rpc_argp = &args,
  4630. .rpc_resp = &res,
  4631. .rpc_cred = cred,
  4632. };
  4633. dprintk("--> %s\n", __func__);
  4634. BUG_ON(clp == NULL);
  4635. nfs4_construct_boot_verifier(clp, &verifier);
  4636. args.id_len = scnprintf(args.id, sizeof(args.id),
  4637. "%s/%s/%u",
  4638. clp->cl_ipaddr,
  4639. clp->cl_rpcclient->cl_nodename,
  4640. clp->cl_rpcclient->cl_auth->au_flavor);
  4641. res.server_scope = kzalloc(sizeof(struct server_scope), GFP_KERNEL);
  4642. if (unlikely(!res.server_scope)) {
  4643. status = -ENOMEM;
  4644. goto out;
  4645. }
  4646. res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_KERNEL);
  4647. if (unlikely(!res.impl_id)) {
  4648. status = -ENOMEM;
  4649. goto out_server_scope;
  4650. }
  4651. status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
  4652. if (!status)
  4653. status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
  4654. if (!status) {
  4655. /* use the most recent implementation id */
  4656. kfree(clp->impl_id);
  4657. clp->impl_id = res.impl_id;
  4658. } else
  4659. kfree(res.impl_id);
  4660. if (!status) {
  4661. if (clp->server_scope &&
  4662. !nfs41_same_server_scope(clp->server_scope,
  4663. res.server_scope)) {
  4664. dprintk("%s: server_scope mismatch detected\n",
  4665. __func__);
  4666. set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
  4667. kfree(clp->server_scope);
  4668. clp->server_scope = NULL;
  4669. }
  4670. if (!clp->server_scope) {
  4671. clp->server_scope = res.server_scope;
  4672. goto out;
  4673. }
  4674. }
  4675. out_server_scope:
  4676. kfree(res.server_scope);
  4677. out:
  4678. if (clp->impl_id)
  4679. dprintk("%s: Server Implementation ID: "
  4680. "domain: %s, name: %s, date: %llu,%u\n",
  4681. __func__, clp->impl_id->domain, clp->impl_id->name,
  4682. clp->impl_id->date.seconds,
  4683. clp->impl_id->date.nseconds);
  4684. dprintk("<-- %s status= %d\n", __func__, status);
  4685. return status;
  4686. }
  4687. struct nfs4_get_lease_time_data {
  4688. struct nfs4_get_lease_time_args *args;
  4689. struct nfs4_get_lease_time_res *res;
  4690. struct nfs_client *clp;
  4691. };
  4692. static void nfs4_get_lease_time_prepare(struct rpc_task *task,
  4693. void *calldata)
  4694. {
  4695. int ret;
  4696. struct nfs4_get_lease_time_data *data =
  4697. (struct nfs4_get_lease_time_data *)calldata;
  4698. dprintk("--> %s\n", __func__);
  4699. rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
  4700. /* just setup sequence, do not trigger session recovery
  4701. since we're invoked within one */
  4702. ret = nfs41_setup_sequence(data->clp->cl_session,
  4703. &data->args->la_seq_args,
  4704. &data->res->lr_seq_res, task);
  4705. BUG_ON(ret == -EAGAIN);
  4706. rpc_call_start(task);
  4707. dprintk("<-- %s\n", __func__);
  4708. }
  4709. /*
  4710. * Called from nfs4_state_manager thread for session setup, so don't recover
  4711. * from sequence operation or clientid errors.
  4712. */
  4713. static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
  4714. {
  4715. struct nfs4_get_lease_time_data *data =
  4716. (struct nfs4_get_lease_time_data *)calldata;
  4717. dprintk("--> %s\n", __func__);
  4718. if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
  4719. return;
  4720. switch (task->tk_status) {
  4721. case -NFS4ERR_DELAY:
  4722. case -NFS4ERR_GRACE:
  4723. dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
  4724. rpc_delay(task, NFS4_POLL_RETRY_MIN);
  4725. task->tk_status = 0;
  4726. /* fall through */
  4727. case -NFS4ERR_RETRY_UNCACHED_REP:
  4728. rpc_restart_call_prepare(task);
  4729. return;
  4730. }
  4731. dprintk("<-- %s\n", __func__);
  4732. }
  4733. static const struct rpc_call_ops nfs4_get_lease_time_ops = {
  4734. .rpc_call_prepare = nfs4_get_lease_time_prepare,
  4735. .rpc_call_done = nfs4_get_lease_time_done,
  4736. };
  4737. int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
  4738. {
  4739. struct rpc_task *task;
  4740. struct nfs4_get_lease_time_args args;
  4741. struct nfs4_get_lease_time_res res = {
  4742. .lr_fsinfo = fsinfo,
  4743. };
  4744. struct nfs4_get_lease_time_data data = {
  4745. .args = &args,
  4746. .res = &res,
  4747. .clp = clp,
  4748. };
  4749. struct rpc_message msg = {
  4750. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
  4751. .rpc_argp = &args,
  4752. .rpc_resp = &res,
  4753. };
  4754. struct rpc_task_setup task_setup = {
  4755. .rpc_client = clp->cl_rpcclient,
  4756. .rpc_message = &msg,
  4757. .callback_ops = &nfs4_get_lease_time_ops,
  4758. .callback_data = &data,
  4759. .flags = RPC_TASK_TIMEOUT,
  4760. };
  4761. int status;
  4762. nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
  4763. dprintk("--> %s\n", __func__);
  4764. task = rpc_run_task(&task_setup);
  4765. if (IS_ERR(task))
  4766. status = PTR_ERR(task);
  4767. else {
  4768. status = task->tk_status;
  4769. rpc_put_task(task);
  4770. }
  4771. dprintk("<-- %s return %d\n", __func__, status);
  4772. return status;
  4773. }
  4774. static struct nfs4_slot *nfs4_alloc_slots(u32 max_slots, gfp_t gfp_flags)
  4775. {
  4776. return kcalloc(max_slots, sizeof(struct nfs4_slot), gfp_flags);
  4777. }
  4778. static void nfs4_add_and_init_slots(struct nfs4_slot_table *tbl,
  4779. struct nfs4_slot *new,
  4780. u32 max_slots,
  4781. u32 ivalue)
  4782. {
  4783. struct nfs4_slot *old = NULL;
  4784. u32 i;
  4785. spin_lock(&tbl->slot_tbl_lock);
  4786. if (new) {
  4787. old = tbl->slots;
  4788. tbl->slots = new;
  4789. tbl->max_slots = max_slots;
  4790. }
  4791. tbl->highest_used_slotid = -1; /* no slot is currently used */
  4792. for (i = 0; i < tbl->max_slots; i++)
  4793. tbl->slots[i].seq_nr = ivalue;
  4794. spin_unlock(&tbl->slot_tbl_lock);
  4795. kfree(old);
  4796. }
  4797. /*
  4798. * (re)Initialise a slot table
  4799. */
  4800. static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
  4801. u32 ivalue)
  4802. {
  4803. struct nfs4_slot *new = NULL;
  4804. int ret = -ENOMEM;
  4805. dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
  4806. max_reqs, tbl->max_slots);
  4807. /* Does the newly negotiated max_reqs match the existing slot table? */
  4808. if (max_reqs != tbl->max_slots) {
  4809. new = nfs4_alloc_slots(max_reqs, GFP_NOFS);
  4810. if (!new)
  4811. goto out;
  4812. }
  4813. ret = 0;
  4814. nfs4_add_and_init_slots(tbl, new, max_reqs, ivalue);
  4815. dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
  4816. tbl, tbl->slots, tbl->max_slots);
  4817. out:
  4818. dprintk("<-- %s: return %d\n", __func__, ret);
  4819. return ret;
  4820. }
  4821. /* Destroy the slot table */
  4822. static void nfs4_destroy_slot_tables(struct nfs4_session *session)
  4823. {
  4824. if (session->fc_slot_table.slots != NULL) {
  4825. kfree(session->fc_slot_table.slots);
  4826. session->fc_slot_table.slots = NULL;
  4827. }
  4828. if (session->bc_slot_table.slots != NULL) {
  4829. kfree(session->bc_slot_table.slots);
  4830. session->bc_slot_table.slots = NULL;
  4831. }
  4832. return;
  4833. }
  4834. /*
  4835. * Initialize or reset the forechannel and backchannel tables
  4836. */
  4837. static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
  4838. {
  4839. struct nfs4_slot_table *tbl;
  4840. int status;
  4841. dprintk("--> %s\n", __func__);
  4842. /* Fore channel */
  4843. tbl = &ses->fc_slot_table;
  4844. status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
  4845. if (status) /* -ENOMEM */
  4846. return status;
  4847. /* Back channel */
  4848. tbl = &ses->bc_slot_table;
  4849. status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
  4850. if (status && tbl->slots == NULL)
  4851. /* Fore and back channel share a connection so get
  4852. * both slot tables or neither */
  4853. nfs4_destroy_slot_tables(ses);
  4854. return status;
  4855. }
  4856. struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
  4857. {
  4858. struct nfs4_session *session;
  4859. struct nfs4_slot_table *tbl;
  4860. session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
  4861. if (!session)
  4862. return NULL;
  4863. tbl = &session->fc_slot_table;
  4864. tbl->highest_used_slotid = NFS4_NO_SLOT;
  4865. spin_lock_init(&tbl->slot_tbl_lock);
  4866. rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
  4867. init_completion(&tbl->complete);
  4868. tbl = &session->bc_slot_table;
  4869. tbl->highest_used_slotid = NFS4_NO_SLOT;
  4870. spin_lock_init(&tbl->slot_tbl_lock);
  4871. rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
  4872. init_completion(&tbl->complete);
  4873. session->session_state = 1<<NFS4_SESSION_INITING;
  4874. session->clp = clp;
  4875. return session;
  4876. }
  4877. void nfs4_destroy_session(struct nfs4_session *session)
  4878. {
  4879. struct rpc_xprt *xprt;
  4880. nfs4_proc_destroy_session(session);
  4881. rcu_read_lock();
  4882. xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
  4883. rcu_read_unlock();
  4884. dprintk("%s Destroy backchannel for xprt %p\n",
  4885. __func__, xprt);
  4886. xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
  4887. nfs4_destroy_slot_tables(session);
  4888. kfree(session);
  4889. }
  4890. /*
  4891. * Initialize the values to be used by the client in CREATE_SESSION
  4892. * If nfs4_init_session set the fore channel request and response sizes,
  4893. * use them.
  4894. *
  4895. * Set the back channel max_resp_sz_cached to zero to force the client to
  4896. * always set csa_cachethis to FALSE because the current implementation
  4897. * of the back channel DRC only supports caching the CB_SEQUENCE operation.
  4898. */
  4899. static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
  4900. {
  4901. struct nfs4_session *session = args->client->cl_session;
  4902. unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
  4903. mxresp_sz = session->fc_attrs.max_resp_sz;
  4904. if (mxrqst_sz == 0)
  4905. mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
  4906. if (mxresp_sz == 0)
  4907. mxresp_sz = NFS_MAX_FILE_IO_SIZE;
  4908. /* Fore channel attributes */
  4909. args->fc_attrs.max_rqst_sz = mxrqst_sz;
  4910. args->fc_attrs.max_resp_sz = mxresp_sz;
  4911. args->fc_attrs.max_ops = NFS4_MAX_OPS;
  4912. args->fc_attrs.max_reqs = max_session_slots;
  4913. dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
  4914. "max_ops=%u max_reqs=%u\n",
  4915. __func__,
  4916. args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
  4917. args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
  4918. /* Back channel attributes */
  4919. args->bc_attrs.max_rqst_sz = PAGE_SIZE;
  4920. args->bc_attrs.max_resp_sz = PAGE_SIZE;
  4921. args->bc_attrs.max_resp_sz_cached = 0;
  4922. args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
  4923. args->bc_attrs.max_reqs = 1;
  4924. dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
  4925. "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
  4926. __func__,
  4927. args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
  4928. args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
  4929. args->bc_attrs.max_reqs);
  4930. }
  4931. static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
  4932. {
  4933. struct nfs4_channel_attrs *sent = &args->fc_attrs;
  4934. struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
  4935. if (rcvd->max_resp_sz > sent->max_resp_sz)
  4936. return -EINVAL;
  4937. /*
  4938. * Our requested max_ops is the minimum we need; we're not
  4939. * prepared to break up compounds into smaller pieces than that.
  4940. * So, no point even trying to continue if the server won't
  4941. * cooperate:
  4942. */
  4943. if (rcvd->max_ops < sent->max_ops)
  4944. return -EINVAL;
  4945. if (rcvd->max_reqs == 0)
  4946. return -EINVAL;
  4947. if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
  4948. rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
  4949. return 0;
  4950. }
  4951. static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
  4952. {
  4953. struct nfs4_channel_attrs *sent = &args->bc_attrs;
  4954. struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
  4955. if (rcvd->max_rqst_sz > sent->max_rqst_sz)
  4956. return -EINVAL;
  4957. if (rcvd->max_resp_sz < sent->max_resp_sz)
  4958. return -EINVAL;
  4959. if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
  4960. return -EINVAL;
  4961. /* These would render the backchannel useless: */
  4962. if (rcvd->max_ops != sent->max_ops)
  4963. return -EINVAL;
  4964. if (rcvd->max_reqs != sent->max_reqs)
  4965. return -EINVAL;
  4966. return 0;
  4967. }
  4968. static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
  4969. struct nfs4_session *session)
  4970. {
  4971. int ret;
  4972. ret = nfs4_verify_fore_channel_attrs(args, session);
  4973. if (ret)
  4974. return ret;
  4975. return nfs4_verify_back_channel_attrs(args, session);
  4976. }
  4977. static int _nfs4_proc_create_session(struct nfs_client *clp)
  4978. {
  4979. struct nfs4_session *session = clp->cl_session;
  4980. struct nfs41_create_session_args args = {
  4981. .client = clp,
  4982. .cb_program = NFS4_CALLBACK,
  4983. };
  4984. struct nfs41_create_session_res res = {
  4985. .client = clp,
  4986. };
  4987. struct rpc_message msg = {
  4988. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
  4989. .rpc_argp = &args,
  4990. .rpc_resp = &res,
  4991. };
  4992. int status;
  4993. nfs4_init_channel_attrs(&args);
  4994. args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
  4995. status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
  4996. if (!status)
  4997. /* Verify the session's negotiated channel_attrs values */
  4998. status = nfs4_verify_channel_attrs(&args, session);
  4999. if (!status) {
  5000. /* Increment the clientid slot sequence id */
  5001. clp->cl_seqid++;
  5002. }
  5003. return status;
  5004. }
  5005. /*
  5006. * Issues a CREATE_SESSION operation to the server.
  5007. * It is the responsibility of the caller to verify the session is
  5008. * expired before calling this routine.
  5009. */
  5010. int nfs4_proc_create_session(struct nfs_client *clp)
  5011. {
  5012. int status;
  5013. unsigned *ptr;
  5014. struct nfs4_session *session = clp->cl_session;
  5015. dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
  5016. status = _nfs4_proc_create_session(clp);
  5017. if (status)
  5018. goto out;
  5019. /* Init or reset the session slot tables */
  5020. status = nfs4_setup_session_slot_tables(session);
  5021. dprintk("slot table setup returned %d\n", status);
  5022. if (status)
  5023. goto out;
  5024. ptr = (unsigned *)&session->sess_id.data[0];
  5025. dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
  5026. clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
  5027. out:
  5028. dprintk("<-- %s\n", __func__);
  5029. return status;
  5030. }
  5031. /*
  5032. * Issue the over-the-wire RPC DESTROY_SESSION.
  5033. * The caller must serialize access to this routine.
  5034. */
  5035. int nfs4_proc_destroy_session(struct nfs4_session *session)
  5036. {
  5037. int status = 0;
  5038. struct rpc_message msg;
  5039. dprintk("--> nfs4_proc_destroy_session\n");
  5040. /* session is still being setup */
  5041. if (session->clp->cl_cons_state != NFS_CS_READY)
  5042. return status;
  5043. msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
  5044. msg.rpc_argp = session;
  5045. msg.rpc_resp = NULL;
  5046. msg.rpc_cred = NULL;
  5047. status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
  5048. if (status)
  5049. printk(KERN_WARNING
  5050. "NFS: Got error %d from the server on DESTROY_SESSION. "
  5051. "Session has been destroyed regardless...\n", status);
  5052. dprintk("<-- nfs4_proc_destroy_session\n");
  5053. return status;
  5054. }
  5055. int nfs4_init_session(struct nfs_server *server)
  5056. {
  5057. struct nfs_client *clp = server->nfs_client;
  5058. struct nfs4_session *session;
  5059. unsigned int rsize, wsize;
  5060. int ret;
  5061. if (!nfs4_has_session(clp))
  5062. return 0;
  5063. session = clp->cl_session;
  5064. if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
  5065. return 0;
  5066. rsize = server->rsize;
  5067. if (rsize == 0)
  5068. rsize = NFS_MAX_FILE_IO_SIZE;
  5069. wsize = server->wsize;
  5070. if (wsize == 0)
  5071. wsize = NFS_MAX_FILE_IO_SIZE;
  5072. session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
  5073. session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
  5074. ret = nfs4_recover_expired_lease(server);
  5075. if (!ret)
  5076. ret = nfs4_check_client_ready(clp);
  5077. return ret;
  5078. }
  5079. int nfs4_init_ds_session(struct nfs_client *clp)
  5080. {
  5081. struct nfs4_session *session = clp->cl_session;
  5082. int ret;
  5083. if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
  5084. return 0;
  5085. ret = nfs4_client_recover_expired_lease(clp);
  5086. if (!ret)
  5087. /* Test for the DS role */
  5088. if (!is_ds_client(clp))
  5089. ret = -ENODEV;
  5090. if (!ret)
  5091. ret = nfs4_check_client_ready(clp);
  5092. return ret;
  5093. }
  5094. EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
  5095. /*
  5096. * Renew the cl_session lease.
  5097. */
  5098. struct nfs4_sequence_data {
  5099. struct nfs_client *clp;
  5100. struct nfs4_sequence_args args;
  5101. struct nfs4_sequence_res res;
  5102. };
  5103. static void nfs41_sequence_release(void *data)
  5104. {
  5105. struct nfs4_sequence_data *calldata = data;
  5106. struct nfs_client *clp = calldata->clp;
  5107. if (atomic_read(&clp->cl_count) > 1)
  5108. nfs4_schedule_state_renewal(clp);
  5109. nfs_put_client(clp);
  5110. kfree(calldata);
  5111. }
  5112. static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
  5113. {
  5114. switch(task->tk_status) {
  5115. case -NFS4ERR_DELAY:
  5116. rpc_delay(task, NFS4_POLL_RETRY_MAX);
  5117. return -EAGAIN;
  5118. default:
  5119. nfs4_schedule_lease_recovery(clp);
  5120. }
  5121. return 0;
  5122. }
  5123. static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
  5124. {
  5125. struct nfs4_sequence_data *calldata = data;
  5126. struct nfs_client *clp = calldata->clp;
  5127. if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
  5128. return;
  5129. if (task->tk_status < 0) {
  5130. dprintk("%s ERROR %d\n", __func__, task->tk_status);
  5131. if (atomic_read(&clp->cl_count) == 1)
  5132. goto out;
  5133. if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
  5134. rpc_restart_call_prepare(task);
  5135. return;
  5136. }
  5137. }
  5138. dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
  5139. out:
  5140. dprintk("<-- %s\n", __func__);
  5141. }
  5142. static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
  5143. {
  5144. struct nfs4_sequence_data *calldata = data;
  5145. struct nfs_client *clp = calldata->clp;
  5146. struct nfs4_sequence_args *args;
  5147. struct nfs4_sequence_res *res;
  5148. args = task->tk_msg.rpc_argp;
  5149. res = task->tk_msg.rpc_resp;
  5150. if (nfs41_setup_sequence(clp->cl_session, args, res, task))
  5151. return;
  5152. rpc_call_start(task);
  5153. }
  5154. static void nfs41_sequence_prepare_privileged(struct rpc_task *task, void *data)
  5155. {
  5156. rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
  5157. nfs41_sequence_prepare(task, data);
  5158. }
  5159. static const struct rpc_call_ops nfs41_sequence_ops = {
  5160. .rpc_call_done = nfs41_sequence_call_done,
  5161. .rpc_call_prepare = nfs41_sequence_prepare,
  5162. .rpc_release = nfs41_sequence_release,
  5163. };
  5164. static const struct rpc_call_ops nfs41_sequence_privileged_ops = {
  5165. .rpc_call_done = nfs41_sequence_call_done,
  5166. .rpc_call_prepare = nfs41_sequence_prepare_privileged,
  5167. .rpc_release = nfs41_sequence_release,
  5168. };
  5169. static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred,
  5170. const struct rpc_call_ops *seq_ops)
  5171. {
  5172. struct nfs4_sequence_data *calldata;
  5173. struct rpc_message msg = {
  5174. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
  5175. .rpc_cred = cred,
  5176. };
  5177. struct rpc_task_setup task_setup_data = {
  5178. .rpc_client = clp->cl_rpcclient,
  5179. .rpc_message = &msg,
  5180. .callback_ops = seq_ops,
  5181. .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
  5182. };
  5183. if (!atomic_inc_not_zero(&clp->cl_count))
  5184. return ERR_PTR(-EIO);
  5185. calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
  5186. if (calldata == NULL) {
  5187. nfs_put_client(clp);
  5188. return ERR_PTR(-ENOMEM);
  5189. }
  5190. nfs41_init_sequence(&calldata->args, &calldata->res, 0);
  5191. msg.rpc_argp = &calldata->args;
  5192. msg.rpc_resp = &calldata->res;
  5193. calldata->clp = clp;
  5194. task_setup_data.callback_data = calldata;
  5195. return rpc_run_task(&task_setup_data);
  5196. }
  5197. static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
  5198. {
  5199. struct rpc_task *task;
  5200. int ret = 0;
  5201. if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
  5202. return -EAGAIN;
  5203. task = _nfs41_proc_sequence(clp, cred, &nfs41_sequence_ops);
  5204. if (IS_ERR(task))
  5205. ret = PTR_ERR(task);
  5206. else
  5207. rpc_put_task_async(task);
  5208. dprintk("<-- %s status=%d\n", __func__, ret);
  5209. return ret;
  5210. }
  5211. static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
  5212. {
  5213. struct rpc_task *task;
  5214. int ret;
  5215. task = _nfs41_proc_sequence(clp, cred, &nfs41_sequence_privileged_ops);
  5216. if (IS_ERR(task)) {
  5217. ret = PTR_ERR(task);
  5218. goto out;
  5219. }
  5220. ret = rpc_wait_for_completion_task(task);
  5221. if (!ret) {
  5222. struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
  5223. if (task->tk_status == 0)
  5224. nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
  5225. ret = task->tk_status;
  5226. }
  5227. rpc_put_task(task);
  5228. out:
  5229. dprintk("<-- %s status=%d\n", __func__, ret);
  5230. return ret;
  5231. }
  5232. struct nfs4_reclaim_complete_data {
  5233. struct nfs_client *clp;
  5234. struct nfs41_reclaim_complete_args arg;
  5235. struct nfs41_reclaim_complete_res res;
  5236. };
  5237. static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
  5238. {
  5239. struct nfs4_reclaim_complete_data *calldata = data;
  5240. rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
  5241. if (nfs41_setup_sequence(calldata->clp->cl_session,
  5242. &calldata->arg.seq_args,
  5243. &calldata->res.seq_res, task))
  5244. return;
  5245. rpc_call_start(task);
  5246. }
  5247. static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
  5248. {
  5249. switch(task->tk_status) {
  5250. case 0:
  5251. case -NFS4ERR_COMPLETE_ALREADY:
  5252. case -NFS4ERR_WRONG_CRED: /* What to do here? */
  5253. break;
  5254. case -NFS4ERR_DELAY:
  5255. rpc_delay(task, NFS4_POLL_RETRY_MAX);
  5256. /* fall through */
  5257. case -NFS4ERR_RETRY_UNCACHED_REP:
  5258. return -EAGAIN;
  5259. default:
  5260. nfs4_schedule_lease_recovery(clp);
  5261. }
  5262. return 0;
  5263. }
  5264. static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
  5265. {
  5266. struct nfs4_reclaim_complete_data *calldata = data;
  5267. struct nfs_client *clp = calldata->clp;
  5268. struct nfs4_sequence_res *res = &calldata->res.seq_res;
  5269. dprintk("--> %s\n", __func__);
  5270. if (!nfs41_sequence_done(task, res))
  5271. return;
  5272. if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
  5273. rpc_restart_call_prepare(task);
  5274. return;
  5275. }
  5276. dprintk("<-- %s\n", __func__);
  5277. }
  5278. static void nfs4_free_reclaim_complete_data(void *data)
  5279. {
  5280. struct nfs4_reclaim_complete_data *calldata = data;
  5281. kfree(calldata);
  5282. }
  5283. static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
  5284. .rpc_call_prepare = nfs4_reclaim_complete_prepare,
  5285. .rpc_call_done = nfs4_reclaim_complete_done,
  5286. .rpc_release = nfs4_free_reclaim_complete_data,
  5287. };
  5288. /*
  5289. * Issue a global reclaim complete.
  5290. */
  5291. static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
  5292. {
  5293. struct nfs4_reclaim_complete_data *calldata;
  5294. struct rpc_task *task;
  5295. struct rpc_message msg = {
  5296. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
  5297. };
  5298. struct rpc_task_setup task_setup_data = {
  5299. .rpc_client = clp->cl_rpcclient,
  5300. .rpc_message = &msg,
  5301. .callback_ops = &nfs4_reclaim_complete_call_ops,
  5302. .flags = RPC_TASK_ASYNC,
  5303. };
  5304. int status = -ENOMEM;
  5305. dprintk("--> %s\n", __func__);
  5306. calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
  5307. if (calldata == NULL)
  5308. goto out;
  5309. calldata->clp = clp;
  5310. calldata->arg.one_fs = 0;
  5311. nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
  5312. msg.rpc_argp = &calldata->arg;
  5313. msg.rpc_resp = &calldata->res;
  5314. task_setup_data.callback_data = calldata;
  5315. task = rpc_run_task(&task_setup_data);
  5316. if (IS_ERR(task)) {
  5317. status = PTR_ERR(task);
  5318. goto out;
  5319. }
  5320. status = nfs4_wait_for_completion_rpc_task(task);
  5321. if (status == 0)
  5322. status = task->tk_status;
  5323. rpc_put_task(task);
  5324. return 0;
  5325. out:
  5326. dprintk("<-- %s status=%d\n", __func__, status);
  5327. return status;
  5328. }
  5329. static void
  5330. nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
  5331. {
  5332. struct nfs4_layoutget *lgp = calldata;
  5333. struct nfs_server *server = NFS_SERVER(lgp->args.inode);
  5334. dprintk("--> %s\n", __func__);
  5335. /* Note the is a race here, where a CB_LAYOUTRECALL can come in
  5336. * right now covering the LAYOUTGET we are about to send.
  5337. * However, that is not so catastrophic, and there seems
  5338. * to be no way to prevent it completely.
  5339. */
  5340. if (nfs4_setup_sequence(server, &lgp->args.seq_args,
  5341. &lgp->res.seq_res, task))
  5342. return;
  5343. if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
  5344. NFS_I(lgp->args.inode)->layout,
  5345. lgp->args.ctx->state)) {
  5346. rpc_exit(task, NFS4_OK);
  5347. return;
  5348. }
  5349. rpc_call_start(task);
  5350. }
  5351. static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
  5352. {
  5353. struct nfs4_layoutget *lgp = calldata;
  5354. struct nfs_server *server = NFS_SERVER(lgp->args.inode);
  5355. dprintk("--> %s\n", __func__);
  5356. if (!nfs4_sequence_done(task, &lgp->res.seq_res))
  5357. return;
  5358. switch (task->tk_status) {
  5359. case 0:
  5360. break;
  5361. case -NFS4ERR_LAYOUTTRYLATER:
  5362. case -NFS4ERR_RECALLCONFLICT:
  5363. task->tk_status = -NFS4ERR_DELAY;
  5364. /* Fall through */
  5365. default:
  5366. if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
  5367. rpc_restart_call_prepare(task);
  5368. return;
  5369. }
  5370. }
  5371. dprintk("<-- %s\n", __func__);
  5372. }
  5373. static size_t max_response_pages(struct nfs_server *server)
  5374. {
  5375. u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
  5376. return nfs_page_array_len(0, max_resp_sz);
  5377. }
  5378. static void nfs4_free_pages(struct page **pages, size_t size)
  5379. {
  5380. int i;
  5381. if (!pages)
  5382. return;
  5383. for (i = 0; i < size; i++) {
  5384. if (!pages[i])
  5385. break;
  5386. __free_page(pages[i]);
  5387. }
  5388. kfree(pages);
  5389. }
  5390. static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
  5391. {
  5392. struct page **pages;
  5393. int i;
  5394. pages = kcalloc(size, sizeof(struct page *), gfp_flags);
  5395. if (!pages) {
  5396. dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
  5397. return NULL;
  5398. }
  5399. for (i = 0; i < size; i++) {
  5400. pages[i] = alloc_page(gfp_flags);
  5401. if (!pages[i]) {
  5402. dprintk("%s: failed to allocate page\n", __func__);
  5403. nfs4_free_pages(pages, size);
  5404. return NULL;
  5405. }
  5406. }
  5407. return pages;
  5408. }
  5409. static void nfs4_layoutget_release(void *calldata)
  5410. {
  5411. struct nfs4_layoutget *lgp = calldata;
  5412. struct nfs_server *server = NFS_SERVER(lgp->args.inode);
  5413. size_t max_pages = max_response_pages(server);
  5414. dprintk("--> %s\n", __func__);
  5415. nfs4_free_pages(lgp->args.layout.pages, max_pages);
  5416. put_nfs_open_context(lgp->args.ctx);
  5417. kfree(calldata);
  5418. dprintk("<-- %s\n", __func__);
  5419. }
  5420. static const struct rpc_call_ops nfs4_layoutget_call_ops = {
  5421. .rpc_call_prepare = nfs4_layoutget_prepare,
  5422. .rpc_call_done = nfs4_layoutget_done,
  5423. .rpc_release = nfs4_layoutget_release,
  5424. };
  5425. int nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
  5426. {
  5427. struct nfs_server *server = NFS_SERVER(lgp->args.inode);
  5428. size_t max_pages = max_response_pages(server);
  5429. struct rpc_task *task;
  5430. struct rpc_message msg = {
  5431. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
  5432. .rpc_argp = &lgp->args,
  5433. .rpc_resp = &lgp->res,
  5434. };
  5435. struct rpc_task_setup task_setup_data = {
  5436. .rpc_client = server->client,
  5437. .rpc_message = &msg,
  5438. .callback_ops = &nfs4_layoutget_call_ops,
  5439. .callback_data = lgp,
  5440. .flags = RPC_TASK_ASYNC,
  5441. };
  5442. int status = 0;
  5443. dprintk("--> %s\n", __func__);
  5444. lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
  5445. if (!lgp->args.layout.pages) {
  5446. nfs4_layoutget_release(lgp);
  5447. return -ENOMEM;
  5448. }
  5449. lgp->args.layout.pglen = max_pages * PAGE_SIZE;
  5450. lgp->res.layoutp = &lgp->args.layout;
  5451. lgp->res.seq_res.sr_slot = NULL;
  5452. nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
  5453. task = rpc_run_task(&task_setup_data);
  5454. if (IS_ERR(task))
  5455. return PTR_ERR(task);
  5456. status = nfs4_wait_for_completion_rpc_task(task);
  5457. if (status == 0)
  5458. status = task->tk_status;
  5459. /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
  5460. if (status == 0 && lgp->res.layoutp->len)
  5461. status = pnfs_layout_process(lgp);
  5462. rpc_put_task(task);
  5463. dprintk("<-- %s status=%d\n", __func__, status);
  5464. return status;
  5465. }
  5466. static void
  5467. nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
  5468. {
  5469. struct nfs4_layoutreturn *lrp = calldata;
  5470. dprintk("--> %s\n", __func__);
  5471. if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
  5472. &lrp->res.seq_res, task))
  5473. return;
  5474. rpc_call_start(task);
  5475. }
  5476. static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
  5477. {
  5478. struct nfs4_layoutreturn *lrp = calldata;
  5479. struct nfs_server *server;
  5480. struct pnfs_layout_hdr *lo = lrp->args.layout;
  5481. dprintk("--> %s\n", __func__);
  5482. if (!nfs4_sequence_done(task, &lrp->res.seq_res))
  5483. return;
  5484. server = NFS_SERVER(lrp->args.inode);
  5485. if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
  5486. rpc_restart_call_prepare(task);
  5487. return;
  5488. }
  5489. spin_lock(&lo->plh_inode->i_lock);
  5490. if (task->tk_status == 0 && lrp->res.lrs_present)
  5491. pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
  5492. lo->plh_block_lgets--;
  5493. spin_unlock(&lo->plh_inode->i_lock);
  5494. dprintk("<-- %s\n", __func__);
  5495. }
  5496. static void nfs4_layoutreturn_release(void *calldata)
  5497. {
  5498. struct nfs4_layoutreturn *lrp = calldata;
  5499. dprintk("--> %s\n", __func__);
  5500. put_layout_hdr(lrp->args.layout);
  5501. kfree(calldata);
  5502. dprintk("<-- %s\n", __func__);
  5503. }
  5504. static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
  5505. .rpc_call_prepare = nfs4_layoutreturn_prepare,
  5506. .rpc_call_done = nfs4_layoutreturn_done,
  5507. .rpc_release = nfs4_layoutreturn_release,
  5508. };
  5509. int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
  5510. {
  5511. struct rpc_task *task;
  5512. struct rpc_message msg = {
  5513. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
  5514. .rpc_argp = &lrp->args,
  5515. .rpc_resp = &lrp->res,
  5516. };
  5517. struct rpc_task_setup task_setup_data = {
  5518. .rpc_client = lrp->clp->cl_rpcclient,
  5519. .rpc_message = &msg,
  5520. .callback_ops = &nfs4_layoutreturn_call_ops,
  5521. .callback_data = lrp,
  5522. };
  5523. int status;
  5524. dprintk("--> %s\n", __func__);
  5525. nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
  5526. task = rpc_run_task(&task_setup_data);
  5527. if (IS_ERR(task))
  5528. return PTR_ERR(task);
  5529. status = task->tk_status;
  5530. dprintk("<-- %s status=%d\n", __func__, status);
  5531. rpc_put_task(task);
  5532. return status;
  5533. }
  5534. /*
  5535. * Retrieve the list of Data Server devices from the MDS.
  5536. */
  5537. static int _nfs4_getdevicelist(struct nfs_server *server,
  5538. const struct nfs_fh *fh,
  5539. struct pnfs_devicelist *devlist)
  5540. {
  5541. struct nfs4_getdevicelist_args args = {
  5542. .fh = fh,
  5543. .layoutclass = server->pnfs_curr_ld->id,
  5544. };
  5545. struct nfs4_getdevicelist_res res = {
  5546. .devlist = devlist,
  5547. };
  5548. struct rpc_message msg = {
  5549. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
  5550. .rpc_argp = &args,
  5551. .rpc_resp = &res,
  5552. };
  5553. int status;
  5554. dprintk("--> %s\n", __func__);
  5555. status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
  5556. &res.seq_res, 0);
  5557. dprintk("<-- %s status=%d\n", __func__, status);
  5558. return status;
  5559. }
  5560. int nfs4_proc_getdevicelist(struct nfs_server *server,
  5561. const struct nfs_fh *fh,
  5562. struct pnfs_devicelist *devlist)
  5563. {
  5564. struct nfs4_exception exception = { };
  5565. int err;
  5566. do {
  5567. err = nfs4_handle_exception(server,
  5568. _nfs4_getdevicelist(server, fh, devlist),
  5569. &exception);
  5570. } while (exception.retry);
  5571. dprintk("%s: err=%d, num_devs=%u\n", __func__,
  5572. err, devlist->num_devs);
  5573. return err;
  5574. }
  5575. EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
  5576. static int
  5577. _nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
  5578. {
  5579. struct nfs4_getdeviceinfo_args args = {
  5580. .pdev = pdev,
  5581. };
  5582. struct nfs4_getdeviceinfo_res res = {
  5583. .pdev = pdev,
  5584. };
  5585. struct rpc_message msg = {
  5586. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
  5587. .rpc_argp = &args,
  5588. .rpc_resp = &res,
  5589. };
  5590. int status;
  5591. dprintk("--> %s\n", __func__);
  5592. status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  5593. dprintk("<-- %s status=%d\n", __func__, status);
  5594. return status;
  5595. }
  5596. int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
  5597. {
  5598. struct nfs4_exception exception = { };
  5599. int err;
  5600. do {
  5601. err = nfs4_handle_exception(server,
  5602. _nfs4_proc_getdeviceinfo(server, pdev),
  5603. &exception);
  5604. } while (exception.retry);
  5605. return err;
  5606. }
  5607. EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
  5608. static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
  5609. {
  5610. struct nfs4_layoutcommit_data *data = calldata;
  5611. struct nfs_server *server = NFS_SERVER(data->args.inode);
  5612. if (nfs4_setup_sequence(server, &data->args.seq_args,
  5613. &data->res.seq_res, task))
  5614. return;
  5615. rpc_call_start(task);
  5616. }
  5617. static void
  5618. nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
  5619. {
  5620. struct nfs4_layoutcommit_data *data = calldata;
  5621. struct nfs_server *server = NFS_SERVER(data->args.inode);
  5622. if (!nfs4_sequence_done(task, &data->res.seq_res))
  5623. return;
  5624. switch (task->tk_status) { /* Just ignore these failures */
  5625. case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
  5626. case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
  5627. case -NFS4ERR_BADLAYOUT: /* no layout */
  5628. case -NFS4ERR_GRACE: /* loca_recalim always false */
  5629. task->tk_status = 0;
  5630. break;
  5631. case 0:
  5632. nfs_post_op_update_inode_force_wcc(data->args.inode,
  5633. data->res.fattr);
  5634. break;
  5635. default:
  5636. if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
  5637. rpc_restart_call_prepare(task);
  5638. return;
  5639. }
  5640. }
  5641. }
  5642. static void nfs4_layoutcommit_release(void *calldata)
  5643. {
  5644. struct nfs4_layoutcommit_data *data = calldata;
  5645. pnfs_cleanup_layoutcommit(data);
  5646. put_rpccred(data->cred);
  5647. kfree(data);
  5648. }
  5649. static const struct rpc_call_ops nfs4_layoutcommit_ops = {
  5650. .rpc_call_prepare = nfs4_layoutcommit_prepare,
  5651. .rpc_call_done = nfs4_layoutcommit_done,
  5652. .rpc_release = nfs4_layoutcommit_release,
  5653. };
  5654. int
  5655. nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
  5656. {
  5657. struct rpc_message msg = {
  5658. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
  5659. .rpc_argp = &data->args,
  5660. .rpc_resp = &data->res,
  5661. .rpc_cred = data->cred,
  5662. };
  5663. struct rpc_task_setup task_setup_data = {
  5664. .task = &data->task,
  5665. .rpc_client = NFS_CLIENT(data->args.inode),
  5666. .rpc_message = &msg,
  5667. .callback_ops = &nfs4_layoutcommit_ops,
  5668. .callback_data = data,
  5669. .flags = RPC_TASK_ASYNC,
  5670. };
  5671. struct rpc_task *task;
  5672. int status = 0;
  5673. dprintk("NFS: %4d initiating layoutcommit call. sync %d "
  5674. "lbw: %llu inode %lu\n",
  5675. data->task.tk_pid, sync,
  5676. data->args.lastbytewritten,
  5677. data->args.inode->i_ino);
  5678. nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
  5679. task = rpc_run_task(&task_setup_data);
  5680. if (IS_ERR(task))
  5681. return PTR_ERR(task);
  5682. if (sync == false)
  5683. goto out;
  5684. status = nfs4_wait_for_completion_rpc_task(task);
  5685. if (status != 0)
  5686. goto out;
  5687. status = task->tk_status;
  5688. out:
  5689. dprintk("%s: status %d\n", __func__, status);
  5690. rpc_put_task(task);
  5691. return status;
  5692. }
  5693. static int
  5694. _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
  5695. struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
  5696. {
  5697. struct nfs41_secinfo_no_name_args args = {
  5698. .style = SECINFO_STYLE_CURRENT_FH,
  5699. };
  5700. struct nfs4_secinfo_res res = {
  5701. .flavors = flavors,
  5702. };
  5703. struct rpc_message msg = {
  5704. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
  5705. .rpc_argp = &args,
  5706. .rpc_resp = &res,
  5707. };
  5708. return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
  5709. }
  5710. static int
  5711. nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
  5712. struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
  5713. {
  5714. struct nfs4_exception exception = { };
  5715. int err;
  5716. do {
  5717. err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
  5718. switch (err) {
  5719. case 0:
  5720. case -NFS4ERR_WRONGSEC:
  5721. case -ENOTSUPP:
  5722. goto out;
  5723. default:
  5724. err = nfs4_handle_exception(server, err, &exception);
  5725. }
  5726. } while (exception.retry);
  5727. out:
  5728. return err;
  5729. }
  5730. static int
  5731. nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
  5732. struct nfs_fsinfo *info)
  5733. {
  5734. int err;
  5735. struct page *page;
  5736. rpc_authflavor_t flavor;
  5737. struct nfs4_secinfo_flavors *flavors;
  5738. page = alloc_page(GFP_KERNEL);
  5739. if (!page) {
  5740. err = -ENOMEM;
  5741. goto out;
  5742. }
  5743. flavors = page_address(page);
  5744. err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
  5745. /*
  5746. * Fall back on "guess and check" method if
  5747. * the server doesn't support SECINFO_NO_NAME
  5748. */
  5749. if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
  5750. err = nfs4_find_root_sec(server, fhandle, info);
  5751. goto out_freepage;
  5752. }
  5753. if (err)
  5754. goto out_freepage;
  5755. flavor = nfs_find_best_sec(flavors);
  5756. if (err == 0)
  5757. err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
  5758. out_freepage:
  5759. put_page(page);
  5760. if (err == -EACCES)
  5761. return -EPERM;
  5762. out:
  5763. return err;
  5764. }
  5765. static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
  5766. {
  5767. int status;
  5768. struct nfs41_test_stateid_args args = {
  5769. .stateid = stateid,
  5770. };
  5771. struct nfs41_test_stateid_res res;
  5772. struct rpc_message msg = {
  5773. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
  5774. .rpc_argp = &args,
  5775. .rpc_resp = &res,
  5776. };
  5777. nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
  5778. status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
  5779. if (status == NFS_OK)
  5780. return res.status;
  5781. return status;
  5782. }
  5783. static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
  5784. {
  5785. struct nfs4_exception exception = { };
  5786. int err;
  5787. do {
  5788. err = nfs4_handle_exception(server,
  5789. _nfs41_test_stateid(server, stateid),
  5790. &exception);
  5791. } while (exception.retry);
  5792. return err;
  5793. }
  5794. static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
  5795. {
  5796. struct nfs41_free_stateid_args args = {
  5797. .stateid = stateid,
  5798. };
  5799. struct nfs41_free_stateid_res res;
  5800. struct rpc_message msg = {
  5801. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
  5802. .rpc_argp = &args,
  5803. .rpc_resp = &res,
  5804. };
  5805. nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
  5806. return nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
  5807. }
  5808. static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
  5809. {
  5810. struct nfs4_exception exception = { };
  5811. int err;
  5812. do {
  5813. err = nfs4_handle_exception(server,
  5814. _nfs4_free_stateid(server, stateid),
  5815. &exception);
  5816. } while (exception.retry);
  5817. return err;
  5818. }
  5819. static bool nfs41_match_stateid(const nfs4_stateid *s1,
  5820. const nfs4_stateid *s2)
  5821. {
  5822. if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
  5823. return false;
  5824. if (s1->seqid == s2->seqid)
  5825. return true;
  5826. if (s1->seqid == 0 || s2->seqid == 0)
  5827. return true;
  5828. return false;
  5829. }
  5830. #endif /* CONFIG_NFS_V4_1 */
  5831. static bool nfs4_match_stateid(const nfs4_stateid *s1,
  5832. const nfs4_stateid *s2)
  5833. {
  5834. return nfs4_stateid_match(s1, s2);
  5835. }
  5836. static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
  5837. .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
  5838. .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
  5839. .recover_open = nfs4_open_reclaim,
  5840. .recover_lock = nfs4_lock_reclaim,
  5841. .establish_clid = nfs4_init_clientid,
  5842. .get_clid_cred = nfs4_get_setclientid_cred,
  5843. };
  5844. #if defined(CONFIG_NFS_V4_1)
  5845. static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
  5846. .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
  5847. .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
  5848. .recover_open = nfs4_open_reclaim,
  5849. .recover_lock = nfs4_lock_reclaim,
  5850. .establish_clid = nfs41_init_clientid,
  5851. .get_clid_cred = nfs4_get_exchange_id_cred,
  5852. .reclaim_complete = nfs41_proc_reclaim_complete,
  5853. };
  5854. #endif /* CONFIG_NFS_V4_1 */
  5855. static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
  5856. .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
  5857. .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
  5858. .recover_open = nfs40_open_expired,
  5859. .recover_lock = nfs4_lock_expired,
  5860. .establish_clid = nfs4_init_clientid,
  5861. .get_clid_cred = nfs4_get_setclientid_cred,
  5862. };
  5863. #if defined(CONFIG_NFS_V4_1)
  5864. static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
  5865. .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
  5866. .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
  5867. .recover_open = nfs41_open_expired,
  5868. .recover_lock = nfs41_lock_expired,
  5869. .establish_clid = nfs41_init_clientid,
  5870. .get_clid_cred = nfs4_get_exchange_id_cred,
  5871. };
  5872. #endif /* CONFIG_NFS_V4_1 */
  5873. static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
  5874. .sched_state_renewal = nfs4_proc_async_renew,
  5875. .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
  5876. .renew_lease = nfs4_proc_renew,
  5877. };
  5878. #if defined(CONFIG_NFS_V4_1)
  5879. static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
  5880. .sched_state_renewal = nfs41_proc_async_sequence,
  5881. .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
  5882. .renew_lease = nfs4_proc_sequence,
  5883. };
  5884. #endif
  5885. static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
  5886. .minor_version = 0,
  5887. .call_sync = _nfs4_call_sync,
  5888. .match_stateid = nfs4_match_stateid,
  5889. .find_root_sec = nfs4_find_root_sec,
  5890. .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
  5891. .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
  5892. .state_renewal_ops = &nfs40_state_renewal_ops,
  5893. };
  5894. #if defined(CONFIG_NFS_V4_1)
  5895. static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
  5896. .minor_version = 1,
  5897. .call_sync = _nfs4_call_sync_session,
  5898. .match_stateid = nfs41_match_stateid,
  5899. .find_root_sec = nfs41_find_root_sec,
  5900. .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
  5901. .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
  5902. .state_renewal_ops = &nfs41_state_renewal_ops,
  5903. .mig_recovery_ops = &nfs41_mig_recovery_ops,
  5904. };
  5905. #endif
  5906. const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
  5907. [0] = &nfs_v4_0_minor_ops,
  5908. #if defined(CONFIG_NFS_V4_1)
  5909. [1] = &nfs_v4_1_minor_ops,
  5910. #endif
  5911. };
  5912. static const struct inode_operations nfs4_file_inode_operations = {
  5913. .permission = nfs_permission,
  5914. .getattr = nfs_getattr,
  5915. .setattr = nfs_setattr,
  5916. .getxattr = generic_getxattr,
  5917. .setxattr = generic_setxattr,
  5918. .listxattr = generic_listxattr,
  5919. .removexattr = generic_removexattr,
  5920. };
  5921. const struct nfs_rpc_ops nfs_v4_clientops = {
  5922. .version = 4, /* protocol version */
  5923. .dentry_ops = &nfs4_dentry_operations,
  5924. .dir_inode_ops = &nfs4_dir_inode_operations,
  5925. .file_inode_ops = &nfs4_file_inode_operations,
  5926. .file_ops = &nfs4_file_operations,
  5927. .getroot = nfs4_proc_get_root,
  5928. .getattr = nfs4_proc_getattr,
  5929. .setattr = nfs4_proc_setattr,
  5930. .lookup = nfs4_proc_lookup,
  5931. .access = nfs4_proc_access,
  5932. .readlink = nfs4_proc_readlink,
  5933. .create = nfs4_proc_create,
  5934. .remove = nfs4_proc_remove,
  5935. .unlink_setup = nfs4_proc_unlink_setup,
  5936. .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
  5937. .unlink_done = nfs4_proc_unlink_done,
  5938. .rename = nfs4_proc_rename,
  5939. .rename_setup = nfs4_proc_rename_setup,
  5940. .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
  5941. .rename_done = nfs4_proc_rename_done,
  5942. .link = nfs4_proc_link,
  5943. .symlink = nfs4_proc_symlink,
  5944. .mkdir = nfs4_proc_mkdir,
  5945. .rmdir = nfs4_proc_remove,
  5946. .readdir = nfs4_proc_readdir,
  5947. .mknod = nfs4_proc_mknod,
  5948. .statfs = nfs4_proc_statfs,
  5949. .fsinfo = nfs4_proc_fsinfo,
  5950. .pathconf = nfs4_proc_pathconf,
  5951. .set_capabilities = nfs4_server_capabilities,
  5952. .decode_dirent = nfs4_decode_dirent,
  5953. .read_setup = nfs4_proc_read_setup,
  5954. .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
  5955. .read_done = nfs4_read_done,
  5956. .write_setup = nfs4_proc_write_setup,
  5957. .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
  5958. .write_done = nfs4_write_done,
  5959. .commit_setup = nfs4_proc_commit_setup,
  5960. .commit_done = nfs4_commit_done,
  5961. .lock = nfs4_proc_lock,
  5962. .clear_acl_cache = nfs4_zap_acl_attr,
  5963. .close_context = nfs4_close_context,
  5964. .open_context = nfs4_atomic_open,
  5965. .init_client = nfs4_init_client,
  5966. .secinfo = nfs4_proc_secinfo,
  5967. };
  5968. static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
  5969. .prefix = XATTR_NAME_NFSV4_ACL,
  5970. .list = nfs4_xattr_list_nfs4_acl,
  5971. .get = nfs4_xattr_get_nfs4_acl,
  5972. .set = nfs4_xattr_set_nfs4_acl,
  5973. };
  5974. const struct xattr_handler *nfs4_xattr_handlers[] = {
  5975. &nfs4_xattr_nfs4_acl_handler,
  5976. NULL
  5977. };
  5978. module_param(max_session_slots, ushort, 0644);
  5979. MODULE_PARM_DESC(max_session_slots, "Maximum number of outstanding NFSv4.1 "
  5980. "requests the client will negotiate");
  5981. /*
  5982. * Local variables:
  5983. * c-basic-offset: 8
  5984. * End:
  5985. */