nfs4proc.c 143 KB

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