nfs4proc.c 143 KB

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