nfs4proc.c 143 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370
  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/slab.h>
  42. #include <linux/sunrpc/clnt.h>
  43. #include <linux/nfs.h>
  44. #include <linux/nfs4.h>
  45. #include <linux/nfs_fs.h>
  46. #include <linux/nfs_page.h>
  47. #include <linux/namei.h>
  48. #include <linux/mount.h>
  49. #include <linux/module.h>
  50. #include <linux/sunrpc/bc_xprt.h>
  51. #include "nfs4_fs.h"
  52. #include "delegation.h"
  53. #include "internal.h"
  54. #include "iostat.h"
  55. #include "callback.h"
  56. #define NFSDBG_FACILITY NFSDBG_PROC
  57. #define NFS4_POLL_RETRY_MIN (HZ/10)
  58. #define NFS4_POLL_RETRY_MAX (15*HZ)
  59. #define NFS4_MAX_LOOP_ON_RECOVER (10)
  60. struct nfs4_opendata;
  61. static int _nfs4_proc_open(struct nfs4_opendata *data);
  62. static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
  63. static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
  64. static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
  65. static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
  66. static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
  67. /* Prevent leaks of NFSv4 errors into userland */
  68. static int nfs4_map_errors(int err)
  69. {
  70. if (err >= -1000)
  71. return err;
  72. switch (err) {
  73. case -NFS4ERR_RESOURCE:
  74. return -EREMOTEIO;
  75. default:
  76. dprintk("%s could not handle NFSv4 error %d\n",
  77. __func__, -err);
  78. break;
  79. }
  80. return -EIO;
  81. }
  82. /*
  83. * This is our standard bitmap for GETATTR requests.
  84. */
  85. const u32 nfs4_fattr_bitmap[2] = {
  86. FATTR4_WORD0_TYPE
  87. | FATTR4_WORD0_CHANGE
  88. | FATTR4_WORD0_SIZE
  89. | FATTR4_WORD0_FSID
  90. | FATTR4_WORD0_FILEID,
  91. FATTR4_WORD1_MODE
  92. | FATTR4_WORD1_NUMLINKS
  93. | FATTR4_WORD1_OWNER
  94. | FATTR4_WORD1_OWNER_GROUP
  95. | FATTR4_WORD1_RAWDEV
  96. | FATTR4_WORD1_SPACE_USED
  97. | FATTR4_WORD1_TIME_ACCESS
  98. | FATTR4_WORD1_TIME_METADATA
  99. | FATTR4_WORD1_TIME_MODIFY
  100. };
  101. const u32 nfs4_statfs_bitmap[2] = {
  102. FATTR4_WORD0_FILES_AVAIL
  103. | FATTR4_WORD0_FILES_FREE
  104. | FATTR4_WORD0_FILES_TOTAL,
  105. FATTR4_WORD1_SPACE_AVAIL
  106. | FATTR4_WORD1_SPACE_FREE
  107. | FATTR4_WORD1_SPACE_TOTAL
  108. };
  109. const u32 nfs4_pathconf_bitmap[2] = {
  110. FATTR4_WORD0_MAXLINK
  111. | FATTR4_WORD0_MAXNAME,
  112. 0
  113. };
  114. const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
  115. | FATTR4_WORD0_MAXREAD
  116. | FATTR4_WORD0_MAXWRITE
  117. | FATTR4_WORD0_LEASE_TIME,
  118. 0
  119. };
  120. const u32 nfs4_fs_locations_bitmap[2] = {
  121. FATTR4_WORD0_TYPE
  122. | FATTR4_WORD0_CHANGE
  123. | FATTR4_WORD0_SIZE
  124. | FATTR4_WORD0_FSID
  125. | FATTR4_WORD0_FILEID
  126. | FATTR4_WORD0_FS_LOCATIONS,
  127. FATTR4_WORD1_MODE
  128. | FATTR4_WORD1_NUMLINKS
  129. | FATTR4_WORD1_OWNER
  130. | FATTR4_WORD1_OWNER_GROUP
  131. | FATTR4_WORD1_RAWDEV
  132. | FATTR4_WORD1_SPACE_USED
  133. | FATTR4_WORD1_TIME_ACCESS
  134. | FATTR4_WORD1_TIME_METADATA
  135. | FATTR4_WORD1_TIME_MODIFY
  136. | FATTR4_WORD1_MOUNTED_ON_FILEID
  137. };
  138. static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
  139. struct nfs4_readdir_arg *readdir)
  140. {
  141. __be32 *start, *p;
  142. BUG_ON(readdir->count < 80);
  143. if (cookie > 2) {
  144. readdir->cookie = cookie;
  145. memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
  146. return;
  147. }
  148. readdir->cookie = 0;
  149. memset(&readdir->verifier, 0, sizeof(readdir->verifier));
  150. if (cookie == 2)
  151. return;
  152. /*
  153. * NFSv4 servers do not return entries for '.' and '..'
  154. * Therefore, we fake these entries here. We let '.'
  155. * have cookie 0 and '..' have cookie 1. Note that
  156. * when talking to the server, we always send cookie 0
  157. * instead of 1 or 2.
  158. */
  159. start = p = kmap_atomic(*readdir->pages, KM_USER0);
  160. if (cookie == 0) {
  161. *p++ = xdr_one; /* next */
  162. *p++ = xdr_zero; /* cookie, first word */
  163. *p++ = xdr_one; /* cookie, second word */
  164. *p++ = xdr_one; /* entry len */
  165. memcpy(p, ".\0\0\0", 4); /* entry */
  166. p++;
  167. *p++ = xdr_one; /* bitmap length */
  168. *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
  169. *p++ = htonl(8); /* attribute buffer length */
  170. p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
  171. }
  172. *p++ = xdr_one; /* next */
  173. *p++ = xdr_zero; /* cookie, first word */
  174. *p++ = xdr_two; /* cookie, second word */
  175. *p++ = xdr_two; /* entry len */
  176. memcpy(p, "..\0\0", 4); /* entry */
  177. p++;
  178. *p++ = xdr_one; /* bitmap length */
  179. *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
  180. *p++ = htonl(8); /* attribute buffer length */
  181. p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
  182. readdir->pgbase = (char *)p - (char *)start;
  183. readdir->count -= readdir->pgbase;
  184. kunmap_atomic(start, KM_USER0);
  185. }
  186. static int nfs4_wait_clnt_recover(struct nfs_client *clp)
  187. {
  188. int res;
  189. might_sleep();
  190. res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
  191. nfs_wait_bit_killable, TASK_KILLABLE);
  192. return res;
  193. }
  194. static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
  195. {
  196. int res = 0;
  197. might_sleep();
  198. if (*timeout <= 0)
  199. *timeout = NFS4_POLL_RETRY_MIN;
  200. if (*timeout > NFS4_POLL_RETRY_MAX)
  201. *timeout = NFS4_POLL_RETRY_MAX;
  202. schedule_timeout_killable(*timeout);
  203. if (fatal_signal_pending(current))
  204. res = -ERESTARTSYS;
  205. *timeout <<= 1;
  206. return res;
  207. }
  208. /* This is the error handling routine for processes that are allowed
  209. * to sleep.
  210. */
  211. static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
  212. {
  213. struct nfs_client *clp = server->nfs_client;
  214. struct nfs4_state *state = exception->state;
  215. int ret = errorcode;
  216. exception->retry = 0;
  217. switch(errorcode) {
  218. case 0:
  219. return 0;
  220. case -NFS4ERR_ADMIN_REVOKED:
  221. case -NFS4ERR_BAD_STATEID:
  222. case -NFS4ERR_OPENMODE:
  223. if (state == NULL)
  224. break;
  225. nfs4_state_mark_reclaim_nograce(clp, state);
  226. goto do_state_recovery;
  227. case -NFS4ERR_STALE_STATEID:
  228. if (state == NULL)
  229. break;
  230. nfs4_state_mark_reclaim_reboot(clp, state);
  231. case -NFS4ERR_STALE_CLIENTID:
  232. case -NFS4ERR_EXPIRED:
  233. goto do_state_recovery;
  234. #if defined(CONFIG_NFS_V4_1)
  235. case -NFS4ERR_BADSESSION:
  236. case -NFS4ERR_BADSLOT:
  237. case -NFS4ERR_BAD_HIGH_SLOT:
  238. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  239. case -NFS4ERR_DEADSESSION:
  240. case -NFS4ERR_SEQ_FALSE_RETRY:
  241. case -NFS4ERR_SEQ_MISORDERED:
  242. dprintk("%s ERROR: %d Reset session\n", __func__,
  243. errorcode);
  244. nfs4_schedule_state_recovery(clp);
  245. exception->retry = 1;
  246. break;
  247. #endif /* defined(CONFIG_NFS_V4_1) */
  248. case -NFS4ERR_FILE_OPEN:
  249. if (exception->timeout > HZ) {
  250. /* We have retried a decent amount, time to
  251. * fail
  252. */
  253. ret = -EBUSY;
  254. break;
  255. }
  256. case -NFS4ERR_GRACE:
  257. case -NFS4ERR_DELAY:
  258. case -EKEYEXPIRED:
  259. ret = nfs4_delay(server->client, &exception->timeout);
  260. if (ret != 0)
  261. break;
  262. case -NFS4ERR_OLD_STATEID:
  263. exception->retry = 1;
  264. }
  265. /* We failed to handle the error */
  266. return nfs4_map_errors(ret);
  267. do_state_recovery:
  268. nfs4_schedule_state_recovery(clp);
  269. ret = nfs4_wait_clnt_recover(clp);
  270. if (ret == 0)
  271. exception->retry = 1;
  272. return ret;
  273. }
  274. static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
  275. {
  276. struct nfs_client *clp = server->nfs_client;
  277. spin_lock(&clp->cl_lock);
  278. if (time_before(clp->cl_last_renewal,timestamp))
  279. clp->cl_last_renewal = timestamp;
  280. spin_unlock(&clp->cl_lock);
  281. }
  282. #if defined(CONFIG_NFS_V4_1)
  283. /*
  284. * nfs4_free_slot - free a slot and efficiently update slot table.
  285. *
  286. * freeing a slot is trivially done by clearing its respective bit
  287. * in the bitmap.
  288. * If the freed slotid equals highest_used_slotid we want to update it
  289. * so that the server would be able to size down the slot table if needed,
  290. * otherwise we know that the highest_used_slotid is still in use.
  291. * When updating highest_used_slotid there may be "holes" in the bitmap
  292. * so we need to scan down from highest_used_slotid to 0 looking for the now
  293. * highest slotid in use.
  294. * If none found, highest_used_slotid is set to -1.
  295. *
  296. * Must be called while holding tbl->slot_tbl_lock
  297. */
  298. static void
  299. nfs4_free_slot(struct nfs4_slot_table *tbl, u8 free_slotid)
  300. {
  301. int slotid = free_slotid;
  302. /* clear used bit in bitmap */
  303. __clear_bit(slotid, tbl->used_slots);
  304. /* update highest_used_slotid when it is freed */
  305. if (slotid == tbl->highest_used_slotid) {
  306. slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
  307. if (slotid < tbl->max_slots)
  308. tbl->highest_used_slotid = slotid;
  309. else
  310. tbl->highest_used_slotid = -1;
  311. }
  312. dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
  313. free_slotid, tbl->highest_used_slotid);
  314. }
  315. /*
  316. * Signal state manager thread if session is drained
  317. */
  318. static void nfs41_check_drain_session_complete(struct nfs4_session *ses)
  319. {
  320. struct rpc_task *task;
  321. if (!test_bit(NFS4CLNT_SESSION_DRAINING, &ses->clp->cl_state)) {
  322. task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
  323. if (task)
  324. rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
  325. return;
  326. }
  327. if (ses->fc_slot_table.highest_used_slotid != -1)
  328. return;
  329. dprintk("%s COMPLETE: Session Drained\n", __func__);
  330. complete(&ses->complete);
  331. }
  332. static void nfs41_sequence_free_slot(const struct nfs_client *clp,
  333. struct nfs4_sequence_res *res)
  334. {
  335. struct nfs4_slot_table *tbl;
  336. tbl = &clp->cl_session->fc_slot_table;
  337. if (res->sr_slotid == NFS4_MAX_SLOT_TABLE) {
  338. /* just wake up the next guy waiting since
  339. * we may have not consumed a slot after all */
  340. dprintk("%s: No slot\n", __func__);
  341. return;
  342. }
  343. spin_lock(&tbl->slot_tbl_lock);
  344. nfs4_free_slot(tbl, res->sr_slotid);
  345. nfs41_check_drain_session_complete(clp->cl_session);
  346. spin_unlock(&tbl->slot_tbl_lock);
  347. res->sr_slotid = NFS4_MAX_SLOT_TABLE;
  348. }
  349. static void nfs41_sequence_done(struct nfs_client *clp,
  350. struct nfs4_sequence_res *res,
  351. int rpc_status)
  352. {
  353. unsigned long timestamp;
  354. struct nfs4_slot_table *tbl;
  355. struct nfs4_slot *slot;
  356. /*
  357. * sr_status remains 1 if an RPC level error occurred. The server
  358. * may or may not have processed the sequence operation..
  359. * Proceed as if the server received and processed the sequence
  360. * operation.
  361. */
  362. if (res->sr_status == 1)
  363. res->sr_status = NFS_OK;
  364. /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
  365. if (res->sr_slotid == NFS4_MAX_SLOT_TABLE)
  366. goto out;
  367. /* Check the SEQUENCE operation status */
  368. if (res->sr_status == 0) {
  369. tbl = &clp->cl_session->fc_slot_table;
  370. slot = tbl->slots + res->sr_slotid;
  371. /* Update the slot's sequence and clientid lease timer */
  372. ++slot->seq_nr;
  373. timestamp = res->sr_renewal_time;
  374. spin_lock(&clp->cl_lock);
  375. if (time_before(clp->cl_last_renewal, timestamp))
  376. clp->cl_last_renewal = timestamp;
  377. spin_unlock(&clp->cl_lock);
  378. /* Check sequence flags */
  379. if (atomic_read(&clp->cl_count) > 1)
  380. nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
  381. }
  382. out:
  383. /* The session may be reset by one of the error handlers. */
  384. dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
  385. nfs41_sequence_free_slot(clp, res);
  386. }
  387. /*
  388. * nfs4_find_slot - efficiently look for a free slot
  389. *
  390. * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
  391. * If found, we mark the slot as used, update the highest_used_slotid,
  392. * and respectively set up the sequence operation args.
  393. * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
  394. *
  395. * Note: must be called with under the slot_tbl_lock.
  396. */
  397. static u8
  398. nfs4_find_slot(struct nfs4_slot_table *tbl)
  399. {
  400. int slotid;
  401. u8 ret_id = NFS4_MAX_SLOT_TABLE;
  402. BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
  403. dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
  404. __func__, tbl->used_slots[0], tbl->highest_used_slotid,
  405. tbl->max_slots);
  406. slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
  407. if (slotid >= tbl->max_slots)
  408. goto out;
  409. __set_bit(slotid, tbl->used_slots);
  410. if (slotid > tbl->highest_used_slotid)
  411. tbl->highest_used_slotid = slotid;
  412. ret_id = slotid;
  413. out:
  414. dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
  415. __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
  416. return ret_id;
  417. }
  418. static int nfs41_setup_sequence(struct nfs4_session *session,
  419. struct nfs4_sequence_args *args,
  420. struct nfs4_sequence_res *res,
  421. int cache_reply,
  422. struct rpc_task *task)
  423. {
  424. struct nfs4_slot *slot;
  425. struct nfs4_slot_table *tbl;
  426. u8 slotid;
  427. dprintk("--> %s\n", __func__);
  428. /* slot already allocated? */
  429. if (res->sr_slotid != NFS4_MAX_SLOT_TABLE)
  430. return 0;
  431. memset(res, 0, sizeof(*res));
  432. res->sr_slotid = NFS4_MAX_SLOT_TABLE;
  433. tbl = &session->fc_slot_table;
  434. spin_lock(&tbl->slot_tbl_lock);
  435. if (test_bit(NFS4CLNT_SESSION_DRAINING, &session->clp->cl_state) &&
  436. !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
  437. /*
  438. * The state manager will wait until the slot table is empty.
  439. * Schedule the reset thread
  440. */
  441. rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
  442. spin_unlock(&tbl->slot_tbl_lock);
  443. dprintk("%s Schedule Session Reset\n", __func__);
  444. return -EAGAIN;
  445. }
  446. if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
  447. !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
  448. rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
  449. spin_unlock(&tbl->slot_tbl_lock);
  450. dprintk("%s enforce FIFO order\n", __func__);
  451. return -EAGAIN;
  452. }
  453. slotid = nfs4_find_slot(tbl);
  454. if (slotid == NFS4_MAX_SLOT_TABLE) {
  455. rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
  456. spin_unlock(&tbl->slot_tbl_lock);
  457. dprintk("<-- %s: no free slots\n", __func__);
  458. return -EAGAIN;
  459. }
  460. spin_unlock(&tbl->slot_tbl_lock);
  461. rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
  462. slot = tbl->slots + slotid;
  463. args->sa_session = session;
  464. args->sa_slotid = slotid;
  465. args->sa_cache_this = cache_reply;
  466. dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
  467. res->sr_session = session;
  468. res->sr_slotid = slotid;
  469. res->sr_renewal_time = jiffies;
  470. /*
  471. * sr_status is only set in decode_sequence, and so will remain
  472. * set to 1 if an rpc level failure occurs.
  473. */
  474. res->sr_status = 1;
  475. return 0;
  476. }
  477. int nfs4_setup_sequence(struct nfs_client *clp,
  478. struct nfs4_sequence_args *args,
  479. struct nfs4_sequence_res *res,
  480. int cache_reply,
  481. struct rpc_task *task)
  482. {
  483. int ret = 0;
  484. dprintk("--> %s clp %p session %p sr_slotid %d\n",
  485. __func__, clp, clp->cl_session, res->sr_slotid);
  486. if (!nfs4_has_session(clp))
  487. goto out;
  488. ret = nfs41_setup_sequence(clp->cl_session, args, res, cache_reply,
  489. task);
  490. if (ret && ret != -EAGAIN) {
  491. /* terminate rpc task */
  492. task->tk_status = ret;
  493. task->tk_action = NULL;
  494. }
  495. out:
  496. dprintk("<-- %s status=%d\n", __func__, ret);
  497. return ret;
  498. }
  499. struct nfs41_call_sync_data {
  500. struct nfs_client *clp;
  501. struct nfs4_sequence_args *seq_args;
  502. struct nfs4_sequence_res *seq_res;
  503. int cache_reply;
  504. };
  505. static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
  506. {
  507. struct nfs41_call_sync_data *data = calldata;
  508. dprintk("--> %s data->clp->cl_session %p\n", __func__,
  509. data->clp->cl_session);
  510. if (nfs4_setup_sequence(data->clp, data->seq_args,
  511. data->seq_res, data->cache_reply, task))
  512. return;
  513. rpc_call_start(task);
  514. }
  515. static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
  516. {
  517. rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
  518. nfs41_call_sync_prepare(task, calldata);
  519. }
  520. static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
  521. {
  522. struct nfs41_call_sync_data *data = calldata;
  523. nfs41_sequence_done(data->clp, data->seq_res, task->tk_status);
  524. }
  525. struct rpc_call_ops nfs41_call_sync_ops = {
  526. .rpc_call_prepare = nfs41_call_sync_prepare,
  527. .rpc_call_done = nfs41_call_sync_done,
  528. };
  529. struct rpc_call_ops nfs41_call_priv_sync_ops = {
  530. .rpc_call_prepare = nfs41_call_priv_sync_prepare,
  531. .rpc_call_done = nfs41_call_sync_done,
  532. };
  533. static int nfs4_call_sync_sequence(struct nfs_client *clp,
  534. struct rpc_clnt *clnt,
  535. struct rpc_message *msg,
  536. struct nfs4_sequence_args *args,
  537. struct nfs4_sequence_res *res,
  538. int cache_reply,
  539. int privileged)
  540. {
  541. int ret;
  542. struct rpc_task *task;
  543. struct nfs41_call_sync_data data = {
  544. .clp = clp,
  545. .seq_args = args,
  546. .seq_res = res,
  547. .cache_reply = cache_reply,
  548. };
  549. struct rpc_task_setup task_setup = {
  550. .rpc_client = clnt,
  551. .rpc_message = msg,
  552. .callback_ops = &nfs41_call_sync_ops,
  553. .callback_data = &data
  554. };
  555. res->sr_slotid = NFS4_MAX_SLOT_TABLE;
  556. if (privileged)
  557. task_setup.callback_ops = &nfs41_call_priv_sync_ops;
  558. task = rpc_run_task(&task_setup);
  559. if (IS_ERR(task))
  560. ret = PTR_ERR(task);
  561. else {
  562. ret = task->tk_status;
  563. rpc_put_task(task);
  564. }
  565. return ret;
  566. }
  567. int _nfs4_call_sync_session(struct nfs_server *server,
  568. struct rpc_message *msg,
  569. struct nfs4_sequence_args *args,
  570. struct nfs4_sequence_res *res,
  571. int cache_reply)
  572. {
  573. return nfs4_call_sync_sequence(server->nfs_client, server->client,
  574. msg, args, res, cache_reply, 0);
  575. }
  576. #endif /* CONFIG_NFS_V4_1 */
  577. int _nfs4_call_sync(struct nfs_server *server,
  578. struct rpc_message *msg,
  579. struct nfs4_sequence_args *args,
  580. struct nfs4_sequence_res *res,
  581. int cache_reply)
  582. {
  583. args->sa_session = res->sr_session = NULL;
  584. return rpc_call_sync(server->client, msg, 0);
  585. }
  586. #define nfs4_call_sync(server, msg, args, res, cache_reply) \
  587. (server)->nfs_client->cl_call_sync((server), (msg), &(args)->seq_args, \
  588. &(res)->seq_res, (cache_reply))
  589. static void nfs4_sequence_done(const struct nfs_server *server,
  590. struct nfs4_sequence_res *res, int rpc_status)
  591. {
  592. #ifdef CONFIG_NFS_V4_1
  593. if (nfs4_has_session(server->nfs_client))
  594. nfs41_sequence_done(server->nfs_client, res, rpc_status);
  595. #endif /* CONFIG_NFS_V4_1 */
  596. }
  597. static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
  598. {
  599. struct nfs_inode *nfsi = NFS_I(dir);
  600. spin_lock(&dir->i_lock);
  601. nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
  602. if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
  603. nfs_force_lookup_revalidate(dir);
  604. nfsi->change_attr = cinfo->after;
  605. spin_unlock(&dir->i_lock);
  606. }
  607. struct nfs4_opendata {
  608. struct kref kref;
  609. struct nfs_openargs o_arg;
  610. struct nfs_openres o_res;
  611. struct nfs_open_confirmargs c_arg;
  612. struct nfs_open_confirmres c_res;
  613. struct nfs_fattr f_attr;
  614. struct nfs_fattr dir_attr;
  615. struct path path;
  616. struct dentry *dir;
  617. struct nfs4_state_owner *owner;
  618. struct nfs4_state *state;
  619. struct iattr attrs;
  620. unsigned long timestamp;
  621. unsigned int rpc_done : 1;
  622. int rpc_status;
  623. int cancelled;
  624. };
  625. static void nfs4_init_opendata_res(struct nfs4_opendata *p)
  626. {
  627. p->o_res.f_attr = &p->f_attr;
  628. p->o_res.dir_attr = &p->dir_attr;
  629. p->o_res.seqid = p->o_arg.seqid;
  630. p->c_res.seqid = p->c_arg.seqid;
  631. p->o_res.server = p->o_arg.server;
  632. nfs_fattr_init(&p->f_attr);
  633. nfs_fattr_init(&p->dir_attr);
  634. p->o_res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
  635. }
  636. static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
  637. struct nfs4_state_owner *sp, fmode_t fmode, int flags,
  638. const struct iattr *attrs)
  639. {
  640. struct dentry *parent = dget_parent(path->dentry);
  641. struct inode *dir = parent->d_inode;
  642. struct nfs_server *server = NFS_SERVER(dir);
  643. struct nfs4_opendata *p;
  644. p = kzalloc(sizeof(*p), GFP_KERNEL);
  645. if (p == NULL)
  646. goto err;
  647. p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
  648. if (p->o_arg.seqid == NULL)
  649. goto err_free;
  650. path_get(path);
  651. p->path = *path;
  652. p->dir = parent;
  653. p->owner = sp;
  654. atomic_inc(&sp->so_count);
  655. p->o_arg.fh = NFS_FH(dir);
  656. p->o_arg.open_flags = flags;
  657. p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
  658. p->o_arg.clientid = server->nfs_client->cl_clientid;
  659. p->o_arg.id = sp->so_owner_id.id;
  660. p->o_arg.name = &p->path.dentry->d_name;
  661. p->o_arg.server = server;
  662. p->o_arg.bitmask = server->attr_bitmask;
  663. p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
  664. if (flags & O_EXCL) {
  665. if (nfs4_has_persistent_session(server->nfs_client)) {
  666. /* GUARDED */
  667. p->o_arg.u.attrs = &p->attrs;
  668. memcpy(&p->attrs, attrs, sizeof(p->attrs));
  669. } else { /* EXCLUSIVE4_1 */
  670. u32 *s = (u32 *) p->o_arg.u.verifier.data;
  671. s[0] = jiffies;
  672. s[1] = current->pid;
  673. }
  674. } else if (flags & O_CREAT) {
  675. p->o_arg.u.attrs = &p->attrs;
  676. memcpy(&p->attrs, attrs, sizeof(p->attrs));
  677. }
  678. p->c_arg.fh = &p->o_res.fh;
  679. p->c_arg.stateid = &p->o_res.stateid;
  680. p->c_arg.seqid = p->o_arg.seqid;
  681. nfs4_init_opendata_res(p);
  682. kref_init(&p->kref);
  683. return p;
  684. err_free:
  685. kfree(p);
  686. err:
  687. dput(parent);
  688. return NULL;
  689. }
  690. static void nfs4_opendata_free(struct kref *kref)
  691. {
  692. struct nfs4_opendata *p = container_of(kref,
  693. struct nfs4_opendata, kref);
  694. nfs_free_seqid(p->o_arg.seqid);
  695. if (p->state != NULL)
  696. nfs4_put_open_state(p->state);
  697. nfs4_put_state_owner(p->owner);
  698. dput(p->dir);
  699. path_put(&p->path);
  700. kfree(p);
  701. }
  702. static void nfs4_opendata_put(struct nfs4_opendata *p)
  703. {
  704. if (p != NULL)
  705. kref_put(&p->kref, nfs4_opendata_free);
  706. }
  707. static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
  708. {
  709. int ret;
  710. ret = rpc_wait_for_completion_task(task);
  711. return ret;
  712. }
  713. static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
  714. {
  715. int ret = 0;
  716. if (open_mode & O_EXCL)
  717. goto out;
  718. switch (mode & (FMODE_READ|FMODE_WRITE)) {
  719. case FMODE_READ:
  720. ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
  721. && state->n_rdonly != 0;
  722. break;
  723. case FMODE_WRITE:
  724. ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
  725. && state->n_wronly != 0;
  726. break;
  727. case FMODE_READ|FMODE_WRITE:
  728. ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
  729. && state->n_rdwr != 0;
  730. }
  731. out:
  732. return ret;
  733. }
  734. static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
  735. {
  736. if ((delegation->type & fmode) != fmode)
  737. return 0;
  738. if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
  739. return 0;
  740. nfs_mark_delegation_referenced(delegation);
  741. return 1;
  742. }
  743. static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
  744. {
  745. switch (fmode) {
  746. case FMODE_WRITE:
  747. state->n_wronly++;
  748. break;
  749. case FMODE_READ:
  750. state->n_rdonly++;
  751. break;
  752. case FMODE_READ|FMODE_WRITE:
  753. state->n_rdwr++;
  754. }
  755. nfs4_state_set_mode_locked(state, state->state | fmode);
  756. }
  757. static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
  758. {
  759. if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
  760. memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
  761. memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
  762. switch (fmode) {
  763. case FMODE_READ:
  764. set_bit(NFS_O_RDONLY_STATE, &state->flags);
  765. break;
  766. case FMODE_WRITE:
  767. set_bit(NFS_O_WRONLY_STATE, &state->flags);
  768. break;
  769. case FMODE_READ|FMODE_WRITE:
  770. set_bit(NFS_O_RDWR_STATE, &state->flags);
  771. }
  772. }
  773. static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
  774. {
  775. write_seqlock(&state->seqlock);
  776. nfs_set_open_stateid_locked(state, stateid, fmode);
  777. write_sequnlock(&state->seqlock);
  778. }
  779. static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
  780. {
  781. /*
  782. * Protect the call to nfs4_state_set_mode_locked and
  783. * serialise the stateid update
  784. */
  785. write_seqlock(&state->seqlock);
  786. if (deleg_stateid != NULL) {
  787. memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
  788. set_bit(NFS_DELEGATED_STATE, &state->flags);
  789. }
  790. if (open_stateid != NULL)
  791. nfs_set_open_stateid_locked(state, open_stateid, fmode);
  792. write_sequnlock(&state->seqlock);
  793. spin_lock(&state->owner->so_lock);
  794. update_open_stateflags(state, fmode);
  795. spin_unlock(&state->owner->so_lock);
  796. }
  797. static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
  798. {
  799. struct nfs_inode *nfsi = NFS_I(state->inode);
  800. struct nfs_delegation *deleg_cur;
  801. int ret = 0;
  802. fmode &= (FMODE_READ|FMODE_WRITE);
  803. rcu_read_lock();
  804. deleg_cur = rcu_dereference(nfsi->delegation);
  805. if (deleg_cur == NULL)
  806. goto no_delegation;
  807. spin_lock(&deleg_cur->lock);
  808. if (nfsi->delegation != deleg_cur ||
  809. (deleg_cur->type & fmode) != fmode)
  810. goto no_delegation_unlock;
  811. if (delegation == NULL)
  812. delegation = &deleg_cur->stateid;
  813. else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
  814. goto no_delegation_unlock;
  815. nfs_mark_delegation_referenced(deleg_cur);
  816. __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
  817. ret = 1;
  818. no_delegation_unlock:
  819. spin_unlock(&deleg_cur->lock);
  820. no_delegation:
  821. rcu_read_unlock();
  822. if (!ret && open_stateid != NULL) {
  823. __update_open_stateid(state, open_stateid, NULL, fmode);
  824. ret = 1;
  825. }
  826. return ret;
  827. }
  828. static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
  829. {
  830. struct nfs_delegation *delegation;
  831. rcu_read_lock();
  832. delegation = rcu_dereference(NFS_I(inode)->delegation);
  833. if (delegation == NULL || (delegation->type & fmode) == fmode) {
  834. rcu_read_unlock();
  835. return;
  836. }
  837. rcu_read_unlock();
  838. nfs_inode_return_delegation(inode);
  839. }
  840. static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
  841. {
  842. struct nfs4_state *state = opendata->state;
  843. struct nfs_inode *nfsi = NFS_I(state->inode);
  844. struct nfs_delegation *delegation;
  845. int open_mode = opendata->o_arg.open_flags & O_EXCL;
  846. fmode_t fmode = opendata->o_arg.fmode;
  847. nfs4_stateid stateid;
  848. int ret = -EAGAIN;
  849. for (;;) {
  850. if (can_open_cached(state, fmode, open_mode)) {
  851. spin_lock(&state->owner->so_lock);
  852. if (can_open_cached(state, fmode, open_mode)) {
  853. update_open_stateflags(state, fmode);
  854. spin_unlock(&state->owner->so_lock);
  855. goto out_return_state;
  856. }
  857. spin_unlock(&state->owner->so_lock);
  858. }
  859. rcu_read_lock();
  860. delegation = rcu_dereference(nfsi->delegation);
  861. if (delegation == NULL ||
  862. !can_open_delegated(delegation, fmode)) {
  863. rcu_read_unlock();
  864. break;
  865. }
  866. /* Save the delegation */
  867. memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
  868. rcu_read_unlock();
  869. ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
  870. if (ret != 0)
  871. goto out;
  872. ret = -EAGAIN;
  873. /* Try to update the stateid using the delegation */
  874. if (update_open_stateid(state, NULL, &stateid, fmode))
  875. goto out_return_state;
  876. }
  877. out:
  878. return ERR_PTR(ret);
  879. out_return_state:
  880. atomic_inc(&state->count);
  881. return state;
  882. }
  883. static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
  884. {
  885. struct inode *inode;
  886. struct nfs4_state *state = NULL;
  887. struct nfs_delegation *delegation;
  888. int ret;
  889. if (!data->rpc_done) {
  890. state = nfs4_try_open_cached(data);
  891. goto out;
  892. }
  893. ret = -EAGAIN;
  894. if (!(data->f_attr.valid & NFS_ATTR_FATTR))
  895. goto err;
  896. inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
  897. ret = PTR_ERR(inode);
  898. if (IS_ERR(inode))
  899. goto err;
  900. ret = -ENOMEM;
  901. state = nfs4_get_open_state(inode, data->owner);
  902. if (state == NULL)
  903. goto err_put_inode;
  904. if (data->o_res.delegation_type != 0) {
  905. int delegation_flags = 0;
  906. rcu_read_lock();
  907. delegation = rcu_dereference(NFS_I(inode)->delegation);
  908. if (delegation)
  909. delegation_flags = delegation->flags;
  910. rcu_read_unlock();
  911. if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
  912. nfs_inode_set_delegation(state->inode,
  913. data->owner->so_cred,
  914. &data->o_res);
  915. else
  916. nfs_inode_reclaim_delegation(state->inode,
  917. data->owner->so_cred,
  918. &data->o_res);
  919. }
  920. update_open_stateid(state, &data->o_res.stateid, NULL,
  921. data->o_arg.fmode);
  922. iput(inode);
  923. out:
  924. return state;
  925. err_put_inode:
  926. iput(inode);
  927. err:
  928. return ERR_PTR(ret);
  929. }
  930. static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
  931. {
  932. struct nfs_inode *nfsi = NFS_I(state->inode);
  933. struct nfs_open_context *ctx;
  934. spin_lock(&state->inode->i_lock);
  935. list_for_each_entry(ctx, &nfsi->open_files, list) {
  936. if (ctx->state != state)
  937. continue;
  938. get_nfs_open_context(ctx);
  939. spin_unlock(&state->inode->i_lock);
  940. return ctx;
  941. }
  942. spin_unlock(&state->inode->i_lock);
  943. return ERR_PTR(-ENOENT);
  944. }
  945. static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
  946. {
  947. struct nfs4_opendata *opendata;
  948. opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL);
  949. if (opendata == NULL)
  950. return ERR_PTR(-ENOMEM);
  951. opendata->state = state;
  952. atomic_inc(&state->count);
  953. return opendata;
  954. }
  955. static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
  956. {
  957. struct nfs4_state *newstate;
  958. int ret;
  959. opendata->o_arg.open_flags = 0;
  960. opendata->o_arg.fmode = fmode;
  961. memset(&opendata->o_res, 0, sizeof(opendata->o_res));
  962. memset(&opendata->c_res, 0, sizeof(opendata->c_res));
  963. nfs4_init_opendata_res(opendata);
  964. ret = _nfs4_recover_proc_open(opendata);
  965. if (ret != 0)
  966. return ret;
  967. newstate = nfs4_opendata_to_nfs4_state(opendata);
  968. if (IS_ERR(newstate))
  969. return PTR_ERR(newstate);
  970. nfs4_close_state(&opendata->path, newstate, fmode);
  971. *res = newstate;
  972. return 0;
  973. }
  974. static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
  975. {
  976. struct nfs4_state *newstate;
  977. int ret;
  978. /* memory barrier prior to reading state->n_* */
  979. clear_bit(NFS_DELEGATED_STATE, &state->flags);
  980. smp_rmb();
  981. if (state->n_rdwr != 0) {
  982. ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
  983. if (ret != 0)
  984. return ret;
  985. if (newstate != state)
  986. return -ESTALE;
  987. }
  988. if (state->n_wronly != 0) {
  989. ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
  990. if (ret != 0)
  991. return ret;
  992. if (newstate != state)
  993. return -ESTALE;
  994. }
  995. if (state->n_rdonly != 0) {
  996. ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
  997. if (ret != 0)
  998. return ret;
  999. if (newstate != state)
  1000. return -ESTALE;
  1001. }
  1002. /*
  1003. * We may have performed cached opens for all three recoveries.
  1004. * Check if we need to update the current stateid.
  1005. */
  1006. if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
  1007. memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
  1008. write_seqlock(&state->seqlock);
  1009. if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
  1010. memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
  1011. write_sequnlock(&state->seqlock);
  1012. }
  1013. return 0;
  1014. }
  1015. /*
  1016. * OPEN_RECLAIM:
  1017. * reclaim state on the server after a reboot.
  1018. */
  1019. static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
  1020. {
  1021. struct nfs_delegation *delegation;
  1022. struct nfs4_opendata *opendata;
  1023. fmode_t delegation_type = 0;
  1024. int status;
  1025. opendata = nfs4_open_recoverdata_alloc(ctx, state);
  1026. if (IS_ERR(opendata))
  1027. return PTR_ERR(opendata);
  1028. opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
  1029. opendata->o_arg.fh = NFS_FH(state->inode);
  1030. rcu_read_lock();
  1031. delegation = rcu_dereference(NFS_I(state->inode)->delegation);
  1032. if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
  1033. delegation_type = delegation->type;
  1034. rcu_read_unlock();
  1035. opendata->o_arg.u.delegation_type = delegation_type;
  1036. status = nfs4_open_recover(opendata, state);
  1037. nfs4_opendata_put(opendata);
  1038. return status;
  1039. }
  1040. static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
  1041. {
  1042. struct nfs_server *server = NFS_SERVER(state->inode);
  1043. struct nfs4_exception exception = { };
  1044. int err;
  1045. do {
  1046. err = _nfs4_do_open_reclaim(ctx, state);
  1047. if (err != -NFS4ERR_DELAY && err != -EKEYEXPIRED)
  1048. break;
  1049. nfs4_handle_exception(server, err, &exception);
  1050. } while (exception.retry);
  1051. return err;
  1052. }
  1053. static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
  1054. {
  1055. struct nfs_open_context *ctx;
  1056. int ret;
  1057. ctx = nfs4_state_find_open_context(state);
  1058. if (IS_ERR(ctx))
  1059. return PTR_ERR(ctx);
  1060. ret = nfs4_do_open_reclaim(ctx, state);
  1061. put_nfs_open_context(ctx);
  1062. return ret;
  1063. }
  1064. static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
  1065. {
  1066. struct nfs4_opendata *opendata;
  1067. int ret;
  1068. opendata = nfs4_open_recoverdata_alloc(ctx, state);
  1069. if (IS_ERR(opendata))
  1070. return PTR_ERR(opendata);
  1071. opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
  1072. memcpy(opendata->o_arg.u.delegation.data, stateid->data,
  1073. sizeof(opendata->o_arg.u.delegation.data));
  1074. ret = nfs4_open_recover(opendata, state);
  1075. nfs4_opendata_put(opendata);
  1076. return ret;
  1077. }
  1078. int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
  1079. {
  1080. struct nfs4_exception exception = { };
  1081. struct nfs_server *server = NFS_SERVER(state->inode);
  1082. int err;
  1083. do {
  1084. err = _nfs4_open_delegation_recall(ctx, state, stateid);
  1085. switch (err) {
  1086. case 0:
  1087. case -ENOENT:
  1088. case -ESTALE:
  1089. goto out;
  1090. case -NFS4ERR_BADSESSION:
  1091. case -NFS4ERR_BADSLOT:
  1092. case -NFS4ERR_BAD_HIGH_SLOT:
  1093. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  1094. case -NFS4ERR_DEADSESSION:
  1095. nfs4_schedule_state_recovery(
  1096. server->nfs_client);
  1097. goto out;
  1098. case -NFS4ERR_STALE_CLIENTID:
  1099. case -NFS4ERR_STALE_STATEID:
  1100. case -NFS4ERR_EXPIRED:
  1101. /* Don't recall a delegation if it was lost */
  1102. nfs4_schedule_state_recovery(server->nfs_client);
  1103. goto out;
  1104. case -ERESTARTSYS:
  1105. /*
  1106. * The show must go on: exit, but mark the
  1107. * stateid as needing recovery.
  1108. */
  1109. case -NFS4ERR_ADMIN_REVOKED:
  1110. case -NFS4ERR_BAD_STATEID:
  1111. nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
  1112. case -ENOMEM:
  1113. err = 0;
  1114. goto out;
  1115. }
  1116. err = nfs4_handle_exception(server, err, &exception);
  1117. } while (exception.retry);
  1118. out:
  1119. return err;
  1120. }
  1121. static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
  1122. {
  1123. struct nfs4_opendata *data = calldata;
  1124. data->rpc_status = task->tk_status;
  1125. if (RPC_ASSASSINATED(task))
  1126. return;
  1127. if (data->rpc_status == 0) {
  1128. memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
  1129. sizeof(data->o_res.stateid.data));
  1130. nfs_confirm_seqid(&data->owner->so_seqid, 0);
  1131. renew_lease(data->o_res.server, data->timestamp);
  1132. data->rpc_done = 1;
  1133. }
  1134. }
  1135. static void nfs4_open_confirm_release(void *calldata)
  1136. {
  1137. struct nfs4_opendata *data = calldata;
  1138. struct nfs4_state *state = NULL;
  1139. /* If this request hasn't been cancelled, do nothing */
  1140. if (data->cancelled == 0)
  1141. goto out_free;
  1142. /* In case of error, no cleanup! */
  1143. if (!data->rpc_done)
  1144. goto out_free;
  1145. state = nfs4_opendata_to_nfs4_state(data);
  1146. if (!IS_ERR(state))
  1147. nfs4_close_state(&data->path, state, data->o_arg.fmode);
  1148. out_free:
  1149. nfs4_opendata_put(data);
  1150. }
  1151. static const struct rpc_call_ops nfs4_open_confirm_ops = {
  1152. .rpc_call_done = nfs4_open_confirm_done,
  1153. .rpc_release = nfs4_open_confirm_release,
  1154. };
  1155. /*
  1156. * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
  1157. */
  1158. static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
  1159. {
  1160. struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
  1161. struct rpc_task *task;
  1162. struct rpc_message msg = {
  1163. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
  1164. .rpc_argp = &data->c_arg,
  1165. .rpc_resp = &data->c_res,
  1166. .rpc_cred = data->owner->so_cred,
  1167. };
  1168. struct rpc_task_setup task_setup_data = {
  1169. .rpc_client = server->client,
  1170. .rpc_message = &msg,
  1171. .callback_ops = &nfs4_open_confirm_ops,
  1172. .callback_data = data,
  1173. .workqueue = nfsiod_workqueue,
  1174. .flags = RPC_TASK_ASYNC,
  1175. };
  1176. int status;
  1177. kref_get(&data->kref);
  1178. data->rpc_done = 0;
  1179. data->rpc_status = 0;
  1180. data->timestamp = jiffies;
  1181. task = rpc_run_task(&task_setup_data);
  1182. if (IS_ERR(task))
  1183. return PTR_ERR(task);
  1184. status = nfs4_wait_for_completion_rpc_task(task);
  1185. if (status != 0) {
  1186. data->cancelled = 1;
  1187. smp_wmb();
  1188. } else
  1189. status = data->rpc_status;
  1190. rpc_put_task(task);
  1191. return status;
  1192. }
  1193. static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
  1194. {
  1195. struct nfs4_opendata *data = calldata;
  1196. struct nfs4_state_owner *sp = data->owner;
  1197. if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
  1198. return;
  1199. /*
  1200. * Check if we still need to send an OPEN call, or if we can use
  1201. * a delegation instead.
  1202. */
  1203. if (data->state != NULL) {
  1204. struct nfs_delegation *delegation;
  1205. if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
  1206. goto out_no_action;
  1207. rcu_read_lock();
  1208. delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
  1209. if (delegation != NULL &&
  1210. test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
  1211. rcu_read_unlock();
  1212. goto out_no_action;
  1213. }
  1214. rcu_read_unlock();
  1215. }
  1216. /* Update sequence id. */
  1217. data->o_arg.id = sp->so_owner_id.id;
  1218. data->o_arg.clientid = sp->so_client->cl_clientid;
  1219. if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
  1220. task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
  1221. nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
  1222. }
  1223. data->timestamp = jiffies;
  1224. if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
  1225. &data->o_arg.seq_args,
  1226. &data->o_res.seq_res, 1, task))
  1227. return;
  1228. rpc_call_start(task);
  1229. return;
  1230. out_no_action:
  1231. task->tk_action = NULL;
  1232. }
  1233. static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
  1234. {
  1235. rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
  1236. nfs4_open_prepare(task, calldata);
  1237. }
  1238. static void nfs4_open_done(struct rpc_task *task, void *calldata)
  1239. {
  1240. struct nfs4_opendata *data = calldata;
  1241. data->rpc_status = task->tk_status;
  1242. nfs4_sequence_done(data->o_arg.server, &data->o_res.seq_res,
  1243. task->tk_status);
  1244. if (RPC_ASSASSINATED(task))
  1245. return;
  1246. if (task->tk_status == 0) {
  1247. switch (data->o_res.f_attr->mode & S_IFMT) {
  1248. case S_IFREG:
  1249. break;
  1250. case S_IFLNK:
  1251. data->rpc_status = -ELOOP;
  1252. break;
  1253. case S_IFDIR:
  1254. data->rpc_status = -EISDIR;
  1255. break;
  1256. default:
  1257. data->rpc_status = -ENOTDIR;
  1258. }
  1259. renew_lease(data->o_res.server, data->timestamp);
  1260. if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
  1261. nfs_confirm_seqid(&data->owner->so_seqid, 0);
  1262. }
  1263. data->rpc_done = 1;
  1264. }
  1265. static void nfs4_open_release(void *calldata)
  1266. {
  1267. struct nfs4_opendata *data = calldata;
  1268. struct nfs4_state *state = NULL;
  1269. /* If this request hasn't been cancelled, do nothing */
  1270. if (data->cancelled == 0)
  1271. goto out_free;
  1272. /* In case of error, no cleanup! */
  1273. if (data->rpc_status != 0 || !data->rpc_done)
  1274. goto out_free;
  1275. /* In case we need an open_confirm, no cleanup! */
  1276. if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
  1277. goto out_free;
  1278. state = nfs4_opendata_to_nfs4_state(data);
  1279. if (!IS_ERR(state))
  1280. nfs4_close_state(&data->path, state, data->o_arg.fmode);
  1281. out_free:
  1282. nfs4_opendata_put(data);
  1283. }
  1284. static const struct rpc_call_ops nfs4_open_ops = {
  1285. .rpc_call_prepare = nfs4_open_prepare,
  1286. .rpc_call_done = nfs4_open_done,
  1287. .rpc_release = nfs4_open_release,
  1288. };
  1289. static const struct rpc_call_ops nfs4_recover_open_ops = {
  1290. .rpc_call_prepare = nfs4_recover_open_prepare,
  1291. .rpc_call_done = nfs4_open_done,
  1292. .rpc_release = nfs4_open_release,
  1293. };
  1294. static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
  1295. {
  1296. struct inode *dir = data->dir->d_inode;
  1297. struct nfs_server *server = NFS_SERVER(dir);
  1298. struct nfs_openargs *o_arg = &data->o_arg;
  1299. struct nfs_openres *o_res = &data->o_res;
  1300. struct rpc_task *task;
  1301. struct rpc_message msg = {
  1302. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
  1303. .rpc_argp = o_arg,
  1304. .rpc_resp = o_res,
  1305. .rpc_cred = data->owner->so_cred,
  1306. };
  1307. struct rpc_task_setup task_setup_data = {
  1308. .rpc_client = server->client,
  1309. .rpc_message = &msg,
  1310. .callback_ops = &nfs4_open_ops,
  1311. .callback_data = data,
  1312. .workqueue = nfsiod_workqueue,
  1313. .flags = RPC_TASK_ASYNC,
  1314. };
  1315. int status;
  1316. kref_get(&data->kref);
  1317. data->rpc_done = 0;
  1318. data->rpc_status = 0;
  1319. data->cancelled = 0;
  1320. if (isrecover)
  1321. task_setup_data.callback_ops = &nfs4_recover_open_ops;
  1322. task = rpc_run_task(&task_setup_data);
  1323. if (IS_ERR(task))
  1324. return PTR_ERR(task);
  1325. status = nfs4_wait_for_completion_rpc_task(task);
  1326. if (status != 0) {
  1327. data->cancelled = 1;
  1328. smp_wmb();
  1329. } else
  1330. status = data->rpc_status;
  1331. rpc_put_task(task);
  1332. return status;
  1333. }
  1334. static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
  1335. {
  1336. struct inode *dir = data->dir->d_inode;
  1337. struct nfs_openres *o_res = &data->o_res;
  1338. int status;
  1339. status = nfs4_run_open_task(data, 1);
  1340. if (status != 0 || !data->rpc_done)
  1341. return status;
  1342. nfs_refresh_inode(dir, o_res->dir_attr);
  1343. if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
  1344. status = _nfs4_proc_open_confirm(data);
  1345. if (status != 0)
  1346. return status;
  1347. }
  1348. return status;
  1349. }
  1350. /*
  1351. * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
  1352. */
  1353. static int _nfs4_proc_open(struct nfs4_opendata *data)
  1354. {
  1355. struct inode *dir = data->dir->d_inode;
  1356. struct nfs_server *server = NFS_SERVER(dir);
  1357. struct nfs_openargs *o_arg = &data->o_arg;
  1358. struct nfs_openres *o_res = &data->o_res;
  1359. int status;
  1360. status = nfs4_run_open_task(data, 0);
  1361. if (status != 0 || !data->rpc_done)
  1362. return status;
  1363. if (o_arg->open_flags & O_CREAT) {
  1364. update_changeattr(dir, &o_res->cinfo);
  1365. nfs_post_op_update_inode(dir, o_res->dir_attr);
  1366. } else
  1367. nfs_refresh_inode(dir, o_res->dir_attr);
  1368. if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
  1369. server->caps &= ~NFS_CAP_POSIX_LOCK;
  1370. if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
  1371. status = _nfs4_proc_open_confirm(data);
  1372. if (status != 0)
  1373. return status;
  1374. }
  1375. if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
  1376. _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
  1377. return 0;
  1378. }
  1379. static int nfs4_recover_expired_lease(struct nfs_server *server)
  1380. {
  1381. struct nfs_client *clp = server->nfs_client;
  1382. unsigned int loop;
  1383. int ret;
  1384. for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
  1385. ret = nfs4_wait_clnt_recover(clp);
  1386. if (ret != 0)
  1387. break;
  1388. if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
  1389. !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
  1390. break;
  1391. nfs4_schedule_state_recovery(clp);
  1392. ret = -EIO;
  1393. }
  1394. return ret;
  1395. }
  1396. /*
  1397. * OPEN_EXPIRED:
  1398. * reclaim state on the server after a network partition.
  1399. * Assumes caller holds the appropriate lock
  1400. */
  1401. static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
  1402. {
  1403. struct nfs4_opendata *opendata;
  1404. int ret;
  1405. opendata = nfs4_open_recoverdata_alloc(ctx, state);
  1406. if (IS_ERR(opendata))
  1407. return PTR_ERR(opendata);
  1408. ret = nfs4_open_recover(opendata, state);
  1409. if (ret == -ESTALE)
  1410. d_drop(ctx->path.dentry);
  1411. nfs4_opendata_put(opendata);
  1412. return ret;
  1413. }
  1414. static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
  1415. {
  1416. struct nfs_server *server = NFS_SERVER(state->inode);
  1417. struct nfs4_exception exception = { };
  1418. int err;
  1419. do {
  1420. err = _nfs4_open_expired(ctx, state);
  1421. switch (err) {
  1422. default:
  1423. goto out;
  1424. case -NFS4ERR_GRACE:
  1425. case -NFS4ERR_DELAY:
  1426. case -EKEYEXPIRED:
  1427. nfs4_handle_exception(server, err, &exception);
  1428. err = 0;
  1429. }
  1430. } while (exception.retry);
  1431. out:
  1432. return err;
  1433. }
  1434. static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
  1435. {
  1436. struct nfs_open_context *ctx;
  1437. int ret;
  1438. ctx = nfs4_state_find_open_context(state);
  1439. if (IS_ERR(ctx))
  1440. return PTR_ERR(ctx);
  1441. ret = nfs4_do_open_expired(ctx, state);
  1442. put_nfs_open_context(ctx);
  1443. return ret;
  1444. }
  1445. /*
  1446. * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
  1447. * fields corresponding to attributes that were used to store the verifier.
  1448. * Make sure we clobber those fields in the later setattr call
  1449. */
  1450. static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
  1451. {
  1452. if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
  1453. !(sattr->ia_valid & ATTR_ATIME_SET))
  1454. sattr->ia_valid |= ATTR_ATIME;
  1455. if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
  1456. !(sattr->ia_valid & ATTR_MTIME_SET))
  1457. sattr->ia_valid |= ATTR_MTIME;
  1458. }
  1459. /*
  1460. * Returns a referenced nfs4_state
  1461. */
  1462. static int _nfs4_do_open(struct inode *dir, struct path *path, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
  1463. {
  1464. struct nfs4_state_owner *sp;
  1465. struct nfs4_state *state = NULL;
  1466. struct nfs_server *server = NFS_SERVER(dir);
  1467. struct nfs4_opendata *opendata;
  1468. int status;
  1469. /* Protect against reboot recovery conflicts */
  1470. status = -ENOMEM;
  1471. if (!(sp = nfs4_get_state_owner(server, cred))) {
  1472. dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
  1473. goto out_err;
  1474. }
  1475. status = nfs4_recover_expired_lease(server);
  1476. if (status != 0)
  1477. goto err_put_state_owner;
  1478. if (path->dentry->d_inode != NULL)
  1479. nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
  1480. status = -ENOMEM;
  1481. opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr);
  1482. if (opendata == NULL)
  1483. goto err_put_state_owner;
  1484. if (path->dentry->d_inode != NULL)
  1485. opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
  1486. status = _nfs4_proc_open(opendata);
  1487. if (status != 0)
  1488. goto err_opendata_put;
  1489. if (opendata->o_arg.open_flags & O_EXCL)
  1490. nfs4_exclusive_attrset(opendata, sattr);
  1491. state = nfs4_opendata_to_nfs4_state(opendata);
  1492. status = PTR_ERR(state);
  1493. if (IS_ERR(state))
  1494. goto err_opendata_put;
  1495. if (server->caps & NFS_CAP_POSIX_LOCK)
  1496. set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
  1497. nfs4_opendata_put(opendata);
  1498. nfs4_put_state_owner(sp);
  1499. *res = state;
  1500. return 0;
  1501. err_opendata_put:
  1502. nfs4_opendata_put(opendata);
  1503. err_put_state_owner:
  1504. nfs4_put_state_owner(sp);
  1505. out_err:
  1506. *res = NULL;
  1507. return status;
  1508. }
  1509. static struct nfs4_state *nfs4_do_open(struct inode *dir, struct path *path, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred)
  1510. {
  1511. struct nfs4_exception exception = { };
  1512. struct nfs4_state *res;
  1513. int status;
  1514. do {
  1515. status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
  1516. if (status == 0)
  1517. break;
  1518. /* NOTE: BAD_SEQID means the server and client disagree about the
  1519. * book-keeping w.r.t. state-changing operations
  1520. * (OPEN/CLOSE/LOCK/LOCKU...)
  1521. * It is actually a sign of a bug on the client or on the server.
  1522. *
  1523. * If we receive a BAD_SEQID error in the particular case of
  1524. * doing an OPEN, we assume that nfs_increment_open_seqid() will
  1525. * have unhashed the old state_owner for us, and that we can
  1526. * therefore safely retry using a new one. We should still warn
  1527. * the user though...
  1528. */
  1529. if (status == -NFS4ERR_BAD_SEQID) {
  1530. printk(KERN_WARNING "NFS: v4 server %s "
  1531. " returned a bad sequence-id error!\n",
  1532. NFS_SERVER(dir)->nfs_client->cl_hostname);
  1533. exception.retry = 1;
  1534. continue;
  1535. }
  1536. /*
  1537. * BAD_STATEID on OPEN means that the server cancelled our
  1538. * state before it received the OPEN_CONFIRM.
  1539. * Recover by retrying the request as per the discussion
  1540. * on Page 181 of RFC3530.
  1541. */
  1542. if (status == -NFS4ERR_BAD_STATEID) {
  1543. exception.retry = 1;
  1544. continue;
  1545. }
  1546. if (status == -EAGAIN) {
  1547. /* We must have found a delegation */
  1548. exception.retry = 1;
  1549. continue;
  1550. }
  1551. res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
  1552. status, &exception));
  1553. } while (exception.retry);
  1554. return res;
  1555. }
  1556. static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
  1557. struct nfs_fattr *fattr, struct iattr *sattr,
  1558. struct nfs4_state *state)
  1559. {
  1560. struct nfs_server *server = NFS_SERVER(inode);
  1561. struct nfs_setattrargs arg = {
  1562. .fh = NFS_FH(inode),
  1563. .iap = sattr,
  1564. .server = server,
  1565. .bitmask = server->attr_bitmask,
  1566. };
  1567. struct nfs_setattrres res = {
  1568. .fattr = fattr,
  1569. .server = server,
  1570. };
  1571. struct rpc_message msg = {
  1572. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
  1573. .rpc_argp = &arg,
  1574. .rpc_resp = &res,
  1575. .rpc_cred = cred,
  1576. };
  1577. unsigned long timestamp = jiffies;
  1578. int status;
  1579. nfs_fattr_init(fattr);
  1580. if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
  1581. /* Use that stateid */
  1582. } else if (state != NULL) {
  1583. nfs4_copy_stateid(&arg.stateid, state, current->files);
  1584. } else
  1585. memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
  1586. status = nfs4_call_sync(server, &msg, &arg, &res, 1);
  1587. if (status == 0 && state != NULL)
  1588. renew_lease(server, timestamp);
  1589. return status;
  1590. }
  1591. static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
  1592. struct nfs_fattr *fattr, struct iattr *sattr,
  1593. struct nfs4_state *state)
  1594. {
  1595. struct nfs_server *server = NFS_SERVER(inode);
  1596. struct nfs4_exception exception = { };
  1597. int err;
  1598. do {
  1599. err = nfs4_handle_exception(server,
  1600. _nfs4_do_setattr(inode, cred, fattr, sattr, state),
  1601. &exception);
  1602. } while (exception.retry);
  1603. return err;
  1604. }
  1605. struct nfs4_closedata {
  1606. struct path path;
  1607. struct inode *inode;
  1608. struct nfs4_state *state;
  1609. struct nfs_closeargs arg;
  1610. struct nfs_closeres res;
  1611. struct nfs_fattr fattr;
  1612. unsigned long timestamp;
  1613. };
  1614. static void nfs4_free_closedata(void *data)
  1615. {
  1616. struct nfs4_closedata *calldata = data;
  1617. struct nfs4_state_owner *sp = calldata->state->owner;
  1618. nfs4_put_open_state(calldata->state);
  1619. nfs_free_seqid(calldata->arg.seqid);
  1620. nfs4_put_state_owner(sp);
  1621. path_put(&calldata->path);
  1622. kfree(calldata);
  1623. }
  1624. static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
  1625. fmode_t fmode)
  1626. {
  1627. spin_lock(&state->owner->so_lock);
  1628. if (!(fmode & FMODE_READ))
  1629. clear_bit(NFS_O_RDONLY_STATE, &state->flags);
  1630. if (!(fmode & FMODE_WRITE))
  1631. clear_bit(NFS_O_WRONLY_STATE, &state->flags);
  1632. clear_bit(NFS_O_RDWR_STATE, &state->flags);
  1633. spin_unlock(&state->owner->so_lock);
  1634. }
  1635. static void nfs4_close_done(struct rpc_task *task, void *data)
  1636. {
  1637. struct nfs4_closedata *calldata = data;
  1638. struct nfs4_state *state = calldata->state;
  1639. struct nfs_server *server = NFS_SERVER(calldata->inode);
  1640. nfs4_sequence_done(server, &calldata->res.seq_res, task->tk_status);
  1641. if (RPC_ASSASSINATED(task))
  1642. return;
  1643. /* hmm. we are done with the inode, and in the process of freeing
  1644. * the state_owner. we keep this around to process errors
  1645. */
  1646. switch (task->tk_status) {
  1647. case 0:
  1648. nfs_set_open_stateid(state, &calldata->res.stateid, 0);
  1649. renew_lease(server, calldata->timestamp);
  1650. nfs4_close_clear_stateid_flags(state,
  1651. calldata->arg.fmode);
  1652. break;
  1653. case -NFS4ERR_STALE_STATEID:
  1654. case -NFS4ERR_OLD_STATEID:
  1655. case -NFS4ERR_BAD_STATEID:
  1656. case -NFS4ERR_EXPIRED:
  1657. if (calldata->arg.fmode == 0)
  1658. break;
  1659. default:
  1660. if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
  1661. rpc_restart_call_prepare(task);
  1662. }
  1663. nfs_release_seqid(calldata->arg.seqid);
  1664. nfs_refresh_inode(calldata->inode, calldata->res.fattr);
  1665. }
  1666. static void nfs4_close_prepare(struct rpc_task *task, void *data)
  1667. {
  1668. struct nfs4_closedata *calldata = data;
  1669. struct nfs4_state *state = calldata->state;
  1670. int call_close = 0;
  1671. if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
  1672. return;
  1673. task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
  1674. calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
  1675. spin_lock(&state->owner->so_lock);
  1676. /* Calculate the change in open mode */
  1677. if (state->n_rdwr == 0) {
  1678. if (state->n_rdonly == 0) {
  1679. call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
  1680. call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
  1681. calldata->arg.fmode &= ~FMODE_READ;
  1682. }
  1683. if (state->n_wronly == 0) {
  1684. call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
  1685. call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
  1686. calldata->arg.fmode &= ~FMODE_WRITE;
  1687. }
  1688. }
  1689. spin_unlock(&state->owner->so_lock);
  1690. if (!call_close) {
  1691. /* Note: exit _without_ calling nfs4_close_done */
  1692. task->tk_action = NULL;
  1693. return;
  1694. }
  1695. if (calldata->arg.fmode == 0)
  1696. task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
  1697. nfs_fattr_init(calldata->res.fattr);
  1698. calldata->timestamp = jiffies;
  1699. if (nfs4_setup_sequence((NFS_SERVER(calldata->inode))->nfs_client,
  1700. &calldata->arg.seq_args, &calldata->res.seq_res,
  1701. 1, task))
  1702. return;
  1703. rpc_call_start(task);
  1704. }
  1705. static const struct rpc_call_ops nfs4_close_ops = {
  1706. .rpc_call_prepare = nfs4_close_prepare,
  1707. .rpc_call_done = nfs4_close_done,
  1708. .rpc_release = nfs4_free_closedata,
  1709. };
  1710. /*
  1711. * It is possible for data to be read/written from a mem-mapped file
  1712. * after the sys_close call (which hits the vfs layer as a flush).
  1713. * This means that we can't safely call nfsv4 close on a file until
  1714. * the inode is cleared. This in turn means that we are not good
  1715. * NFSv4 citizens - we do not indicate to the server to update the file's
  1716. * share state even when we are done with one of the three share
  1717. * stateid's in the inode.
  1718. *
  1719. * NOTE: Caller must be holding the sp->so_owner semaphore!
  1720. */
  1721. int nfs4_do_close(struct path *path, struct nfs4_state *state, int wait)
  1722. {
  1723. struct nfs_server *server = NFS_SERVER(state->inode);
  1724. struct nfs4_closedata *calldata;
  1725. struct nfs4_state_owner *sp = state->owner;
  1726. struct rpc_task *task;
  1727. struct rpc_message msg = {
  1728. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
  1729. .rpc_cred = state->owner->so_cred,
  1730. };
  1731. struct rpc_task_setup task_setup_data = {
  1732. .rpc_client = server->client,
  1733. .rpc_message = &msg,
  1734. .callback_ops = &nfs4_close_ops,
  1735. .workqueue = nfsiod_workqueue,
  1736. .flags = RPC_TASK_ASYNC,
  1737. };
  1738. int status = -ENOMEM;
  1739. calldata = kzalloc(sizeof(*calldata), GFP_KERNEL);
  1740. if (calldata == NULL)
  1741. goto out;
  1742. calldata->inode = state->inode;
  1743. calldata->state = state;
  1744. calldata->arg.fh = NFS_FH(state->inode);
  1745. calldata->arg.stateid = &state->open_stateid;
  1746. /* Serialization for the sequence id */
  1747. calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
  1748. if (calldata->arg.seqid == NULL)
  1749. goto out_free_calldata;
  1750. calldata->arg.fmode = 0;
  1751. calldata->arg.bitmask = server->cache_consistency_bitmask;
  1752. calldata->res.fattr = &calldata->fattr;
  1753. calldata->res.seqid = calldata->arg.seqid;
  1754. calldata->res.server = server;
  1755. calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
  1756. path_get(path);
  1757. calldata->path = *path;
  1758. msg.rpc_argp = &calldata->arg,
  1759. msg.rpc_resp = &calldata->res,
  1760. task_setup_data.callback_data = calldata;
  1761. task = rpc_run_task(&task_setup_data);
  1762. if (IS_ERR(task))
  1763. return PTR_ERR(task);
  1764. status = 0;
  1765. if (wait)
  1766. status = rpc_wait_for_completion_task(task);
  1767. rpc_put_task(task);
  1768. return status;
  1769. out_free_calldata:
  1770. kfree(calldata);
  1771. out:
  1772. nfs4_put_open_state(state);
  1773. nfs4_put_state_owner(sp);
  1774. return status;
  1775. }
  1776. static int nfs4_intent_set_file(struct nameidata *nd, struct path *path, struct nfs4_state *state, fmode_t fmode)
  1777. {
  1778. struct file *filp;
  1779. int ret;
  1780. /* If the open_intent is for execute, we have an extra check to make */
  1781. if (fmode & FMODE_EXEC) {
  1782. ret = nfs_may_open(state->inode,
  1783. state->owner->so_cred,
  1784. nd->intent.open.flags);
  1785. if (ret < 0)
  1786. goto out_close;
  1787. }
  1788. filp = lookup_instantiate_filp(nd, path->dentry, NULL);
  1789. if (!IS_ERR(filp)) {
  1790. struct nfs_open_context *ctx;
  1791. ctx = nfs_file_open_context(filp);
  1792. ctx->state = state;
  1793. return 0;
  1794. }
  1795. ret = PTR_ERR(filp);
  1796. out_close:
  1797. nfs4_close_sync(path, state, fmode & (FMODE_READ|FMODE_WRITE));
  1798. return ret;
  1799. }
  1800. struct dentry *
  1801. nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
  1802. {
  1803. struct path path = {
  1804. .mnt = nd->path.mnt,
  1805. .dentry = dentry,
  1806. };
  1807. struct dentry *parent;
  1808. struct iattr attr;
  1809. struct rpc_cred *cred;
  1810. struct nfs4_state *state;
  1811. struct dentry *res;
  1812. fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
  1813. if (nd->flags & LOOKUP_CREATE) {
  1814. attr.ia_mode = nd->intent.open.create_mode;
  1815. attr.ia_valid = ATTR_MODE;
  1816. if (!IS_POSIXACL(dir))
  1817. attr.ia_mode &= ~current_umask();
  1818. } else {
  1819. attr.ia_valid = 0;
  1820. BUG_ON(nd->intent.open.flags & O_CREAT);
  1821. }
  1822. cred = rpc_lookup_cred();
  1823. if (IS_ERR(cred))
  1824. return (struct dentry *)cred;
  1825. parent = dentry->d_parent;
  1826. /* Protect against concurrent sillydeletes */
  1827. nfs_block_sillyrename(parent);
  1828. state = nfs4_do_open(dir, &path, fmode, nd->intent.open.flags, &attr, cred);
  1829. put_rpccred(cred);
  1830. if (IS_ERR(state)) {
  1831. if (PTR_ERR(state) == -ENOENT) {
  1832. d_add(dentry, NULL);
  1833. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1834. }
  1835. nfs_unblock_sillyrename(parent);
  1836. return (struct dentry *)state;
  1837. }
  1838. res = d_add_unique(dentry, igrab(state->inode));
  1839. if (res != NULL)
  1840. path.dentry = res;
  1841. nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
  1842. nfs_unblock_sillyrename(parent);
  1843. nfs4_intent_set_file(nd, &path, state, fmode);
  1844. return res;
  1845. }
  1846. int
  1847. nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
  1848. {
  1849. struct path path = {
  1850. .mnt = nd->path.mnt,
  1851. .dentry = dentry,
  1852. };
  1853. struct rpc_cred *cred;
  1854. struct nfs4_state *state;
  1855. fmode_t fmode = openflags & (FMODE_READ | FMODE_WRITE);
  1856. cred = rpc_lookup_cred();
  1857. if (IS_ERR(cred))
  1858. return PTR_ERR(cred);
  1859. state = nfs4_do_open(dir, &path, fmode, openflags, NULL, cred);
  1860. put_rpccred(cred);
  1861. if (IS_ERR(state)) {
  1862. switch (PTR_ERR(state)) {
  1863. case -EPERM:
  1864. case -EACCES:
  1865. case -EDQUOT:
  1866. case -ENOSPC:
  1867. case -EROFS:
  1868. return PTR_ERR(state);
  1869. default:
  1870. goto out_drop;
  1871. }
  1872. }
  1873. if (state->inode == dentry->d_inode) {
  1874. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1875. nfs4_intent_set_file(nd, &path, state, fmode);
  1876. return 1;
  1877. }
  1878. nfs4_close_sync(&path, state, fmode);
  1879. out_drop:
  1880. d_drop(dentry);
  1881. return 0;
  1882. }
  1883. static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
  1884. {
  1885. if (ctx->state == NULL)
  1886. return;
  1887. if (is_sync)
  1888. nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
  1889. else
  1890. nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
  1891. }
  1892. static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
  1893. {
  1894. struct nfs4_server_caps_arg args = {
  1895. .fhandle = fhandle,
  1896. };
  1897. struct nfs4_server_caps_res res = {};
  1898. struct rpc_message msg = {
  1899. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
  1900. .rpc_argp = &args,
  1901. .rpc_resp = &res,
  1902. };
  1903. int status;
  1904. status = nfs4_call_sync(server, &msg, &args, &res, 0);
  1905. if (status == 0) {
  1906. memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
  1907. server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
  1908. NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
  1909. NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
  1910. NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
  1911. NFS_CAP_CTIME|NFS_CAP_MTIME);
  1912. if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
  1913. server->caps |= NFS_CAP_ACLS;
  1914. if (res.has_links != 0)
  1915. server->caps |= NFS_CAP_HARDLINKS;
  1916. if (res.has_symlinks != 0)
  1917. server->caps |= NFS_CAP_SYMLINKS;
  1918. if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
  1919. server->caps |= NFS_CAP_FILEID;
  1920. if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
  1921. server->caps |= NFS_CAP_MODE;
  1922. if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
  1923. server->caps |= NFS_CAP_NLINK;
  1924. if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
  1925. server->caps |= NFS_CAP_OWNER;
  1926. if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
  1927. server->caps |= NFS_CAP_OWNER_GROUP;
  1928. if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
  1929. server->caps |= NFS_CAP_ATIME;
  1930. if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
  1931. server->caps |= NFS_CAP_CTIME;
  1932. if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
  1933. server->caps |= NFS_CAP_MTIME;
  1934. memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
  1935. server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
  1936. server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
  1937. server->acl_bitmask = res.acl_bitmask;
  1938. }
  1939. return status;
  1940. }
  1941. int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
  1942. {
  1943. struct nfs4_exception exception = { };
  1944. int err;
  1945. do {
  1946. err = nfs4_handle_exception(server,
  1947. _nfs4_server_capabilities(server, fhandle),
  1948. &exception);
  1949. } while (exception.retry);
  1950. return err;
  1951. }
  1952. static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
  1953. struct nfs_fsinfo *info)
  1954. {
  1955. struct nfs4_lookup_root_arg args = {
  1956. .bitmask = nfs4_fattr_bitmap,
  1957. };
  1958. struct nfs4_lookup_res res = {
  1959. .server = server,
  1960. .fattr = info->fattr,
  1961. .fh = fhandle,
  1962. };
  1963. struct rpc_message msg = {
  1964. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
  1965. .rpc_argp = &args,
  1966. .rpc_resp = &res,
  1967. };
  1968. nfs_fattr_init(info->fattr);
  1969. return nfs4_call_sync(server, &msg, &args, &res, 0);
  1970. }
  1971. static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
  1972. struct nfs_fsinfo *info)
  1973. {
  1974. struct nfs4_exception exception = { };
  1975. int err;
  1976. do {
  1977. err = nfs4_handle_exception(server,
  1978. _nfs4_lookup_root(server, fhandle, info),
  1979. &exception);
  1980. } while (exception.retry);
  1981. return err;
  1982. }
  1983. /*
  1984. * get the file handle for the "/" directory on the server
  1985. */
  1986. static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
  1987. struct nfs_fsinfo *info)
  1988. {
  1989. int status;
  1990. status = nfs4_lookup_root(server, fhandle, info);
  1991. if (status == 0)
  1992. status = nfs4_server_capabilities(server, fhandle);
  1993. if (status == 0)
  1994. status = nfs4_do_fsinfo(server, fhandle, info);
  1995. return nfs4_map_errors(status);
  1996. }
  1997. /*
  1998. * Get locations and (maybe) other attributes of a referral.
  1999. * Note that we'll actually follow the referral later when
  2000. * we detect fsid mismatch in inode revalidation
  2001. */
  2002. static int nfs4_get_referral(struct inode *dir, const struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle)
  2003. {
  2004. int status = -ENOMEM;
  2005. struct page *page = NULL;
  2006. struct nfs4_fs_locations *locations = NULL;
  2007. page = alloc_page(GFP_KERNEL);
  2008. if (page == NULL)
  2009. goto out;
  2010. locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
  2011. if (locations == NULL)
  2012. goto out;
  2013. status = nfs4_proc_fs_locations(dir, name, locations, page);
  2014. if (status != 0)
  2015. goto out;
  2016. /* Make sure server returned a different fsid for the referral */
  2017. if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
  2018. dprintk("%s: server did not return a different fsid for a referral at %s\n", __func__, name->name);
  2019. status = -EIO;
  2020. goto out;
  2021. }
  2022. memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
  2023. fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
  2024. if (!fattr->mode)
  2025. fattr->mode = S_IFDIR;
  2026. memset(fhandle, 0, sizeof(struct nfs_fh));
  2027. out:
  2028. if (page)
  2029. __free_page(page);
  2030. if (locations)
  2031. kfree(locations);
  2032. return status;
  2033. }
  2034. static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
  2035. {
  2036. struct nfs4_getattr_arg args = {
  2037. .fh = fhandle,
  2038. .bitmask = server->attr_bitmask,
  2039. };
  2040. struct nfs4_getattr_res res = {
  2041. .fattr = fattr,
  2042. .server = server,
  2043. };
  2044. struct rpc_message msg = {
  2045. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
  2046. .rpc_argp = &args,
  2047. .rpc_resp = &res,
  2048. };
  2049. nfs_fattr_init(fattr);
  2050. return nfs4_call_sync(server, &msg, &args, &res, 0);
  2051. }
  2052. static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
  2053. {
  2054. struct nfs4_exception exception = { };
  2055. int err;
  2056. do {
  2057. err = nfs4_handle_exception(server,
  2058. _nfs4_proc_getattr(server, fhandle, fattr),
  2059. &exception);
  2060. } while (exception.retry);
  2061. return err;
  2062. }
  2063. /*
  2064. * The file is not closed if it is opened due to the a request to change
  2065. * the size of the file. The open call will not be needed once the
  2066. * VFS layer lookup-intents are implemented.
  2067. *
  2068. * Close is called when the inode is destroyed.
  2069. * If we haven't opened the file for O_WRONLY, we
  2070. * need to in the size_change case to obtain a stateid.
  2071. *
  2072. * Got race?
  2073. * Because OPEN is always done by name in nfsv4, it is
  2074. * possible that we opened a different file by the same
  2075. * name. We can recognize this race condition, but we
  2076. * can't do anything about it besides returning an error.
  2077. *
  2078. * This will be fixed with VFS changes (lookup-intent).
  2079. */
  2080. static int
  2081. nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
  2082. struct iattr *sattr)
  2083. {
  2084. struct inode *inode = dentry->d_inode;
  2085. struct rpc_cred *cred = NULL;
  2086. struct nfs4_state *state = NULL;
  2087. int status;
  2088. nfs_fattr_init(fattr);
  2089. /* Search for an existing open(O_WRITE) file */
  2090. if (sattr->ia_valid & ATTR_FILE) {
  2091. struct nfs_open_context *ctx;
  2092. ctx = nfs_file_open_context(sattr->ia_file);
  2093. if (ctx) {
  2094. cred = ctx->cred;
  2095. state = ctx->state;
  2096. }
  2097. }
  2098. status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
  2099. if (status == 0)
  2100. nfs_setattr_update_inode(inode, sattr);
  2101. return status;
  2102. }
  2103. static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
  2104. const struct qstr *name, struct nfs_fh *fhandle,
  2105. struct nfs_fattr *fattr)
  2106. {
  2107. int status;
  2108. struct nfs4_lookup_arg args = {
  2109. .bitmask = server->attr_bitmask,
  2110. .dir_fh = dirfh,
  2111. .name = name,
  2112. };
  2113. struct nfs4_lookup_res res = {
  2114. .server = server,
  2115. .fattr = fattr,
  2116. .fh = fhandle,
  2117. };
  2118. struct rpc_message msg = {
  2119. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
  2120. .rpc_argp = &args,
  2121. .rpc_resp = &res,
  2122. };
  2123. nfs_fattr_init(fattr);
  2124. dprintk("NFS call lookupfh %s\n", name->name);
  2125. status = nfs4_call_sync(server, &msg, &args, &res, 0);
  2126. dprintk("NFS reply lookupfh: %d\n", status);
  2127. return status;
  2128. }
  2129. static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
  2130. struct qstr *name, struct nfs_fh *fhandle,
  2131. struct nfs_fattr *fattr)
  2132. {
  2133. struct nfs4_exception exception = { };
  2134. int err;
  2135. do {
  2136. err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
  2137. /* FIXME: !!!! */
  2138. if (err == -NFS4ERR_MOVED) {
  2139. err = -EREMOTE;
  2140. break;
  2141. }
  2142. err = nfs4_handle_exception(server, err, &exception);
  2143. } while (exception.retry);
  2144. return err;
  2145. }
  2146. static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
  2147. struct nfs_fh *fhandle, struct nfs_fattr *fattr)
  2148. {
  2149. int status;
  2150. dprintk("NFS call lookup %s\n", name->name);
  2151. status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
  2152. if (status == -NFS4ERR_MOVED)
  2153. status = nfs4_get_referral(dir, name, fattr, fhandle);
  2154. dprintk("NFS reply lookup: %d\n", status);
  2155. return status;
  2156. }
  2157. static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
  2158. {
  2159. struct nfs4_exception exception = { };
  2160. int err;
  2161. do {
  2162. err = nfs4_handle_exception(NFS_SERVER(dir),
  2163. _nfs4_proc_lookup(dir, name, fhandle, fattr),
  2164. &exception);
  2165. } while (exception.retry);
  2166. return err;
  2167. }
  2168. static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
  2169. {
  2170. struct nfs_server *server = NFS_SERVER(inode);
  2171. struct nfs4_accessargs args = {
  2172. .fh = NFS_FH(inode),
  2173. .bitmask = server->attr_bitmask,
  2174. };
  2175. struct nfs4_accessres res = {
  2176. .server = server,
  2177. };
  2178. struct rpc_message msg = {
  2179. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
  2180. .rpc_argp = &args,
  2181. .rpc_resp = &res,
  2182. .rpc_cred = entry->cred,
  2183. };
  2184. int mode = entry->mask;
  2185. int status;
  2186. /*
  2187. * Determine which access bits we want to ask for...
  2188. */
  2189. if (mode & MAY_READ)
  2190. args.access |= NFS4_ACCESS_READ;
  2191. if (S_ISDIR(inode->i_mode)) {
  2192. if (mode & MAY_WRITE)
  2193. args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
  2194. if (mode & MAY_EXEC)
  2195. args.access |= NFS4_ACCESS_LOOKUP;
  2196. } else {
  2197. if (mode & MAY_WRITE)
  2198. args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
  2199. if (mode & MAY_EXEC)
  2200. args.access |= NFS4_ACCESS_EXECUTE;
  2201. }
  2202. res.fattr = nfs_alloc_fattr();
  2203. if (res.fattr == NULL)
  2204. return -ENOMEM;
  2205. status = nfs4_call_sync(server, &msg, &args, &res, 0);
  2206. if (!status) {
  2207. entry->mask = 0;
  2208. if (res.access & NFS4_ACCESS_READ)
  2209. entry->mask |= MAY_READ;
  2210. if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
  2211. entry->mask |= MAY_WRITE;
  2212. if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
  2213. entry->mask |= MAY_EXEC;
  2214. nfs_refresh_inode(inode, res.fattr);
  2215. }
  2216. nfs_free_fattr(res.fattr);
  2217. return status;
  2218. }
  2219. static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
  2220. {
  2221. struct nfs4_exception exception = { };
  2222. int err;
  2223. do {
  2224. err = nfs4_handle_exception(NFS_SERVER(inode),
  2225. _nfs4_proc_access(inode, entry),
  2226. &exception);
  2227. } while (exception.retry);
  2228. return err;
  2229. }
  2230. /*
  2231. * TODO: For the time being, we don't try to get any attributes
  2232. * along with any of the zero-copy operations READ, READDIR,
  2233. * READLINK, WRITE.
  2234. *
  2235. * In the case of the first three, we want to put the GETATTR
  2236. * after the read-type operation -- this is because it is hard
  2237. * to predict the length of a GETATTR response in v4, and thus
  2238. * align the READ data correctly. This means that the GETATTR
  2239. * may end up partially falling into the page cache, and we should
  2240. * shift it into the 'tail' of the xdr_buf before processing.
  2241. * To do this efficiently, we need to know the total length
  2242. * of data received, which doesn't seem to be available outside
  2243. * of the RPC layer.
  2244. *
  2245. * In the case of WRITE, we also want to put the GETATTR after
  2246. * the operation -- in this case because we want to make sure
  2247. * we get the post-operation mtime and size. This means that
  2248. * we can't use xdr_encode_pages() as written: we need a variant
  2249. * of it which would leave room in the 'tail' iovec.
  2250. *
  2251. * Both of these changes to the XDR layer would in fact be quite
  2252. * minor, but I decided to leave them for a subsequent patch.
  2253. */
  2254. static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
  2255. unsigned int pgbase, unsigned int pglen)
  2256. {
  2257. struct nfs4_readlink args = {
  2258. .fh = NFS_FH(inode),
  2259. .pgbase = pgbase,
  2260. .pglen = pglen,
  2261. .pages = &page,
  2262. };
  2263. struct nfs4_readlink_res res;
  2264. struct rpc_message msg = {
  2265. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
  2266. .rpc_argp = &args,
  2267. .rpc_resp = &res,
  2268. };
  2269. return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
  2270. }
  2271. static int nfs4_proc_readlink(struct inode *inode, struct page *page,
  2272. unsigned int pgbase, unsigned int pglen)
  2273. {
  2274. struct nfs4_exception exception = { };
  2275. int err;
  2276. do {
  2277. err = nfs4_handle_exception(NFS_SERVER(inode),
  2278. _nfs4_proc_readlink(inode, page, pgbase, pglen),
  2279. &exception);
  2280. } while (exception.retry);
  2281. return err;
  2282. }
  2283. /*
  2284. * Got race?
  2285. * We will need to arrange for the VFS layer to provide an atomic open.
  2286. * Until then, this create/open method is prone to inefficiency and race
  2287. * conditions due to the lookup, create, and open VFS calls from sys_open()
  2288. * placed on the wire.
  2289. *
  2290. * Given the above sorry state of affairs, I'm simply sending an OPEN.
  2291. * The file will be opened again in the subsequent VFS open call
  2292. * (nfs4_proc_file_open).
  2293. *
  2294. * The open for read will just hang around to be used by any process that
  2295. * opens the file O_RDONLY. This will all be resolved with the VFS changes.
  2296. */
  2297. static int
  2298. nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
  2299. int flags, struct nameidata *nd)
  2300. {
  2301. struct path path = {
  2302. .mnt = nd->path.mnt,
  2303. .dentry = dentry,
  2304. };
  2305. struct nfs4_state *state;
  2306. struct rpc_cred *cred;
  2307. fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE);
  2308. int status = 0;
  2309. cred = rpc_lookup_cred();
  2310. if (IS_ERR(cred)) {
  2311. status = PTR_ERR(cred);
  2312. goto out;
  2313. }
  2314. state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred);
  2315. d_drop(dentry);
  2316. if (IS_ERR(state)) {
  2317. status = PTR_ERR(state);
  2318. goto out_putcred;
  2319. }
  2320. d_add(dentry, igrab(state->inode));
  2321. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  2322. if (flags & O_EXCL) {
  2323. struct nfs_fattr fattr;
  2324. status = nfs4_do_setattr(state->inode, cred, &fattr, sattr, state);
  2325. if (status == 0)
  2326. nfs_setattr_update_inode(state->inode, sattr);
  2327. nfs_post_op_update_inode(state->inode, &fattr);
  2328. }
  2329. if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
  2330. status = nfs4_intent_set_file(nd, &path, state, fmode);
  2331. else
  2332. nfs4_close_sync(&path, state, fmode);
  2333. out_putcred:
  2334. put_rpccred(cred);
  2335. out:
  2336. return status;
  2337. }
  2338. static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
  2339. {
  2340. struct nfs_server *server = NFS_SERVER(dir);
  2341. struct nfs_removeargs args = {
  2342. .fh = NFS_FH(dir),
  2343. .name.len = name->len,
  2344. .name.name = name->name,
  2345. .bitmask = server->attr_bitmask,
  2346. };
  2347. struct nfs_removeres res = {
  2348. .server = server,
  2349. };
  2350. struct rpc_message msg = {
  2351. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
  2352. .rpc_argp = &args,
  2353. .rpc_resp = &res,
  2354. };
  2355. int status;
  2356. nfs_fattr_init(&res.dir_attr);
  2357. status = nfs4_call_sync(server, &msg, &args, &res, 1);
  2358. if (status == 0) {
  2359. update_changeattr(dir, &res.cinfo);
  2360. nfs_post_op_update_inode(dir, &res.dir_attr);
  2361. }
  2362. return status;
  2363. }
  2364. static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
  2365. {
  2366. struct nfs4_exception exception = { };
  2367. int err;
  2368. do {
  2369. err = nfs4_handle_exception(NFS_SERVER(dir),
  2370. _nfs4_proc_remove(dir, name),
  2371. &exception);
  2372. } while (exception.retry);
  2373. return err;
  2374. }
  2375. static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
  2376. {
  2377. struct nfs_server *server = NFS_SERVER(dir);
  2378. struct nfs_removeargs *args = msg->rpc_argp;
  2379. struct nfs_removeres *res = msg->rpc_resp;
  2380. args->bitmask = server->cache_consistency_bitmask;
  2381. res->server = server;
  2382. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
  2383. }
  2384. static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
  2385. {
  2386. struct nfs_removeres *res = task->tk_msg.rpc_resp;
  2387. nfs4_sequence_done(res->server, &res->seq_res, task->tk_status);
  2388. if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
  2389. return 0;
  2390. update_changeattr(dir, &res->cinfo);
  2391. nfs_post_op_update_inode(dir, &res->dir_attr);
  2392. return 1;
  2393. }
  2394. static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
  2395. struct inode *new_dir, struct qstr *new_name)
  2396. {
  2397. struct nfs_server *server = NFS_SERVER(old_dir);
  2398. struct nfs4_rename_arg arg = {
  2399. .old_dir = NFS_FH(old_dir),
  2400. .new_dir = NFS_FH(new_dir),
  2401. .old_name = old_name,
  2402. .new_name = new_name,
  2403. .bitmask = server->attr_bitmask,
  2404. };
  2405. struct nfs4_rename_res res = {
  2406. .server = server,
  2407. };
  2408. struct rpc_message msg = {
  2409. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
  2410. .rpc_argp = &arg,
  2411. .rpc_resp = &res,
  2412. };
  2413. int status = -ENOMEM;
  2414. res.old_fattr = nfs_alloc_fattr();
  2415. res.new_fattr = nfs_alloc_fattr();
  2416. if (res.old_fattr == NULL || res.new_fattr == NULL)
  2417. goto out;
  2418. status = nfs4_call_sync(server, &msg, &arg, &res, 1);
  2419. if (!status) {
  2420. update_changeattr(old_dir, &res.old_cinfo);
  2421. nfs_post_op_update_inode(old_dir, res.old_fattr);
  2422. update_changeattr(new_dir, &res.new_cinfo);
  2423. nfs_post_op_update_inode(new_dir, res.new_fattr);
  2424. }
  2425. out:
  2426. nfs_free_fattr(res.new_fattr);
  2427. nfs_free_fattr(res.old_fattr);
  2428. return status;
  2429. }
  2430. static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
  2431. struct inode *new_dir, struct qstr *new_name)
  2432. {
  2433. struct nfs4_exception exception = { };
  2434. int err;
  2435. do {
  2436. err = nfs4_handle_exception(NFS_SERVER(old_dir),
  2437. _nfs4_proc_rename(old_dir, old_name,
  2438. new_dir, new_name),
  2439. &exception);
  2440. } while (exception.retry);
  2441. return err;
  2442. }
  2443. static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
  2444. {
  2445. struct nfs_server *server = NFS_SERVER(inode);
  2446. struct nfs4_link_arg arg = {
  2447. .fh = NFS_FH(inode),
  2448. .dir_fh = NFS_FH(dir),
  2449. .name = name,
  2450. .bitmask = server->attr_bitmask,
  2451. };
  2452. struct nfs_fattr fattr, dir_attr;
  2453. struct nfs4_link_res res = {
  2454. .server = server,
  2455. .fattr = &fattr,
  2456. .dir_attr = &dir_attr,
  2457. };
  2458. struct rpc_message msg = {
  2459. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
  2460. .rpc_argp = &arg,
  2461. .rpc_resp = &res,
  2462. };
  2463. int status;
  2464. nfs_fattr_init(res.fattr);
  2465. nfs_fattr_init(res.dir_attr);
  2466. status = nfs4_call_sync(server, &msg, &arg, &res, 1);
  2467. if (!status) {
  2468. update_changeattr(dir, &res.cinfo);
  2469. nfs_post_op_update_inode(dir, res.dir_attr);
  2470. nfs_post_op_update_inode(inode, res.fattr);
  2471. }
  2472. return status;
  2473. }
  2474. static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
  2475. {
  2476. struct nfs4_exception exception = { };
  2477. int err;
  2478. do {
  2479. err = nfs4_handle_exception(NFS_SERVER(inode),
  2480. _nfs4_proc_link(inode, dir, name),
  2481. &exception);
  2482. } while (exception.retry);
  2483. return err;
  2484. }
  2485. struct nfs4_createdata {
  2486. struct rpc_message msg;
  2487. struct nfs4_create_arg arg;
  2488. struct nfs4_create_res res;
  2489. struct nfs_fh fh;
  2490. struct nfs_fattr fattr;
  2491. struct nfs_fattr dir_fattr;
  2492. };
  2493. static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
  2494. struct qstr *name, struct iattr *sattr, u32 ftype)
  2495. {
  2496. struct nfs4_createdata *data;
  2497. data = kzalloc(sizeof(*data), GFP_KERNEL);
  2498. if (data != NULL) {
  2499. struct nfs_server *server = NFS_SERVER(dir);
  2500. data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
  2501. data->msg.rpc_argp = &data->arg;
  2502. data->msg.rpc_resp = &data->res;
  2503. data->arg.dir_fh = NFS_FH(dir);
  2504. data->arg.server = server;
  2505. data->arg.name = name;
  2506. data->arg.attrs = sattr;
  2507. data->arg.ftype = ftype;
  2508. data->arg.bitmask = server->attr_bitmask;
  2509. data->res.server = server;
  2510. data->res.fh = &data->fh;
  2511. data->res.fattr = &data->fattr;
  2512. data->res.dir_fattr = &data->dir_fattr;
  2513. nfs_fattr_init(data->res.fattr);
  2514. nfs_fattr_init(data->res.dir_fattr);
  2515. }
  2516. return data;
  2517. }
  2518. static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
  2519. {
  2520. int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
  2521. &data->arg, &data->res, 1);
  2522. if (status == 0) {
  2523. update_changeattr(dir, &data->res.dir_cinfo);
  2524. nfs_post_op_update_inode(dir, data->res.dir_fattr);
  2525. status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
  2526. }
  2527. return status;
  2528. }
  2529. static void nfs4_free_createdata(struct nfs4_createdata *data)
  2530. {
  2531. kfree(data);
  2532. }
  2533. static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
  2534. struct page *page, unsigned int len, struct iattr *sattr)
  2535. {
  2536. struct nfs4_createdata *data;
  2537. int status = -ENAMETOOLONG;
  2538. if (len > NFS4_MAXPATHLEN)
  2539. goto out;
  2540. status = -ENOMEM;
  2541. data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
  2542. if (data == NULL)
  2543. goto out;
  2544. data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
  2545. data->arg.u.symlink.pages = &page;
  2546. data->arg.u.symlink.len = len;
  2547. status = nfs4_do_create(dir, dentry, data);
  2548. nfs4_free_createdata(data);
  2549. out:
  2550. return status;
  2551. }
  2552. static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
  2553. struct page *page, unsigned int len, struct iattr *sattr)
  2554. {
  2555. struct nfs4_exception exception = { };
  2556. int err;
  2557. do {
  2558. err = nfs4_handle_exception(NFS_SERVER(dir),
  2559. _nfs4_proc_symlink(dir, dentry, page,
  2560. len, sattr),
  2561. &exception);
  2562. } while (exception.retry);
  2563. return err;
  2564. }
  2565. static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
  2566. struct iattr *sattr)
  2567. {
  2568. struct nfs4_createdata *data;
  2569. int status = -ENOMEM;
  2570. data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
  2571. if (data == NULL)
  2572. goto out;
  2573. status = nfs4_do_create(dir, dentry, data);
  2574. nfs4_free_createdata(data);
  2575. out:
  2576. return status;
  2577. }
  2578. static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
  2579. struct iattr *sattr)
  2580. {
  2581. struct nfs4_exception exception = { };
  2582. int err;
  2583. do {
  2584. err = nfs4_handle_exception(NFS_SERVER(dir),
  2585. _nfs4_proc_mkdir(dir, dentry, sattr),
  2586. &exception);
  2587. } while (exception.retry);
  2588. return err;
  2589. }
  2590. static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
  2591. u64 cookie, struct page *page, unsigned int count, int plus)
  2592. {
  2593. struct inode *dir = dentry->d_inode;
  2594. struct nfs4_readdir_arg args = {
  2595. .fh = NFS_FH(dir),
  2596. .pages = &page,
  2597. .pgbase = 0,
  2598. .count = count,
  2599. .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
  2600. };
  2601. struct nfs4_readdir_res res;
  2602. struct rpc_message msg = {
  2603. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
  2604. .rpc_argp = &args,
  2605. .rpc_resp = &res,
  2606. .rpc_cred = cred,
  2607. };
  2608. int status;
  2609. dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
  2610. dentry->d_parent->d_name.name,
  2611. dentry->d_name.name,
  2612. (unsigned long long)cookie);
  2613. nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
  2614. res.pgbase = args.pgbase;
  2615. status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
  2616. if (status == 0)
  2617. memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
  2618. nfs_invalidate_atime(dir);
  2619. dprintk("%s: returns %d\n", __func__, status);
  2620. return status;
  2621. }
  2622. static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
  2623. u64 cookie, struct page *page, unsigned int count, int plus)
  2624. {
  2625. struct nfs4_exception exception = { };
  2626. int err;
  2627. do {
  2628. err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
  2629. _nfs4_proc_readdir(dentry, cred, cookie,
  2630. page, count, plus),
  2631. &exception);
  2632. } while (exception.retry);
  2633. return err;
  2634. }
  2635. static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
  2636. struct iattr *sattr, dev_t rdev)
  2637. {
  2638. struct nfs4_createdata *data;
  2639. int mode = sattr->ia_mode;
  2640. int status = -ENOMEM;
  2641. BUG_ON(!(sattr->ia_valid & ATTR_MODE));
  2642. BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
  2643. data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
  2644. if (data == NULL)
  2645. goto out;
  2646. if (S_ISFIFO(mode))
  2647. data->arg.ftype = NF4FIFO;
  2648. else if (S_ISBLK(mode)) {
  2649. data->arg.ftype = NF4BLK;
  2650. data->arg.u.device.specdata1 = MAJOR(rdev);
  2651. data->arg.u.device.specdata2 = MINOR(rdev);
  2652. }
  2653. else if (S_ISCHR(mode)) {
  2654. data->arg.ftype = NF4CHR;
  2655. data->arg.u.device.specdata1 = MAJOR(rdev);
  2656. data->arg.u.device.specdata2 = MINOR(rdev);
  2657. }
  2658. status = nfs4_do_create(dir, dentry, data);
  2659. nfs4_free_createdata(data);
  2660. out:
  2661. return status;
  2662. }
  2663. static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
  2664. struct iattr *sattr, dev_t rdev)
  2665. {
  2666. struct nfs4_exception exception = { };
  2667. int err;
  2668. do {
  2669. err = nfs4_handle_exception(NFS_SERVER(dir),
  2670. _nfs4_proc_mknod(dir, dentry, sattr, rdev),
  2671. &exception);
  2672. } while (exception.retry);
  2673. return err;
  2674. }
  2675. static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
  2676. struct nfs_fsstat *fsstat)
  2677. {
  2678. struct nfs4_statfs_arg args = {
  2679. .fh = fhandle,
  2680. .bitmask = server->attr_bitmask,
  2681. };
  2682. struct nfs4_statfs_res res = {
  2683. .fsstat = fsstat,
  2684. };
  2685. struct rpc_message msg = {
  2686. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
  2687. .rpc_argp = &args,
  2688. .rpc_resp = &res,
  2689. };
  2690. nfs_fattr_init(fsstat->fattr);
  2691. return nfs4_call_sync(server, &msg, &args, &res, 0);
  2692. }
  2693. static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
  2694. {
  2695. struct nfs4_exception exception = { };
  2696. int err;
  2697. do {
  2698. err = nfs4_handle_exception(server,
  2699. _nfs4_proc_statfs(server, fhandle, fsstat),
  2700. &exception);
  2701. } while (exception.retry);
  2702. return err;
  2703. }
  2704. static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
  2705. struct nfs_fsinfo *fsinfo)
  2706. {
  2707. struct nfs4_fsinfo_arg args = {
  2708. .fh = fhandle,
  2709. .bitmask = server->attr_bitmask,
  2710. };
  2711. struct nfs4_fsinfo_res res = {
  2712. .fsinfo = fsinfo,
  2713. };
  2714. struct rpc_message msg = {
  2715. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
  2716. .rpc_argp = &args,
  2717. .rpc_resp = &res,
  2718. };
  2719. return nfs4_call_sync(server, &msg, &args, &res, 0);
  2720. }
  2721. static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
  2722. {
  2723. struct nfs4_exception exception = { };
  2724. int err;
  2725. do {
  2726. err = nfs4_handle_exception(server,
  2727. _nfs4_do_fsinfo(server, fhandle, fsinfo),
  2728. &exception);
  2729. } while (exception.retry);
  2730. return err;
  2731. }
  2732. static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
  2733. {
  2734. nfs_fattr_init(fsinfo->fattr);
  2735. return nfs4_do_fsinfo(server, fhandle, fsinfo);
  2736. }
  2737. static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
  2738. struct nfs_pathconf *pathconf)
  2739. {
  2740. struct nfs4_pathconf_arg args = {
  2741. .fh = fhandle,
  2742. .bitmask = server->attr_bitmask,
  2743. };
  2744. struct nfs4_pathconf_res res = {
  2745. .pathconf = pathconf,
  2746. };
  2747. struct rpc_message msg = {
  2748. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
  2749. .rpc_argp = &args,
  2750. .rpc_resp = &res,
  2751. };
  2752. /* None of the pathconf attributes are mandatory to implement */
  2753. if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
  2754. memset(pathconf, 0, sizeof(*pathconf));
  2755. return 0;
  2756. }
  2757. nfs_fattr_init(pathconf->fattr);
  2758. return nfs4_call_sync(server, &msg, &args, &res, 0);
  2759. }
  2760. static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
  2761. struct nfs_pathconf *pathconf)
  2762. {
  2763. struct nfs4_exception exception = { };
  2764. int err;
  2765. do {
  2766. err = nfs4_handle_exception(server,
  2767. _nfs4_proc_pathconf(server, fhandle, pathconf),
  2768. &exception);
  2769. } while (exception.retry);
  2770. return err;
  2771. }
  2772. static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
  2773. {
  2774. struct nfs_server *server = NFS_SERVER(data->inode);
  2775. dprintk("--> %s\n", __func__);
  2776. nfs4_sequence_done(server, &data->res.seq_res, task->tk_status);
  2777. if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
  2778. nfs_restart_rpc(task, server->nfs_client);
  2779. return -EAGAIN;
  2780. }
  2781. nfs_invalidate_atime(data->inode);
  2782. if (task->tk_status > 0)
  2783. renew_lease(server, data->timestamp);
  2784. return 0;
  2785. }
  2786. static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
  2787. {
  2788. data->timestamp = jiffies;
  2789. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
  2790. }
  2791. static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
  2792. {
  2793. struct inode *inode = data->inode;
  2794. nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
  2795. task->tk_status);
  2796. if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
  2797. nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
  2798. return -EAGAIN;
  2799. }
  2800. if (task->tk_status >= 0) {
  2801. renew_lease(NFS_SERVER(inode), data->timestamp);
  2802. nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
  2803. }
  2804. return 0;
  2805. }
  2806. static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
  2807. {
  2808. struct nfs_server *server = NFS_SERVER(data->inode);
  2809. data->args.bitmask = server->cache_consistency_bitmask;
  2810. data->res.server = server;
  2811. data->timestamp = jiffies;
  2812. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
  2813. }
  2814. static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
  2815. {
  2816. struct inode *inode = data->inode;
  2817. nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
  2818. task->tk_status);
  2819. if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
  2820. nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
  2821. return -EAGAIN;
  2822. }
  2823. nfs_refresh_inode(inode, data->res.fattr);
  2824. return 0;
  2825. }
  2826. static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
  2827. {
  2828. struct nfs_server *server = NFS_SERVER(data->inode);
  2829. data->args.bitmask = server->cache_consistency_bitmask;
  2830. data->res.server = server;
  2831. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
  2832. }
  2833. /*
  2834. * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
  2835. * standalone procedure for queueing an asynchronous RENEW.
  2836. */
  2837. static void nfs4_renew_release(void *data)
  2838. {
  2839. struct nfs_client *clp = data;
  2840. if (atomic_read(&clp->cl_count) > 1)
  2841. nfs4_schedule_state_renewal(clp);
  2842. nfs_put_client(clp);
  2843. }
  2844. static void nfs4_renew_done(struct rpc_task *task, void *data)
  2845. {
  2846. struct nfs_client *clp = data;
  2847. unsigned long timestamp = task->tk_start;
  2848. if (task->tk_status < 0) {
  2849. /* Unless we're shutting down, schedule state recovery! */
  2850. if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
  2851. nfs4_schedule_state_recovery(clp);
  2852. return;
  2853. }
  2854. spin_lock(&clp->cl_lock);
  2855. if (time_before(clp->cl_last_renewal,timestamp))
  2856. clp->cl_last_renewal = timestamp;
  2857. spin_unlock(&clp->cl_lock);
  2858. }
  2859. static const struct rpc_call_ops nfs4_renew_ops = {
  2860. .rpc_call_done = nfs4_renew_done,
  2861. .rpc_release = nfs4_renew_release,
  2862. };
  2863. int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
  2864. {
  2865. struct rpc_message msg = {
  2866. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
  2867. .rpc_argp = clp,
  2868. .rpc_cred = cred,
  2869. };
  2870. if (!atomic_inc_not_zero(&clp->cl_count))
  2871. return -EIO;
  2872. return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
  2873. &nfs4_renew_ops, clp);
  2874. }
  2875. int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
  2876. {
  2877. struct rpc_message msg = {
  2878. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
  2879. .rpc_argp = clp,
  2880. .rpc_cred = cred,
  2881. };
  2882. unsigned long now = jiffies;
  2883. int status;
  2884. status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
  2885. if (status < 0)
  2886. return status;
  2887. spin_lock(&clp->cl_lock);
  2888. if (time_before(clp->cl_last_renewal,now))
  2889. clp->cl_last_renewal = now;
  2890. spin_unlock(&clp->cl_lock);
  2891. return 0;
  2892. }
  2893. static inline int nfs4_server_supports_acls(struct nfs_server *server)
  2894. {
  2895. return (server->caps & NFS_CAP_ACLS)
  2896. && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
  2897. && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
  2898. }
  2899. /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
  2900. * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
  2901. * the stack.
  2902. */
  2903. #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
  2904. static void buf_to_pages(const void *buf, size_t buflen,
  2905. struct page **pages, unsigned int *pgbase)
  2906. {
  2907. const void *p = buf;
  2908. *pgbase = offset_in_page(buf);
  2909. p -= *pgbase;
  2910. while (p < buf + buflen) {
  2911. *(pages++) = virt_to_page(p);
  2912. p += PAGE_CACHE_SIZE;
  2913. }
  2914. }
  2915. struct nfs4_cached_acl {
  2916. int cached;
  2917. size_t len;
  2918. char data[0];
  2919. };
  2920. static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
  2921. {
  2922. struct nfs_inode *nfsi = NFS_I(inode);
  2923. spin_lock(&inode->i_lock);
  2924. kfree(nfsi->nfs4_acl);
  2925. nfsi->nfs4_acl = acl;
  2926. spin_unlock(&inode->i_lock);
  2927. }
  2928. static void nfs4_zap_acl_attr(struct inode *inode)
  2929. {
  2930. nfs4_set_cached_acl(inode, NULL);
  2931. }
  2932. static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
  2933. {
  2934. struct nfs_inode *nfsi = NFS_I(inode);
  2935. struct nfs4_cached_acl *acl;
  2936. int ret = -ENOENT;
  2937. spin_lock(&inode->i_lock);
  2938. acl = nfsi->nfs4_acl;
  2939. if (acl == NULL)
  2940. goto out;
  2941. if (buf == NULL) /* user is just asking for length */
  2942. goto out_len;
  2943. if (acl->cached == 0)
  2944. goto out;
  2945. ret = -ERANGE; /* see getxattr(2) man page */
  2946. if (acl->len > buflen)
  2947. goto out;
  2948. memcpy(buf, acl->data, acl->len);
  2949. out_len:
  2950. ret = acl->len;
  2951. out:
  2952. spin_unlock(&inode->i_lock);
  2953. return ret;
  2954. }
  2955. static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
  2956. {
  2957. struct nfs4_cached_acl *acl;
  2958. if (buf && acl_len <= PAGE_SIZE) {
  2959. acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
  2960. if (acl == NULL)
  2961. goto out;
  2962. acl->cached = 1;
  2963. memcpy(acl->data, buf, acl_len);
  2964. } else {
  2965. acl = kmalloc(sizeof(*acl), GFP_KERNEL);
  2966. if (acl == NULL)
  2967. goto out;
  2968. acl->cached = 0;
  2969. }
  2970. acl->len = acl_len;
  2971. out:
  2972. nfs4_set_cached_acl(inode, acl);
  2973. }
  2974. static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
  2975. {
  2976. struct page *pages[NFS4ACL_MAXPAGES];
  2977. struct nfs_getaclargs args = {
  2978. .fh = NFS_FH(inode),
  2979. .acl_pages = pages,
  2980. .acl_len = buflen,
  2981. };
  2982. struct nfs_getaclres res = {
  2983. .acl_len = buflen,
  2984. };
  2985. void *resp_buf;
  2986. struct rpc_message msg = {
  2987. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
  2988. .rpc_argp = &args,
  2989. .rpc_resp = &res,
  2990. };
  2991. struct page *localpage = NULL;
  2992. int ret;
  2993. if (buflen < PAGE_SIZE) {
  2994. /* As long as we're doing a round trip to the server anyway,
  2995. * let's be prepared for a page of acl data. */
  2996. localpage = alloc_page(GFP_KERNEL);
  2997. resp_buf = page_address(localpage);
  2998. if (localpage == NULL)
  2999. return -ENOMEM;
  3000. args.acl_pages[0] = localpage;
  3001. args.acl_pgbase = 0;
  3002. args.acl_len = PAGE_SIZE;
  3003. } else {
  3004. resp_buf = buf;
  3005. buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
  3006. }
  3007. ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
  3008. if (ret)
  3009. goto out_free;
  3010. if (res.acl_len > args.acl_len)
  3011. nfs4_write_cached_acl(inode, NULL, res.acl_len);
  3012. else
  3013. nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
  3014. if (buf) {
  3015. ret = -ERANGE;
  3016. if (res.acl_len > buflen)
  3017. goto out_free;
  3018. if (localpage)
  3019. memcpy(buf, resp_buf, res.acl_len);
  3020. }
  3021. ret = res.acl_len;
  3022. out_free:
  3023. if (localpage)
  3024. __free_page(localpage);
  3025. return ret;
  3026. }
  3027. static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
  3028. {
  3029. struct nfs4_exception exception = { };
  3030. ssize_t ret;
  3031. do {
  3032. ret = __nfs4_get_acl_uncached(inode, buf, buflen);
  3033. if (ret >= 0)
  3034. break;
  3035. ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
  3036. } while (exception.retry);
  3037. return ret;
  3038. }
  3039. static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
  3040. {
  3041. struct nfs_server *server = NFS_SERVER(inode);
  3042. int ret;
  3043. if (!nfs4_server_supports_acls(server))
  3044. return -EOPNOTSUPP;
  3045. ret = nfs_revalidate_inode(server, inode);
  3046. if (ret < 0)
  3047. return ret;
  3048. ret = nfs4_read_cached_acl(inode, buf, buflen);
  3049. if (ret != -ENOENT)
  3050. return ret;
  3051. return nfs4_get_acl_uncached(inode, buf, buflen);
  3052. }
  3053. static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
  3054. {
  3055. struct nfs_server *server = NFS_SERVER(inode);
  3056. struct page *pages[NFS4ACL_MAXPAGES];
  3057. struct nfs_setaclargs arg = {
  3058. .fh = NFS_FH(inode),
  3059. .acl_pages = pages,
  3060. .acl_len = buflen,
  3061. };
  3062. struct nfs_setaclres res;
  3063. struct rpc_message msg = {
  3064. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
  3065. .rpc_argp = &arg,
  3066. .rpc_resp = &res,
  3067. };
  3068. int ret;
  3069. if (!nfs4_server_supports_acls(server))
  3070. return -EOPNOTSUPP;
  3071. nfs_inode_return_delegation(inode);
  3072. buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
  3073. ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
  3074. nfs_access_zap_cache(inode);
  3075. nfs_zap_acl_cache(inode);
  3076. return ret;
  3077. }
  3078. static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
  3079. {
  3080. struct nfs4_exception exception = { };
  3081. int err;
  3082. do {
  3083. err = nfs4_handle_exception(NFS_SERVER(inode),
  3084. __nfs4_proc_set_acl(inode, buf, buflen),
  3085. &exception);
  3086. } while (exception.retry);
  3087. return err;
  3088. }
  3089. static int
  3090. _nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs_client *clp, struct nfs4_state *state)
  3091. {
  3092. if (!clp || task->tk_status >= 0)
  3093. return 0;
  3094. switch(task->tk_status) {
  3095. case -NFS4ERR_ADMIN_REVOKED:
  3096. case -NFS4ERR_BAD_STATEID:
  3097. case -NFS4ERR_OPENMODE:
  3098. if (state == NULL)
  3099. break;
  3100. nfs4_state_mark_reclaim_nograce(clp, state);
  3101. goto do_state_recovery;
  3102. case -NFS4ERR_STALE_STATEID:
  3103. if (state == NULL)
  3104. break;
  3105. nfs4_state_mark_reclaim_reboot(clp, state);
  3106. case -NFS4ERR_STALE_CLIENTID:
  3107. case -NFS4ERR_EXPIRED:
  3108. goto do_state_recovery;
  3109. #if defined(CONFIG_NFS_V4_1)
  3110. case -NFS4ERR_BADSESSION:
  3111. case -NFS4ERR_BADSLOT:
  3112. case -NFS4ERR_BAD_HIGH_SLOT:
  3113. case -NFS4ERR_DEADSESSION:
  3114. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  3115. case -NFS4ERR_SEQ_FALSE_RETRY:
  3116. case -NFS4ERR_SEQ_MISORDERED:
  3117. dprintk("%s ERROR %d, Reset session\n", __func__,
  3118. task->tk_status);
  3119. nfs4_schedule_state_recovery(clp);
  3120. task->tk_status = 0;
  3121. return -EAGAIN;
  3122. #endif /* CONFIG_NFS_V4_1 */
  3123. case -NFS4ERR_DELAY:
  3124. if (server)
  3125. nfs_inc_server_stats(server, NFSIOS_DELAY);
  3126. case -NFS4ERR_GRACE:
  3127. case -EKEYEXPIRED:
  3128. rpc_delay(task, NFS4_POLL_RETRY_MAX);
  3129. task->tk_status = 0;
  3130. return -EAGAIN;
  3131. case -NFS4ERR_OLD_STATEID:
  3132. task->tk_status = 0;
  3133. return -EAGAIN;
  3134. }
  3135. task->tk_status = nfs4_map_errors(task->tk_status);
  3136. return 0;
  3137. do_state_recovery:
  3138. rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
  3139. nfs4_schedule_state_recovery(clp);
  3140. if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
  3141. rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
  3142. task->tk_status = 0;
  3143. return -EAGAIN;
  3144. }
  3145. static int
  3146. nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
  3147. {
  3148. return _nfs4_async_handle_error(task, server, server->nfs_client, state);
  3149. }
  3150. int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
  3151. {
  3152. nfs4_verifier sc_verifier;
  3153. struct nfs4_setclientid setclientid = {
  3154. .sc_verifier = &sc_verifier,
  3155. .sc_prog = program,
  3156. };
  3157. struct rpc_message msg = {
  3158. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
  3159. .rpc_argp = &setclientid,
  3160. .rpc_resp = clp,
  3161. .rpc_cred = cred,
  3162. };
  3163. __be32 *p;
  3164. int loop = 0;
  3165. int status;
  3166. p = (__be32*)sc_verifier.data;
  3167. *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
  3168. *p = htonl((u32)clp->cl_boot_time.tv_nsec);
  3169. for(;;) {
  3170. setclientid.sc_name_len = scnprintf(setclientid.sc_name,
  3171. sizeof(setclientid.sc_name), "%s/%s %s %s %u",
  3172. clp->cl_ipaddr,
  3173. rpc_peeraddr2str(clp->cl_rpcclient,
  3174. RPC_DISPLAY_ADDR),
  3175. rpc_peeraddr2str(clp->cl_rpcclient,
  3176. RPC_DISPLAY_PROTO),
  3177. clp->cl_rpcclient->cl_auth->au_ops->au_name,
  3178. clp->cl_id_uniquifier);
  3179. setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
  3180. sizeof(setclientid.sc_netid),
  3181. rpc_peeraddr2str(clp->cl_rpcclient,
  3182. RPC_DISPLAY_NETID));
  3183. setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
  3184. sizeof(setclientid.sc_uaddr), "%s.%u.%u",
  3185. clp->cl_ipaddr, port >> 8, port & 255);
  3186. status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
  3187. if (status != -NFS4ERR_CLID_INUSE)
  3188. break;
  3189. if (signalled())
  3190. break;
  3191. if (loop++ & 1)
  3192. ssleep(clp->cl_lease_time + 1);
  3193. else
  3194. if (++clp->cl_id_uniquifier == 0)
  3195. break;
  3196. }
  3197. return status;
  3198. }
  3199. static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
  3200. {
  3201. struct nfs_fsinfo fsinfo;
  3202. struct rpc_message msg = {
  3203. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
  3204. .rpc_argp = clp,
  3205. .rpc_resp = &fsinfo,
  3206. .rpc_cred = cred,
  3207. };
  3208. unsigned long now;
  3209. int status;
  3210. now = jiffies;
  3211. status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
  3212. if (status == 0) {
  3213. spin_lock(&clp->cl_lock);
  3214. clp->cl_lease_time = fsinfo.lease_time * HZ;
  3215. clp->cl_last_renewal = now;
  3216. spin_unlock(&clp->cl_lock);
  3217. }
  3218. return status;
  3219. }
  3220. int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
  3221. {
  3222. long timeout = 0;
  3223. int err;
  3224. do {
  3225. err = _nfs4_proc_setclientid_confirm(clp, cred);
  3226. switch (err) {
  3227. case 0:
  3228. return err;
  3229. case -NFS4ERR_RESOURCE:
  3230. /* The IBM lawyers misread another document! */
  3231. case -NFS4ERR_DELAY:
  3232. case -EKEYEXPIRED:
  3233. err = nfs4_delay(clp->cl_rpcclient, &timeout);
  3234. }
  3235. } while (err == 0);
  3236. return err;
  3237. }
  3238. struct nfs4_delegreturndata {
  3239. struct nfs4_delegreturnargs args;
  3240. struct nfs4_delegreturnres res;
  3241. struct nfs_fh fh;
  3242. nfs4_stateid stateid;
  3243. unsigned long timestamp;
  3244. struct nfs_fattr fattr;
  3245. int rpc_status;
  3246. };
  3247. static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
  3248. {
  3249. struct nfs4_delegreturndata *data = calldata;
  3250. nfs4_sequence_done(data->res.server, &data->res.seq_res,
  3251. task->tk_status);
  3252. switch (task->tk_status) {
  3253. case -NFS4ERR_STALE_STATEID:
  3254. case -NFS4ERR_EXPIRED:
  3255. case 0:
  3256. renew_lease(data->res.server, data->timestamp);
  3257. break;
  3258. default:
  3259. if (nfs4_async_handle_error(task, data->res.server, NULL) ==
  3260. -EAGAIN) {
  3261. nfs_restart_rpc(task, data->res.server->nfs_client);
  3262. return;
  3263. }
  3264. }
  3265. data->rpc_status = task->tk_status;
  3266. }
  3267. static void nfs4_delegreturn_release(void *calldata)
  3268. {
  3269. kfree(calldata);
  3270. }
  3271. #if defined(CONFIG_NFS_V4_1)
  3272. static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
  3273. {
  3274. struct nfs4_delegreturndata *d_data;
  3275. d_data = (struct nfs4_delegreturndata *)data;
  3276. if (nfs4_setup_sequence(d_data->res.server->nfs_client,
  3277. &d_data->args.seq_args,
  3278. &d_data->res.seq_res, 1, task))
  3279. return;
  3280. rpc_call_start(task);
  3281. }
  3282. #endif /* CONFIG_NFS_V4_1 */
  3283. static const struct rpc_call_ops nfs4_delegreturn_ops = {
  3284. #if defined(CONFIG_NFS_V4_1)
  3285. .rpc_call_prepare = nfs4_delegreturn_prepare,
  3286. #endif /* CONFIG_NFS_V4_1 */
  3287. .rpc_call_done = nfs4_delegreturn_done,
  3288. .rpc_release = nfs4_delegreturn_release,
  3289. };
  3290. static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
  3291. {
  3292. struct nfs4_delegreturndata *data;
  3293. struct nfs_server *server = NFS_SERVER(inode);
  3294. struct rpc_task *task;
  3295. struct rpc_message msg = {
  3296. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
  3297. .rpc_cred = cred,
  3298. };
  3299. struct rpc_task_setup task_setup_data = {
  3300. .rpc_client = server->client,
  3301. .rpc_message = &msg,
  3302. .callback_ops = &nfs4_delegreturn_ops,
  3303. .flags = RPC_TASK_ASYNC,
  3304. };
  3305. int status = 0;
  3306. data = kzalloc(sizeof(*data), GFP_KERNEL);
  3307. if (data == NULL)
  3308. return -ENOMEM;
  3309. data->args.fhandle = &data->fh;
  3310. data->args.stateid = &data->stateid;
  3311. data->args.bitmask = server->attr_bitmask;
  3312. nfs_copy_fh(&data->fh, NFS_FH(inode));
  3313. memcpy(&data->stateid, stateid, sizeof(data->stateid));
  3314. data->res.fattr = &data->fattr;
  3315. data->res.server = server;
  3316. data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
  3317. nfs_fattr_init(data->res.fattr);
  3318. data->timestamp = jiffies;
  3319. data->rpc_status = 0;
  3320. task_setup_data.callback_data = data;
  3321. msg.rpc_argp = &data->args,
  3322. msg.rpc_resp = &data->res,
  3323. task = rpc_run_task(&task_setup_data);
  3324. if (IS_ERR(task))
  3325. return PTR_ERR(task);
  3326. if (!issync)
  3327. goto out;
  3328. status = nfs4_wait_for_completion_rpc_task(task);
  3329. if (status != 0)
  3330. goto out;
  3331. status = data->rpc_status;
  3332. if (status != 0)
  3333. goto out;
  3334. nfs_refresh_inode(inode, &data->fattr);
  3335. out:
  3336. rpc_put_task(task);
  3337. return status;
  3338. }
  3339. int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
  3340. {
  3341. struct nfs_server *server = NFS_SERVER(inode);
  3342. struct nfs4_exception exception = { };
  3343. int err;
  3344. do {
  3345. err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
  3346. switch (err) {
  3347. case -NFS4ERR_STALE_STATEID:
  3348. case -NFS4ERR_EXPIRED:
  3349. case 0:
  3350. return 0;
  3351. }
  3352. err = nfs4_handle_exception(server, err, &exception);
  3353. } while (exception.retry);
  3354. return err;
  3355. }
  3356. #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
  3357. #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
  3358. /*
  3359. * sleep, with exponential backoff, and retry the LOCK operation.
  3360. */
  3361. static unsigned long
  3362. nfs4_set_lock_task_retry(unsigned long timeout)
  3363. {
  3364. schedule_timeout_killable(timeout);
  3365. timeout <<= 1;
  3366. if (timeout > NFS4_LOCK_MAXTIMEOUT)
  3367. return NFS4_LOCK_MAXTIMEOUT;
  3368. return timeout;
  3369. }
  3370. static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  3371. {
  3372. struct inode *inode = state->inode;
  3373. struct nfs_server *server = NFS_SERVER(inode);
  3374. struct nfs_client *clp = server->nfs_client;
  3375. struct nfs_lockt_args arg = {
  3376. .fh = NFS_FH(inode),
  3377. .fl = request,
  3378. };
  3379. struct nfs_lockt_res res = {
  3380. .denied = request,
  3381. };
  3382. struct rpc_message msg = {
  3383. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
  3384. .rpc_argp = &arg,
  3385. .rpc_resp = &res,
  3386. .rpc_cred = state->owner->so_cred,
  3387. };
  3388. struct nfs4_lock_state *lsp;
  3389. int status;
  3390. arg.lock_owner.clientid = clp->cl_clientid;
  3391. status = nfs4_set_lock_state(state, request);
  3392. if (status != 0)
  3393. goto out;
  3394. lsp = request->fl_u.nfs4_fl.owner;
  3395. arg.lock_owner.id = lsp->ls_id.id;
  3396. status = nfs4_call_sync(server, &msg, &arg, &res, 1);
  3397. switch (status) {
  3398. case 0:
  3399. request->fl_type = F_UNLCK;
  3400. break;
  3401. case -NFS4ERR_DENIED:
  3402. status = 0;
  3403. }
  3404. request->fl_ops->fl_release_private(request);
  3405. out:
  3406. return status;
  3407. }
  3408. static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  3409. {
  3410. struct nfs4_exception exception = { };
  3411. int err;
  3412. do {
  3413. err = nfs4_handle_exception(NFS_SERVER(state->inode),
  3414. _nfs4_proc_getlk(state, cmd, request),
  3415. &exception);
  3416. } while (exception.retry);
  3417. return err;
  3418. }
  3419. static int do_vfs_lock(struct file *file, struct file_lock *fl)
  3420. {
  3421. int res = 0;
  3422. switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
  3423. case FL_POSIX:
  3424. res = posix_lock_file_wait(file, fl);
  3425. break;
  3426. case FL_FLOCK:
  3427. res = flock_lock_file_wait(file, fl);
  3428. break;
  3429. default:
  3430. BUG();
  3431. }
  3432. return res;
  3433. }
  3434. struct nfs4_unlockdata {
  3435. struct nfs_locku_args arg;
  3436. struct nfs_locku_res res;
  3437. struct nfs4_lock_state *lsp;
  3438. struct nfs_open_context *ctx;
  3439. struct file_lock fl;
  3440. const struct nfs_server *server;
  3441. unsigned long timestamp;
  3442. };
  3443. static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
  3444. struct nfs_open_context *ctx,
  3445. struct nfs4_lock_state *lsp,
  3446. struct nfs_seqid *seqid)
  3447. {
  3448. struct nfs4_unlockdata *p;
  3449. struct inode *inode = lsp->ls_state->inode;
  3450. p = kzalloc(sizeof(*p), GFP_KERNEL);
  3451. if (p == NULL)
  3452. return NULL;
  3453. p->arg.fh = NFS_FH(inode);
  3454. p->arg.fl = &p->fl;
  3455. p->arg.seqid = seqid;
  3456. p->res.seqid = seqid;
  3457. p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
  3458. p->arg.stateid = &lsp->ls_stateid;
  3459. p->lsp = lsp;
  3460. atomic_inc(&lsp->ls_count);
  3461. /* Ensure we don't close file until we're done freeing locks! */
  3462. p->ctx = get_nfs_open_context(ctx);
  3463. memcpy(&p->fl, fl, sizeof(p->fl));
  3464. p->server = NFS_SERVER(inode);
  3465. return p;
  3466. }
  3467. static void nfs4_locku_release_calldata(void *data)
  3468. {
  3469. struct nfs4_unlockdata *calldata = data;
  3470. nfs_free_seqid(calldata->arg.seqid);
  3471. nfs4_put_lock_state(calldata->lsp);
  3472. put_nfs_open_context(calldata->ctx);
  3473. kfree(calldata);
  3474. }
  3475. static void nfs4_locku_done(struct rpc_task *task, void *data)
  3476. {
  3477. struct nfs4_unlockdata *calldata = data;
  3478. nfs4_sequence_done(calldata->server, &calldata->res.seq_res,
  3479. task->tk_status);
  3480. if (RPC_ASSASSINATED(task))
  3481. return;
  3482. switch (task->tk_status) {
  3483. case 0:
  3484. memcpy(calldata->lsp->ls_stateid.data,
  3485. calldata->res.stateid.data,
  3486. sizeof(calldata->lsp->ls_stateid.data));
  3487. renew_lease(calldata->server, calldata->timestamp);
  3488. break;
  3489. case -NFS4ERR_BAD_STATEID:
  3490. case -NFS4ERR_OLD_STATEID:
  3491. case -NFS4ERR_STALE_STATEID:
  3492. case -NFS4ERR_EXPIRED:
  3493. break;
  3494. default:
  3495. if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
  3496. nfs_restart_rpc(task,
  3497. calldata->server->nfs_client);
  3498. }
  3499. }
  3500. static void nfs4_locku_prepare(struct rpc_task *task, void *data)
  3501. {
  3502. struct nfs4_unlockdata *calldata = data;
  3503. if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
  3504. return;
  3505. if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
  3506. /* Note: exit _without_ running nfs4_locku_done */
  3507. task->tk_action = NULL;
  3508. return;
  3509. }
  3510. calldata->timestamp = jiffies;
  3511. if (nfs4_setup_sequence(calldata->server->nfs_client,
  3512. &calldata->arg.seq_args,
  3513. &calldata->res.seq_res, 1, task))
  3514. return;
  3515. rpc_call_start(task);
  3516. }
  3517. static const struct rpc_call_ops nfs4_locku_ops = {
  3518. .rpc_call_prepare = nfs4_locku_prepare,
  3519. .rpc_call_done = nfs4_locku_done,
  3520. .rpc_release = nfs4_locku_release_calldata,
  3521. };
  3522. static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
  3523. struct nfs_open_context *ctx,
  3524. struct nfs4_lock_state *lsp,
  3525. struct nfs_seqid *seqid)
  3526. {
  3527. struct nfs4_unlockdata *data;
  3528. struct rpc_message msg = {
  3529. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
  3530. .rpc_cred = ctx->cred,
  3531. };
  3532. struct rpc_task_setup task_setup_data = {
  3533. .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
  3534. .rpc_message = &msg,
  3535. .callback_ops = &nfs4_locku_ops,
  3536. .workqueue = nfsiod_workqueue,
  3537. .flags = RPC_TASK_ASYNC,
  3538. };
  3539. /* Ensure this is an unlock - when canceling a lock, the
  3540. * canceled lock is passed in, and it won't be an unlock.
  3541. */
  3542. fl->fl_type = F_UNLCK;
  3543. data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
  3544. if (data == NULL) {
  3545. nfs_free_seqid(seqid);
  3546. return ERR_PTR(-ENOMEM);
  3547. }
  3548. msg.rpc_argp = &data->arg,
  3549. msg.rpc_resp = &data->res,
  3550. task_setup_data.callback_data = data;
  3551. return rpc_run_task(&task_setup_data);
  3552. }
  3553. static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
  3554. {
  3555. struct nfs_inode *nfsi = NFS_I(state->inode);
  3556. struct nfs_seqid *seqid;
  3557. struct nfs4_lock_state *lsp;
  3558. struct rpc_task *task;
  3559. int status = 0;
  3560. unsigned char fl_flags = request->fl_flags;
  3561. status = nfs4_set_lock_state(state, request);
  3562. /* Unlock _before_ we do the RPC call */
  3563. request->fl_flags |= FL_EXISTS;
  3564. down_read(&nfsi->rwsem);
  3565. if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
  3566. up_read(&nfsi->rwsem);
  3567. goto out;
  3568. }
  3569. up_read(&nfsi->rwsem);
  3570. if (status != 0)
  3571. goto out;
  3572. /* Is this a delegated lock? */
  3573. if (test_bit(NFS_DELEGATED_STATE, &state->flags))
  3574. goto out;
  3575. lsp = request->fl_u.nfs4_fl.owner;
  3576. seqid = nfs_alloc_seqid(&lsp->ls_seqid);
  3577. status = -ENOMEM;
  3578. if (seqid == NULL)
  3579. goto out;
  3580. task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
  3581. status = PTR_ERR(task);
  3582. if (IS_ERR(task))
  3583. goto out;
  3584. status = nfs4_wait_for_completion_rpc_task(task);
  3585. rpc_put_task(task);
  3586. out:
  3587. request->fl_flags = fl_flags;
  3588. return status;
  3589. }
  3590. struct nfs4_lockdata {
  3591. struct nfs_lock_args arg;
  3592. struct nfs_lock_res res;
  3593. struct nfs4_lock_state *lsp;
  3594. struct nfs_open_context *ctx;
  3595. struct file_lock fl;
  3596. unsigned long timestamp;
  3597. int rpc_status;
  3598. int cancelled;
  3599. struct nfs_server *server;
  3600. };
  3601. static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
  3602. struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
  3603. {
  3604. struct nfs4_lockdata *p;
  3605. struct inode *inode = lsp->ls_state->inode;
  3606. struct nfs_server *server = NFS_SERVER(inode);
  3607. p = kzalloc(sizeof(*p), GFP_KERNEL);
  3608. if (p == NULL)
  3609. return NULL;
  3610. p->arg.fh = NFS_FH(inode);
  3611. p->arg.fl = &p->fl;
  3612. p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid);
  3613. if (p->arg.open_seqid == NULL)
  3614. goto out_free;
  3615. p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
  3616. if (p->arg.lock_seqid == NULL)
  3617. goto out_free_seqid;
  3618. p->arg.lock_stateid = &lsp->ls_stateid;
  3619. p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
  3620. p->arg.lock_owner.id = lsp->ls_id.id;
  3621. p->res.lock_seqid = p->arg.lock_seqid;
  3622. p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
  3623. p->lsp = lsp;
  3624. p->server = server;
  3625. atomic_inc(&lsp->ls_count);
  3626. p->ctx = get_nfs_open_context(ctx);
  3627. memcpy(&p->fl, fl, sizeof(p->fl));
  3628. return p;
  3629. out_free_seqid:
  3630. nfs_free_seqid(p->arg.open_seqid);
  3631. out_free:
  3632. kfree(p);
  3633. return NULL;
  3634. }
  3635. static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
  3636. {
  3637. struct nfs4_lockdata *data = calldata;
  3638. struct nfs4_state *state = data->lsp->ls_state;
  3639. dprintk("%s: begin!\n", __func__);
  3640. if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
  3641. return;
  3642. /* Do we need to do an open_to_lock_owner? */
  3643. if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
  3644. if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
  3645. return;
  3646. data->arg.open_stateid = &state->stateid;
  3647. data->arg.new_lock_owner = 1;
  3648. data->res.open_seqid = data->arg.open_seqid;
  3649. } else
  3650. data->arg.new_lock_owner = 0;
  3651. data->timestamp = jiffies;
  3652. if (nfs4_setup_sequence(data->server->nfs_client, &data->arg.seq_args,
  3653. &data->res.seq_res, 1, task))
  3654. return;
  3655. rpc_call_start(task);
  3656. dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
  3657. }
  3658. static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
  3659. {
  3660. rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
  3661. nfs4_lock_prepare(task, calldata);
  3662. }
  3663. static void nfs4_lock_done(struct rpc_task *task, void *calldata)
  3664. {
  3665. struct nfs4_lockdata *data = calldata;
  3666. dprintk("%s: begin!\n", __func__);
  3667. nfs4_sequence_done(data->server, &data->res.seq_res,
  3668. task->tk_status);
  3669. data->rpc_status = task->tk_status;
  3670. if (RPC_ASSASSINATED(task))
  3671. goto out;
  3672. if (data->arg.new_lock_owner != 0) {
  3673. if (data->rpc_status == 0)
  3674. nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
  3675. else
  3676. goto out;
  3677. }
  3678. if (data->rpc_status == 0) {
  3679. memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
  3680. sizeof(data->lsp->ls_stateid.data));
  3681. data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
  3682. renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
  3683. }
  3684. out:
  3685. dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
  3686. }
  3687. static void nfs4_lock_release(void *calldata)
  3688. {
  3689. struct nfs4_lockdata *data = calldata;
  3690. dprintk("%s: begin!\n", __func__);
  3691. nfs_free_seqid(data->arg.open_seqid);
  3692. if (data->cancelled != 0) {
  3693. struct rpc_task *task;
  3694. task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
  3695. data->arg.lock_seqid);
  3696. if (!IS_ERR(task))
  3697. rpc_put_task(task);
  3698. dprintk("%s: cancelling lock!\n", __func__);
  3699. } else
  3700. nfs_free_seqid(data->arg.lock_seqid);
  3701. nfs4_put_lock_state(data->lsp);
  3702. put_nfs_open_context(data->ctx);
  3703. kfree(data);
  3704. dprintk("%s: done!\n", __func__);
  3705. }
  3706. static const struct rpc_call_ops nfs4_lock_ops = {
  3707. .rpc_call_prepare = nfs4_lock_prepare,
  3708. .rpc_call_done = nfs4_lock_done,
  3709. .rpc_release = nfs4_lock_release,
  3710. };
  3711. static const struct rpc_call_ops nfs4_recover_lock_ops = {
  3712. .rpc_call_prepare = nfs4_recover_lock_prepare,
  3713. .rpc_call_done = nfs4_lock_done,
  3714. .rpc_release = nfs4_lock_release,
  3715. };
  3716. static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
  3717. {
  3718. struct nfs_client *clp = server->nfs_client;
  3719. struct nfs4_state *state = lsp->ls_state;
  3720. switch (error) {
  3721. case -NFS4ERR_ADMIN_REVOKED:
  3722. case -NFS4ERR_BAD_STATEID:
  3723. case -NFS4ERR_EXPIRED:
  3724. if (new_lock_owner != 0 ||
  3725. (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
  3726. nfs4_state_mark_reclaim_nograce(clp, state);
  3727. lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
  3728. break;
  3729. case -NFS4ERR_STALE_STATEID:
  3730. if (new_lock_owner != 0 ||
  3731. (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
  3732. nfs4_state_mark_reclaim_reboot(clp, state);
  3733. lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
  3734. };
  3735. }
  3736. static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
  3737. {
  3738. struct nfs4_lockdata *data;
  3739. struct rpc_task *task;
  3740. struct rpc_message msg = {
  3741. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
  3742. .rpc_cred = state->owner->so_cred,
  3743. };
  3744. struct rpc_task_setup task_setup_data = {
  3745. .rpc_client = NFS_CLIENT(state->inode),
  3746. .rpc_message = &msg,
  3747. .callback_ops = &nfs4_lock_ops,
  3748. .workqueue = nfsiod_workqueue,
  3749. .flags = RPC_TASK_ASYNC,
  3750. };
  3751. int ret;
  3752. dprintk("%s: begin!\n", __func__);
  3753. data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
  3754. fl->fl_u.nfs4_fl.owner);
  3755. if (data == NULL)
  3756. return -ENOMEM;
  3757. if (IS_SETLKW(cmd))
  3758. data->arg.block = 1;
  3759. if (recovery_type > NFS_LOCK_NEW) {
  3760. if (recovery_type == NFS_LOCK_RECLAIM)
  3761. data->arg.reclaim = NFS_LOCK_RECLAIM;
  3762. task_setup_data.callback_ops = &nfs4_recover_lock_ops;
  3763. }
  3764. msg.rpc_argp = &data->arg,
  3765. msg.rpc_resp = &data->res,
  3766. task_setup_data.callback_data = data;
  3767. task = rpc_run_task(&task_setup_data);
  3768. if (IS_ERR(task))
  3769. return PTR_ERR(task);
  3770. ret = nfs4_wait_for_completion_rpc_task(task);
  3771. if (ret == 0) {
  3772. ret = data->rpc_status;
  3773. if (ret)
  3774. nfs4_handle_setlk_error(data->server, data->lsp,
  3775. data->arg.new_lock_owner, ret);
  3776. } else
  3777. data->cancelled = 1;
  3778. rpc_put_task(task);
  3779. dprintk("%s: done, ret = %d!\n", __func__, ret);
  3780. return ret;
  3781. }
  3782. static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
  3783. {
  3784. struct nfs_server *server = NFS_SERVER(state->inode);
  3785. struct nfs4_exception exception = { };
  3786. int err;
  3787. do {
  3788. /* Cache the lock if possible... */
  3789. if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
  3790. return 0;
  3791. err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
  3792. if (err != -NFS4ERR_DELAY && err != -EKEYEXPIRED)
  3793. break;
  3794. nfs4_handle_exception(server, err, &exception);
  3795. } while (exception.retry);
  3796. return err;
  3797. }
  3798. static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
  3799. {
  3800. struct nfs_server *server = NFS_SERVER(state->inode);
  3801. struct nfs4_exception exception = { };
  3802. int err;
  3803. err = nfs4_set_lock_state(state, request);
  3804. if (err != 0)
  3805. return err;
  3806. do {
  3807. if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
  3808. return 0;
  3809. err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
  3810. switch (err) {
  3811. default:
  3812. goto out;
  3813. case -NFS4ERR_GRACE:
  3814. case -NFS4ERR_DELAY:
  3815. case -EKEYEXPIRED:
  3816. nfs4_handle_exception(server, err, &exception);
  3817. err = 0;
  3818. }
  3819. } while (exception.retry);
  3820. out:
  3821. return err;
  3822. }
  3823. static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  3824. {
  3825. struct nfs_inode *nfsi = NFS_I(state->inode);
  3826. unsigned char fl_flags = request->fl_flags;
  3827. int status = -ENOLCK;
  3828. if ((fl_flags & FL_POSIX) &&
  3829. !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
  3830. goto out;
  3831. /* Is this a delegated open? */
  3832. status = nfs4_set_lock_state(state, request);
  3833. if (status != 0)
  3834. goto out;
  3835. request->fl_flags |= FL_ACCESS;
  3836. status = do_vfs_lock(request->fl_file, request);
  3837. if (status < 0)
  3838. goto out;
  3839. down_read(&nfsi->rwsem);
  3840. if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
  3841. /* Yes: cache locks! */
  3842. /* ...but avoid races with delegation recall... */
  3843. request->fl_flags = fl_flags & ~FL_SLEEP;
  3844. status = do_vfs_lock(request->fl_file, request);
  3845. goto out_unlock;
  3846. }
  3847. status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
  3848. if (status != 0)
  3849. goto out_unlock;
  3850. /* Note: we always want to sleep here! */
  3851. request->fl_flags = fl_flags | FL_SLEEP;
  3852. if (do_vfs_lock(request->fl_file, request) < 0)
  3853. printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
  3854. out_unlock:
  3855. up_read(&nfsi->rwsem);
  3856. out:
  3857. request->fl_flags = fl_flags;
  3858. return status;
  3859. }
  3860. static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  3861. {
  3862. struct nfs4_exception exception = { };
  3863. int err;
  3864. do {
  3865. err = _nfs4_proc_setlk(state, cmd, request);
  3866. if (err == -NFS4ERR_DENIED)
  3867. err = -EAGAIN;
  3868. err = nfs4_handle_exception(NFS_SERVER(state->inode),
  3869. err, &exception);
  3870. } while (exception.retry);
  3871. return err;
  3872. }
  3873. static int
  3874. nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
  3875. {
  3876. struct nfs_open_context *ctx;
  3877. struct nfs4_state *state;
  3878. unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
  3879. int status;
  3880. /* verify open state */
  3881. ctx = nfs_file_open_context(filp);
  3882. state = ctx->state;
  3883. if (request->fl_start < 0 || request->fl_end < 0)
  3884. return -EINVAL;
  3885. if (IS_GETLK(cmd)) {
  3886. if (state != NULL)
  3887. return nfs4_proc_getlk(state, F_GETLK, request);
  3888. return 0;
  3889. }
  3890. if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
  3891. return -EINVAL;
  3892. if (request->fl_type == F_UNLCK) {
  3893. if (state != NULL)
  3894. return nfs4_proc_unlck(state, cmd, request);
  3895. return 0;
  3896. }
  3897. if (state == NULL)
  3898. return -ENOLCK;
  3899. do {
  3900. status = nfs4_proc_setlk(state, cmd, request);
  3901. if ((status != -EAGAIN) || IS_SETLK(cmd))
  3902. break;
  3903. timeout = nfs4_set_lock_task_retry(timeout);
  3904. status = -ERESTARTSYS;
  3905. if (signalled())
  3906. break;
  3907. } while(status < 0);
  3908. return status;
  3909. }
  3910. int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
  3911. {
  3912. struct nfs_server *server = NFS_SERVER(state->inode);
  3913. struct nfs4_exception exception = { };
  3914. int err;
  3915. err = nfs4_set_lock_state(state, fl);
  3916. if (err != 0)
  3917. goto out;
  3918. do {
  3919. err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
  3920. switch (err) {
  3921. default:
  3922. printk(KERN_ERR "%s: unhandled error %d.\n",
  3923. __func__, err);
  3924. case 0:
  3925. case -ESTALE:
  3926. goto out;
  3927. case -NFS4ERR_EXPIRED:
  3928. case -NFS4ERR_STALE_CLIENTID:
  3929. case -NFS4ERR_STALE_STATEID:
  3930. case -NFS4ERR_BADSESSION:
  3931. case -NFS4ERR_BADSLOT:
  3932. case -NFS4ERR_BAD_HIGH_SLOT:
  3933. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  3934. case -NFS4ERR_DEADSESSION:
  3935. nfs4_schedule_state_recovery(server->nfs_client);
  3936. goto out;
  3937. case -ERESTARTSYS:
  3938. /*
  3939. * The show must go on: exit, but mark the
  3940. * stateid as needing recovery.
  3941. */
  3942. case -NFS4ERR_ADMIN_REVOKED:
  3943. case -NFS4ERR_BAD_STATEID:
  3944. case -NFS4ERR_OPENMODE:
  3945. nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
  3946. err = 0;
  3947. goto out;
  3948. case -ENOMEM:
  3949. case -NFS4ERR_DENIED:
  3950. /* kill_proc(fl->fl_pid, SIGLOST, 1); */
  3951. err = 0;
  3952. goto out;
  3953. case -NFS4ERR_DELAY:
  3954. case -EKEYEXPIRED:
  3955. break;
  3956. }
  3957. err = nfs4_handle_exception(server, err, &exception);
  3958. } while (exception.retry);
  3959. out:
  3960. return err;
  3961. }
  3962. #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
  3963. int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
  3964. size_t buflen, int flags)
  3965. {
  3966. struct inode *inode = dentry->d_inode;
  3967. if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
  3968. return -EOPNOTSUPP;
  3969. return nfs4_proc_set_acl(inode, buf, buflen);
  3970. }
  3971. /* The getxattr man page suggests returning -ENODATA for unknown attributes,
  3972. * and that's what we'll do for e.g. user attributes that haven't been set.
  3973. * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
  3974. * attributes in kernel-managed attribute namespaces. */
  3975. ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
  3976. size_t buflen)
  3977. {
  3978. struct inode *inode = dentry->d_inode;
  3979. if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
  3980. return -EOPNOTSUPP;
  3981. return nfs4_proc_get_acl(inode, buf, buflen);
  3982. }
  3983. ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
  3984. {
  3985. size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
  3986. if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
  3987. return 0;
  3988. if (buf && buflen < len)
  3989. return -ERANGE;
  3990. if (buf)
  3991. memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
  3992. return len;
  3993. }
  3994. static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
  3995. {
  3996. if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
  3997. (fattr->valid & NFS_ATTR_FATTR_FSID) &&
  3998. (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
  3999. return;
  4000. fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
  4001. NFS_ATTR_FATTR_NLINK;
  4002. fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
  4003. fattr->nlink = 2;
  4004. }
  4005. int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
  4006. struct nfs4_fs_locations *fs_locations, struct page *page)
  4007. {
  4008. struct nfs_server *server = NFS_SERVER(dir);
  4009. u32 bitmask[2] = {
  4010. [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
  4011. [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
  4012. };
  4013. struct nfs4_fs_locations_arg args = {
  4014. .dir_fh = NFS_FH(dir),
  4015. .name = name,
  4016. .page = page,
  4017. .bitmask = bitmask,
  4018. };
  4019. struct nfs4_fs_locations_res res = {
  4020. .fs_locations = fs_locations,
  4021. };
  4022. struct rpc_message msg = {
  4023. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
  4024. .rpc_argp = &args,
  4025. .rpc_resp = &res,
  4026. };
  4027. int status;
  4028. dprintk("%s: start\n", __func__);
  4029. nfs_fattr_init(&fs_locations->fattr);
  4030. fs_locations->server = server;
  4031. fs_locations->nlocations = 0;
  4032. status = nfs4_call_sync(server, &msg, &args, &res, 0);
  4033. nfs_fixup_referral_attributes(&fs_locations->fattr);
  4034. dprintk("%s: returned status = %d\n", __func__, status);
  4035. return status;
  4036. }
  4037. #ifdef CONFIG_NFS_V4_1
  4038. /*
  4039. * nfs4_proc_exchange_id()
  4040. *
  4041. * Since the clientid has expired, all compounds using sessions
  4042. * associated with the stale clientid will be returning
  4043. * NFS4ERR_BADSESSION in the sequence operation, and will therefore
  4044. * be in some phase of session reset.
  4045. */
  4046. int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
  4047. {
  4048. nfs4_verifier verifier;
  4049. struct nfs41_exchange_id_args args = {
  4050. .client = clp,
  4051. .flags = clp->cl_exchange_flags,
  4052. };
  4053. struct nfs41_exchange_id_res res = {
  4054. .client = clp,
  4055. };
  4056. int status;
  4057. struct rpc_message msg = {
  4058. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
  4059. .rpc_argp = &args,
  4060. .rpc_resp = &res,
  4061. .rpc_cred = cred,
  4062. };
  4063. __be32 *p;
  4064. dprintk("--> %s\n", __func__);
  4065. BUG_ON(clp == NULL);
  4066. /* Remove server-only flags */
  4067. args.flags &= ~EXCHGID4_FLAG_CONFIRMED_R;
  4068. p = (u32 *)verifier.data;
  4069. *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
  4070. *p = htonl((u32)clp->cl_boot_time.tv_nsec);
  4071. args.verifier = &verifier;
  4072. while (1) {
  4073. args.id_len = scnprintf(args.id, sizeof(args.id),
  4074. "%s/%s %u",
  4075. clp->cl_ipaddr,
  4076. rpc_peeraddr2str(clp->cl_rpcclient,
  4077. RPC_DISPLAY_ADDR),
  4078. clp->cl_id_uniquifier);
  4079. status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
  4080. if (status != -NFS4ERR_CLID_INUSE)
  4081. break;
  4082. if (signalled())
  4083. break;
  4084. if (++clp->cl_id_uniquifier == 0)
  4085. break;
  4086. }
  4087. dprintk("<-- %s status= %d\n", __func__, status);
  4088. return status;
  4089. }
  4090. struct nfs4_get_lease_time_data {
  4091. struct nfs4_get_lease_time_args *args;
  4092. struct nfs4_get_lease_time_res *res;
  4093. struct nfs_client *clp;
  4094. };
  4095. static void nfs4_get_lease_time_prepare(struct rpc_task *task,
  4096. void *calldata)
  4097. {
  4098. int ret;
  4099. struct nfs4_get_lease_time_data *data =
  4100. (struct nfs4_get_lease_time_data *)calldata;
  4101. dprintk("--> %s\n", __func__);
  4102. rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
  4103. /* just setup sequence, do not trigger session recovery
  4104. since we're invoked within one */
  4105. ret = nfs41_setup_sequence(data->clp->cl_session,
  4106. &data->args->la_seq_args,
  4107. &data->res->lr_seq_res, 0, task);
  4108. BUG_ON(ret == -EAGAIN);
  4109. rpc_call_start(task);
  4110. dprintk("<-- %s\n", __func__);
  4111. }
  4112. /*
  4113. * Called from nfs4_state_manager thread for session setup, so don't recover
  4114. * from sequence operation or clientid errors.
  4115. */
  4116. static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
  4117. {
  4118. struct nfs4_get_lease_time_data *data =
  4119. (struct nfs4_get_lease_time_data *)calldata;
  4120. dprintk("--> %s\n", __func__);
  4121. nfs41_sequence_done(data->clp, &data->res->lr_seq_res, task->tk_status);
  4122. switch (task->tk_status) {
  4123. case -NFS4ERR_DELAY:
  4124. case -NFS4ERR_GRACE:
  4125. case -EKEYEXPIRED:
  4126. dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
  4127. rpc_delay(task, NFS4_POLL_RETRY_MIN);
  4128. task->tk_status = 0;
  4129. nfs_restart_rpc(task, data->clp);
  4130. return;
  4131. }
  4132. dprintk("<-- %s\n", __func__);
  4133. }
  4134. struct rpc_call_ops nfs4_get_lease_time_ops = {
  4135. .rpc_call_prepare = nfs4_get_lease_time_prepare,
  4136. .rpc_call_done = nfs4_get_lease_time_done,
  4137. };
  4138. int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
  4139. {
  4140. struct rpc_task *task;
  4141. struct nfs4_get_lease_time_args args;
  4142. struct nfs4_get_lease_time_res res = {
  4143. .lr_fsinfo = fsinfo,
  4144. };
  4145. struct nfs4_get_lease_time_data data = {
  4146. .args = &args,
  4147. .res = &res,
  4148. .clp = clp,
  4149. };
  4150. struct rpc_message msg = {
  4151. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
  4152. .rpc_argp = &args,
  4153. .rpc_resp = &res,
  4154. };
  4155. struct rpc_task_setup task_setup = {
  4156. .rpc_client = clp->cl_rpcclient,
  4157. .rpc_message = &msg,
  4158. .callback_ops = &nfs4_get_lease_time_ops,
  4159. .callback_data = &data
  4160. };
  4161. int status;
  4162. res.lr_seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
  4163. dprintk("--> %s\n", __func__);
  4164. task = rpc_run_task(&task_setup);
  4165. if (IS_ERR(task))
  4166. status = PTR_ERR(task);
  4167. else {
  4168. status = task->tk_status;
  4169. rpc_put_task(task);
  4170. }
  4171. dprintk("<-- %s return %d\n", __func__, status);
  4172. return status;
  4173. }
  4174. /*
  4175. * Reset a slot table
  4176. */
  4177. static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
  4178. int ivalue)
  4179. {
  4180. struct nfs4_slot *new = NULL;
  4181. int i;
  4182. int ret = 0;
  4183. dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
  4184. max_reqs, tbl->max_slots);
  4185. /* Does the newly negotiated max_reqs match the existing slot table? */
  4186. if (max_reqs != tbl->max_slots) {
  4187. ret = -ENOMEM;
  4188. new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
  4189. GFP_KERNEL);
  4190. if (!new)
  4191. goto out;
  4192. ret = 0;
  4193. kfree(tbl->slots);
  4194. }
  4195. spin_lock(&tbl->slot_tbl_lock);
  4196. if (new) {
  4197. tbl->slots = new;
  4198. tbl->max_slots = max_reqs;
  4199. }
  4200. for (i = 0; i < tbl->max_slots; ++i)
  4201. tbl->slots[i].seq_nr = ivalue;
  4202. spin_unlock(&tbl->slot_tbl_lock);
  4203. dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
  4204. tbl, tbl->slots, tbl->max_slots);
  4205. out:
  4206. dprintk("<-- %s: return %d\n", __func__, ret);
  4207. return ret;
  4208. }
  4209. /*
  4210. * Reset the forechannel and backchannel slot tables
  4211. */
  4212. static int nfs4_reset_slot_tables(struct nfs4_session *session)
  4213. {
  4214. int status;
  4215. status = nfs4_reset_slot_table(&session->fc_slot_table,
  4216. session->fc_attrs.max_reqs, 1);
  4217. if (status)
  4218. return status;
  4219. status = nfs4_reset_slot_table(&session->bc_slot_table,
  4220. session->bc_attrs.max_reqs, 0);
  4221. return status;
  4222. }
  4223. /* Destroy the slot table */
  4224. static void nfs4_destroy_slot_tables(struct nfs4_session *session)
  4225. {
  4226. if (session->fc_slot_table.slots != NULL) {
  4227. kfree(session->fc_slot_table.slots);
  4228. session->fc_slot_table.slots = NULL;
  4229. }
  4230. if (session->bc_slot_table.slots != NULL) {
  4231. kfree(session->bc_slot_table.slots);
  4232. session->bc_slot_table.slots = NULL;
  4233. }
  4234. return;
  4235. }
  4236. /*
  4237. * Initialize slot table
  4238. */
  4239. static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
  4240. int max_slots, int ivalue)
  4241. {
  4242. struct nfs4_slot *slot;
  4243. int ret = -ENOMEM;
  4244. BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
  4245. dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
  4246. slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_KERNEL);
  4247. if (!slot)
  4248. goto out;
  4249. ret = 0;
  4250. spin_lock(&tbl->slot_tbl_lock);
  4251. tbl->max_slots = max_slots;
  4252. tbl->slots = slot;
  4253. tbl->highest_used_slotid = -1; /* no slot is currently used */
  4254. spin_unlock(&tbl->slot_tbl_lock);
  4255. dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
  4256. tbl, tbl->slots, tbl->max_slots);
  4257. out:
  4258. dprintk("<-- %s: return %d\n", __func__, ret);
  4259. return ret;
  4260. }
  4261. /*
  4262. * Initialize the forechannel and backchannel tables
  4263. */
  4264. static int nfs4_init_slot_tables(struct nfs4_session *session)
  4265. {
  4266. struct nfs4_slot_table *tbl;
  4267. int status = 0;
  4268. tbl = &session->fc_slot_table;
  4269. if (tbl->slots == NULL) {
  4270. status = nfs4_init_slot_table(tbl,
  4271. session->fc_attrs.max_reqs, 1);
  4272. if (status)
  4273. return status;
  4274. }
  4275. tbl = &session->bc_slot_table;
  4276. if (tbl->slots == NULL) {
  4277. status = nfs4_init_slot_table(tbl,
  4278. session->bc_attrs.max_reqs, 0);
  4279. if (status)
  4280. nfs4_destroy_slot_tables(session);
  4281. }
  4282. return status;
  4283. }
  4284. struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
  4285. {
  4286. struct nfs4_session *session;
  4287. struct nfs4_slot_table *tbl;
  4288. session = kzalloc(sizeof(struct nfs4_session), GFP_KERNEL);
  4289. if (!session)
  4290. return NULL;
  4291. /*
  4292. * The create session reply races with the server back
  4293. * channel probe. Mark the client NFS_CS_SESSION_INITING
  4294. * so that the client back channel can find the
  4295. * nfs_client struct
  4296. */
  4297. clp->cl_cons_state = NFS_CS_SESSION_INITING;
  4298. init_completion(&session->complete);
  4299. tbl = &session->fc_slot_table;
  4300. tbl->highest_used_slotid = -1;
  4301. spin_lock_init(&tbl->slot_tbl_lock);
  4302. rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
  4303. tbl = &session->bc_slot_table;
  4304. tbl->highest_used_slotid = -1;
  4305. spin_lock_init(&tbl->slot_tbl_lock);
  4306. rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
  4307. session->clp = clp;
  4308. return session;
  4309. }
  4310. void nfs4_destroy_session(struct nfs4_session *session)
  4311. {
  4312. nfs4_proc_destroy_session(session);
  4313. dprintk("%s Destroy backchannel for xprt %p\n",
  4314. __func__, session->clp->cl_rpcclient->cl_xprt);
  4315. xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
  4316. NFS41_BC_MIN_CALLBACKS);
  4317. nfs4_destroy_slot_tables(session);
  4318. kfree(session);
  4319. }
  4320. /*
  4321. * Initialize the values to be used by the client in CREATE_SESSION
  4322. * If nfs4_init_session set the fore channel request and response sizes,
  4323. * use them.
  4324. *
  4325. * Set the back channel max_resp_sz_cached to zero to force the client to
  4326. * always set csa_cachethis to FALSE because the current implementation
  4327. * of the back channel DRC only supports caching the CB_SEQUENCE operation.
  4328. */
  4329. static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
  4330. {
  4331. struct nfs4_session *session = args->client->cl_session;
  4332. unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
  4333. mxresp_sz = session->fc_attrs.max_resp_sz;
  4334. if (mxrqst_sz == 0)
  4335. mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
  4336. if (mxresp_sz == 0)
  4337. mxresp_sz = NFS_MAX_FILE_IO_SIZE;
  4338. /* Fore channel attributes */
  4339. args->fc_attrs.headerpadsz = 0;
  4340. args->fc_attrs.max_rqst_sz = mxrqst_sz;
  4341. args->fc_attrs.max_resp_sz = mxresp_sz;
  4342. args->fc_attrs.max_ops = NFS4_MAX_OPS;
  4343. args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
  4344. dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
  4345. "max_ops=%u max_reqs=%u\n",
  4346. __func__,
  4347. args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
  4348. args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
  4349. /* Back channel attributes */
  4350. args->bc_attrs.headerpadsz = 0;
  4351. args->bc_attrs.max_rqst_sz = PAGE_SIZE;
  4352. args->bc_attrs.max_resp_sz = PAGE_SIZE;
  4353. args->bc_attrs.max_resp_sz_cached = 0;
  4354. args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
  4355. args->bc_attrs.max_reqs = 1;
  4356. dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
  4357. "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
  4358. __func__,
  4359. args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
  4360. args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
  4361. args->bc_attrs.max_reqs);
  4362. }
  4363. static int _verify_channel_attr(char *chan, char *attr_name, u32 sent, u32 rcvd)
  4364. {
  4365. if (rcvd <= sent)
  4366. return 0;
  4367. printk(KERN_WARNING "%s: Session INVALID: %s channel %s increased. "
  4368. "sent=%u rcvd=%u\n", __func__, chan, attr_name, sent, rcvd);
  4369. return -EINVAL;
  4370. }
  4371. #define _verify_fore_channel_attr(_name_) \
  4372. _verify_channel_attr("fore", #_name_, \
  4373. args->fc_attrs._name_, \
  4374. session->fc_attrs._name_)
  4375. #define _verify_back_channel_attr(_name_) \
  4376. _verify_channel_attr("back", #_name_, \
  4377. args->bc_attrs._name_, \
  4378. session->bc_attrs._name_)
  4379. /*
  4380. * The server is not allowed to increase the fore channel header pad size,
  4381. * maximum response size, or maximum number of operations.
  4382. *
  4383. * The back channel attributes are only negotiatied down: We send what the
  4384. * (back channel) server insists upon.
  4385. */
  4386. static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
  4387. struct nfs4_session *session)
  4388. {
  4389. int ret = 0;
  4390. ret |= _verify_fore_channel_attr(headerpadsz);
  4391. ret |= _verify_fore_channel_attr(max_resp_sz);
  4392. ret |= _verify_fore_channel_attr(max_ops);
  4393. ret |= _verify_back_channel_attr(headerpadsz);
  4394. ret |= _verify_back_channel_attr(max_rqst_sz);
  4395. ret |= _verify_back_channel_attr(max_resp_sz);
  4396. ret |= _verify_back_channel_attr(max_resp_sz_cached);
  4397. ret |= _verify_back_channel_attr(max_ops);
  4398. ret |= _verify_back_channel_attr(max_reqs);
  4399. return ret;
  4400. }
  4401. static int _nfs4_proc_create_session(struct nfs_client *clp)
  4402. {
  4403. struct nfs4_session *session = clp->cl_session;
  4404. struct nfs41_create_session_args args = {
  4405. .client = clp,
  4406. .cb_program = NFS4_CALLBACK,
  4407. };
  4408. struct nfs41_create_session_res res = {
  4409. .client = clp,
  4410. };
  4411. struct rpc_message msg = {
  4412. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
  4413. .rpc_argp = &args,
  4414. .rpc_resp = &res,
  4415. };
  4416. int status;
  4417. nfs4_init_channel_attrs(&args);
  4418. args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
  4419. status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
  4420. if (!status)
  4421. /* Verify the session's negotiated channel_attrs values */
  4422. status = nfs4_verify_channel_attrs(&args, session);
  4423. if (!status) {
  4424. /* Increment the clientid slot sequence id */
  4425. clp->cl_seqid++;
  4426. }
  4427. return status;
  4428. }
  4429. /*
  4430. * Issues a CREATE_SESSION operation to the server.
  4431. * It is the responsibility of the caller to verify the session is
  4432. * expired before calling this routine.
  4433. */
  4434. int nfs4_proc_create_session(struct nfs_client *clp)
  4435. {
  4436. int status;
  4437. unsigned *ptr;
  4438. struct nfs4_session *session = clp->cl_session;
  4439. dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
  4440. status = _nfs4_proc_create_session(clp);
  4441. if (status)
  4442. goto out;
  4443. /* Init and reset the fore channel */
  4444. status = nfs4_init_slot_tables(session);
  4445. dprintk("slot table initialization returned %d\n", status);
  4446. if (status)
  4447. goto out;
  4448. status = nfs4_reset_slot_tables(session);
  4449. dprintk("slot table reset returned %d\n", status);
  4450. if (status)
  4451. goto out;
  4452. ptr = (unsigned *)&session->sess_id.data[0];
  4453. dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
  4454. clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
  4455. out:
  4456. dprintk("<-- %s\n", __func__);
  4457. return status;
  4458. }
  4459. /*
  4460. * Issue the over-the-wire RPC DESTROY_SESSION.
  4461. * The caller must serialize access to this routine.
  4462. */
  4463. int nfs4_proc_destroy_session(struct nfs4_session *session)
  4464. {
  4465. int status = 0;
  4466. struct rpc_message msg;
  4467. dprintk("--> nfs4_proc_destroy_session\n");
  4468. /* session is still being setup */
  4469. if (session->clp->cl_cons_state != NFS_CS_READY)
  4470. return status;
  4471. msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
  4472. msg.rpc_argp = session;
  4473. msg.rpc_resp = NULL;
  4474. msg.rpc_cred = NULL;
  4475. status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
  4476. if (status)
  4477. printk(KERN_WARNING
  4478. "Got error %d from the server on DESTROY_SESSION. "
  4479. "Session has been destroyed regardless...\n", status);
  4480. dprintk("<-- nfs4_proc_destroy_session\n");
  4481. return status;
  4482. }
  4483. int nfs4_init_session(struct nfs_server *server)
  4484. {
  4485. struct nfs_client *clp = server->nfs_client;
  4486. struct nfs4_session *session;
  4487. unsigned int rsize, wsize;
  4488. int ret;
  4489. if (!nfs4_has_session(clp))
  4490. return 0;
  4491. rsize = server->rsize;
  4492. if (rsize == 0)
  4493. rsize = NFS_MAX_FILE_IO_SIZE;
  4494. wsize = server->wsize;
  4495. if (wsize == 0)
  4496. wsize = NFS_MAX_FILE_IO_SIZE;
  4497. session = clp->cl_session;
  4498. session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
  4499. session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
  4500. ret = nfs4_recover_expired_lease(server);
  4501. if (!ret)
  4502. ret = nfs4_check_client_ready(clp);
  4503. return ret;
  4504. }
  4505. /*
  4506. * Renew the cl_session lease.
  4507. */
  4508. static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
  4509. {
  4510. struct nfs4_sequence_args args;
  4511. struct nfs4_sequence_res res;
  4512. struct rpc_message msg = {
  4513. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
  4514. .rpc_argp = &args,
  4515. .rpc_resp = &res,
  4516. .rpc_cred = cred,
  4517. };
  4518. args.sa_cache_this = 0;
  4519. return nfs4_call_sync_sequence(clp, clp->cl_rpcclient, &msg, &args,
  4520. &res, args.sa_cache_this, 1);
  4521. }
  4522. static void nfs41_sequence_release(void *data)
  4523. {
  4524. struct nfs_client *clp = (struct nfs_client *)data;
  4525. if (atomic_read(&clp->cl_count) > 1)
  4526. nfs4_schedule_state_renewal(clp);
  4527. nfs_put_client(clp);
  4528. }
  4529. static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
  4530. {
  4531. struct nfs_client *clp = (struct nfs_client *)data;
  4532. nfs41_sequence_done(clp, task->tk_msg.rpc_resp, task->tk_status);
  4533. if (task->tk_status < 0) {
  4534. dprintk("%s ERROR %d\n", __func__, task->tk_status);
  4535. if (atomic_read(&clp->cl_count) == 1)
  4536. goto out;
  4537. if (_nfs4_async_handle_error(task, NULL, clp, NULL)
  4538. == -EAGAIN) {
  4539. nfs_restart_rpc(task, clp);
  4540. return;
  4541. }
  4542. }
  4543. dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
  4544. out:
  4545. kfree(task->tk_msg.rpc_argp);
  4546. kfree(task->tk_msg.rpc_resp);
  4547. dprintk("<-- %s\n", __func__);
  4548. }
  4549. static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
  4550. {
  4551. struct nfs_client *clp;
  4552. struct nfs4_sequence_args *args;
  4553. struct nfs4_sequence_res *res;
  4554. clp = (struct nfs_client *)data;
  4555. args = task->tk_msg.rpc_argp;
  4556. res = task->tk_msg.rpc_resp;
  4557. if (nfs4_setup_sequence(clp, args, res, 0, task))
  4558. return;
  4559. rpc_call_start(task);
  4560. }
  4561. static const struct rpc_call_ops nfs41_sequence_ops = {
  4562. .rpc_call_done = nfs41_sequence_call_done,
  4563. .rpc_call_prepare = nfs41_sequence_prepare,
  4564. .rpc_release = nfs41_sequence_release,
  4565. };
  4566. static int nfs41_proc_async_sequence(struct nfs_client *clp,
  4567. struct rpc_cred *cred)
  4568. {
  4569. struct nfs4_sequence_args *args;
  4570. struct nfs4_sequence_res *res;
  4571. struct rpc_message msg = {
  4572. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
  4573. .rpc_cred = cred,
  4574. };
  4575. if (!atomic_inc_not_zero(&clp->cl_count))
  4576. return -EIO;
  4577. args = kzalloc(sizeof(*args), GFP_KERNEL);
  4578. res = kzalloc(sizeof(*res), GFP_KERNEL);
  4579. if (!args || !res) {
  4580. kfree(args);
  4581. kfree(res);
  4582. nfs_put_client(clp);
  4583. return -ENOMEM;
  4584. }
  4585. res->sr_slotid = NFS4_MAX_SLOT_TABLE;
  4586. msg.rpc_argp = args;
  4587. msg.rpc_resp = res;
  4588. return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
  4589. &nfs41_sequence_ops, (void *)clp);
  4590. }
  4591. struct nfs4_reclaim_complete_data {
  4592. struct nfs_client *clp;
  4593. struct nfs41_reclaim_complete_args arg;
  4594. struct nfs41_reclaim_complete_res res;
  4595. };
  4596. static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
  4597. {
  4598. struct nfs4_reclaim_complete_data *calldata = data;
  4599. rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
  4600. if (nfs4_setup_sequence(calldata->clp, &calldata->arg.seq_args,
  4601. &calldata->res.seq_res, 0, task))
  4602. return;
  4603. rpc_call_start(task);
  4604. }
  4605. static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
  4606. {
  4607. struct nfs4_reclaim_complete_data *calldata = data;
  4608. struct nfs_client *clp = calldata->clp;
  4609. struct nfs4_sequence_res *res = &calldata->res.seq_res;
  4610. dprintk("--> %s\n", __func__);
  4611. nfs41_sequence_done(clp, res, task->tk_status);
  4612. switch (task->tk_status) {
  4613. case 0:
  4614. case -NFS4ERR_COMPLETE_ALREADY:
  4615. break;
  4616. case -NFS4ERR_BADSESSION:
  4617. case -NFS4ERR_DEADSESSION:
  4618. /*
  4619. * Handle the session error, but do not retry the operation, as
  4620. * we have no way of telling whether the clientid had to be
  4621. * reset before we got our reply. If reset, a new wave of
  4622. * reclaim operations will follow, containing their own reclaim
  4623. * complete. We don't want our retry to get on the way of
  4624. * recovery by incorrectly indicating to the server that we're
  4625. * done reclaiming state since the process had to be restarted.
  4626. */
  4627. _nfs4_async_handle_error(task, NULL, clp, NULL);
  4628. break;
  4629. default:
  4630. if (_nfs4_async_handle_error(
  4631. task, NULL, clp, NULL) == -EAGAIN) {
  4632. rpc_restart_call_prepare(task);
  4633. return;
  4634. }
  4635. }
  4636. dprintk("<-- %s\n", __func__);
  4637. }
  4638. static void nfs4_free_reclaim_complete_data(void *data)
  4639. {
  4640. struct nfs4_reclaim_complete_data *calldata = data;
  4641. kfree(calldata);
  4642. }
  4643. static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
  4644. .rpc_call_prepare = nfs4_reclaim_complete_prepare,
  4645. .rpc_call_done = nfs4_reclaim_complete_done,
  4646. .rpc_release = nfs4_free_reclaim_complete_data,
  4647. };
  4648. /*
  4649. * Issue a global reclaim complete.
  4650. */
  4651. static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
  4652. {
  4653. struct nfs4_reclaim_complete_data *calldata;
  4654. struct rpc_task *task;
  4655. struct rpc_message msg = {
  4656. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
  4657. };
  4658. struct rpc_task_setup task_setup_data = {
  4659. .rpc_client = clp->cl_rpcclient,
  4660. .rpc_message = &msg,
  4661. .callback_ops = &nfs4_reclaim_complete_call_ops,
  4662. .flags = RPC_TASK_ASYNC,
  4663. };
  4664. int status = -ENOMEM;
  4665. dprintk("--> %s\n", __func__);
  4666. calldata = kzalloc(sizeof(*calldata), GFP_KERNEL);
  4667. if (calldata == NULL)
  4668. goto out;
  4669. calldata->clp = clp;
  4670. calldata->arg.one_fs = 0;
  4671. calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
  4672. msg.rpc_argp = &calldata->arg;
  4673. msg.rpc_resp = &calldata->res;
  4674. task_setup_data.callback_data = calldata;
  4675. task = rpc_run_task(&task_setup_data);
  4676. if (IS_ERR(task)) {
  4677. status = PTR_ERR(task);
  4678. goto out;
  4679. }
  4680. rpc_put_task(task);
  4681. return 0;
  4682. out:
  4683. dprintk("<-- %s status=%d\n", __func__, status);
  4684. return status;
  4685. }
  4686. #endif /* CONFIG_NFS_V4_1 */
  4687. struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
  4688. .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
  4689. .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
  4690. .recover_open = nfs4_open_reclaim,
  4691. .recover_lock = nfs4_lock_reclaim,
  4692. .establish_clid = nfs4_init_clientid,
  4693. .get_clid_cred = nfs4_get_setclientid_cred,
  4694. };
  4695. #if defined(CONFIG_NFS_V4_1)
  4696. struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
  4697. .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
  4698. .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
  4699. .recover_open = nfs4_open_reclaim,
  4700. .recover_lock = nfs4_lock_reclaim,
  4701. .establish_clid = nfs41_init_clientid,
  4702. .get_clid_cred = nfs4_get_exchange_id_cred,
  4703. .reclaim_complete = nfs41_proc_reclaim_complete,
  4704. };
  4705. #endif /* CONFIG_NFS_V4_1 */
  4706. struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
  4707. .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
  4708. .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
  4709. .recover_open = nfs4_open_expired,
  4710. .recover_lock = nfs4_lock_expired,
  4711. .establish_clid = nfs4_init_clientid,
  4712. .get_clid_cred = nfs4_get_setclientid_cred,
  4713. };
  4714. #if defined(CONFIG_NFS_V4_1)
  4715. struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
  4716. .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
  4717. .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
  4718. .recover_open = nfs4_open_expired,
  4719. .recover_lock = nfs4_lock_expired,
  4720. .establish_clid = nfs41_init_clientid,
  4721. .get_clid_cred = nfs4_get_exchange_id_cred,
  4722. };
  4723. #endif /* CONFIG_NFS_V4_1 */
  4724. struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
  4725. .sched_state_renewal = nfs4_proc_async_renew,
  4726. .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
  4727. .renew_lease = nfs4_proc_renew,
  4728. };
  4729. #if defined(CONFIG_NFS_V4_1)
  4730. struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
  4731. .sched_state_renewal = nfs41_proc_async_sequence,
  4732. .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
  4733. .renew_lease = nfs4_proc_sequence,
  4734. };
  4735. #endif
  4736. /*
  4737. * Per minor version reboot and network partition recovery ops
  4738. */
  4739. struct nfs4_state_recovery_ops *nfs4_reboot_recovery_ops[] = {
  4740. &nfs40_reboot_recovery_ops,
  4741. #if defined(CONFIG_NFS_V4_1)
  4742. &nfs41_reboot_recovery_ops,
  4743. #endif
  4744. };
  4745. struct nfs4_state_recovery_ops *nfs4_nograce_recovery_ops[] = {
  4746. &nfs40_nograce_recovery_ops,
  4747. #if defined(CONFIG_NFS_V4_1)
  4748. &nfs41_nograce_recovery_ops,
  4749. #endif
  4750. };
  4751. struct nfs4_state_maintenance_ops *nfs4_state_renewal_ops[] = {
  4752. &nfs40_state_renewal_ops,
  4753. #if defined(CONFIG_NFS_V4_1)
  4754. &nfs41_state_renewal_ops,
  4755. #endif
  4756. };
  4757. static const struct inode_operations nfs4_file_inode_operations = {
  4758. .permission = nfs_permission,
  4759. .getattr = nfs_getattr,
  4760. .setattr = nfs_setattr,
  4761. .getxattr = nfs4_getxattr,
  4762. .setxattr = nfs4_setxattr,
  4763. .listxattr = nfs4_listxattr,
  4764. };
  4765. const struct nfs_rpc_ops nfs_v4_clientops = {
  4766. .version = 4, /* protocol version */
  4767. .dentry_ops = &nfs4_dentry_operations,
  4768. .dir_inode_ops = &nfs4_dir_inode_operations,
  4769. .file_inode_ops = &nfs4_file_inode_operations,
  4770. .getroot = nfs4_proc_get_root,
  4771. .getattr = nfs4_proc_getattr,
  4772. .setattr = nfs4_proc_setattr,
  4773. .lookupfh = nfs4_proc_lookupfh,
  4774. .lookup = nfs4_proc_lookup,
  4775. .access = nfs4_proc_access,
  4776. .readlink = nfs4_proc_readlink,
  4777. .create = nfs4_proc_create,
  4778. .remove = nfs4_proc_remove,
  4779. .unlink_setup = nfs4_proc_unlink_setup,
  4780. .unlink_done = nfs4_proc_unlink_done,
  4781. .rename = nfs4_proc_rename,
  4782. .link = nfs4_proc_link,
  4783. .symlink = nfs4_proc_symlink,
  4784. .mkdir = nfs4_proc_mkdir,
  4785. .rmdir = nfs4_proc_remove,
  4786. .readdir = nfs4_proc_readdir,
  4787. .mknod = nfs4_proc_mknod,
  4788. .statfs = nfs4_proc_statfs,
  4789. .fsinfo = nfs4_proc_fsinfo,
  4790. .pathconf = nfs4_proc_pathconf,
  4791. .set_capabilities = nfs4_server_capabilities,
  4792. .decode_dirent = nfs4_decode_dirent,
  4793. .read_setup = nfs4_proc_read_setup,
  4794. .read_done = nfs4_read_done,
  4795. .write_setup = nfs4_proc_write_setup,
  4796. .write_done = nfs4_write_done,
  4797. .commit_setup = nfs4_proc_commit_setup,
  4798. .commit_done = nfs4_commit_done,
  4799. .lock = nfs4_proc_lock,
  4800. .clear_acl_cache = nfs4_zap_acl_attr,
  4801. .close_context = nfs4_close_context,
  4802. };
  4803. /*
  4804. * Local variables:
  4805. * c-basic-offset: 8
  4806. * End:
  4807. */