nfs4proc.c 136 KB

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