nfs4proc.c 136 KB

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