nfs4proc.c 136 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067
  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 inline 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. if (err != -NFS4ERR_DELAY)
  1348. break;
  1349. nfs4_handle_exception(server, err, &exception);
  1350. } while (exception.retry);
  1351. return err;
  1352. }
  1353. static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
  1354. {
  1355. struct nfs_open_context *ctx;
  1356. int ret;
  1357. ctx = nfs4_state_find_open_context(state);
  1358. if (IS_ERR(ctx))
  1359. return PTR_ERR(ctx);
  1360. ret = nfs4_do_open_expired(ctx, state);
  1361. put_nfs_open_context(ctx);
  1362. return ret;
  1363. }
  1364. /*
  1365. * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
  1366. * fields corresponding to attributes that were used to store the verifier.
  1367. * Make sure we clobber those fields in the later setattr call
  1368. */
  1369. static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
  1370. {
  1371. if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
  1372. !(sattr->ia_valid & ATTR_ATIME_SET))
  1373. sattr->ia_valid |= ATTR_ATIME;
  1374. if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
  1375. !(sattr->ia_valid & ATTR_MTIME_SET))
  1376. sattr->ia_valid |= ATTR_MTIME;
  1377. }
  1378. /*
  1379. * Returns a referenced nfs4_state
  1380. */
  1381. 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)
  1382. {
  1383. struct nfs4_state_owner *sp;
  1384. struct nfs4_state *state = NULL;
  1385. struct nfs_server *server = NFS_SERVER(dir);
  1386. struct nfs4_opendata *opendata;
  1387. int status;
  1388. /* Protect against reboot recovery conflicts */
  1389. status = -ENOMEM;
  1390. if (!(sp = nfs4_get_state_owner(server, cred))) {
  1391. dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
  1392. goto out_err;
  1393. }
  1394. status = nfs4_recover_expired_lease(server);
  1395. if (status != 0)
  1396. goto err_put_state_owner;
  1397. if (path->dentry->d_inode != NULL)
  1398. nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
  1399. status = -ENOMEM;
  1400. opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr);
  1401. if (opendata == NULL)
  1402. goto err_put_state_owner;
  1403. if (path->dentry->d_inode != NULL)
  1404. opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
  1405. status = _nfs4_proc_open(opendata);
  1406. if (status != 0)
  1407. goto err_opendata_put;
  1408. if (opendata->o_arg.open_flags & O_EXCL)
  1409. nfs4_exclusive_attrset(opendata, sattr);
  1410. state = nfs4_opendata_to_nfs4_state(opendata);
  1411. status = PTR_ERR(state);
  1412. if (IS_ERR(state))
  1413. goto err_opendata_put;
  1414. nfs4_opendata_put(opendata);
  1415. nfs4_put_state_owner(sp);
  1416. *res = state;
  1417. return 0;
  1418. err_opendata_put:
  1419. nfs4_opendata_put(opendata);
  1420. err_put_state_owner:
  1421. nfs4_put_state_owner(sp);
  1422. out_err:
  1423. *res = NULL;
  1424. return status;
  1425. }
  1426. 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)
  1427. {
  1428. struct nfs4_exception exception = { };
  1429. struct nfs4_state *res;
  1430. int status;
  1431. do {
  1432. status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
  1433. if (status == 0)
  1434. break;
  1435. /* NOTE: BAD_SEQID means the server and client disagree about the
  1436. * book-keeping w.r.t. state-changing operations
  1437. * (OPEN/CLOSE/LOCK/LOCKU...)
  1438. * It is actually a sign of a bug on the client or on the server.
  1439. *
  1440. * If we receive a BAD_SEQID error in the particular case of
  1441. * doing an OPEN, we assume that nfs_increment_open_seqid() will
  1442. * have unhashed the old state_owner for us, and that we can
  1443. * therefore safely retry using a new one. We should still warn
  1444. * the user though...
  1445. */
  1446. if (status == -NFS4ERR_BAD_SEQID) {
  1447. printk(KERN_WARNING "NFS: v4 server %s "
  1448. " returned a bad sequence-id error!\n",
  1449. NFS_SERVER(dir)->nfs_client->cl_hostname);
  1450. exception.retry = 1;
  1451. continue;
  1452. }
  1453. /*
  1454. * BAD_STATEID on OPEN means that the server cancelled our
  1455. * state before it received the OPEN_CONFIRM.
  1456. * Recover by retrying the request as per the discussion
  1457. * on Page 181 of RFC3530.
  1458. */
  1459. if (status == -NFS4ERR_BAD_STATEID) {
  1460. exception.retry = 1;
  1461. continue;
  1462. }
  1463. if (status == -EAGAIN) {
  1464. /* We must have found a delegation */
  1465. exception.retry = 1;
  1466. continue;
  1467. }
  1468. res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
  1469. status, &exception));
  1470. } while (exception.retry);
  1471. return res;
  1472. }
  1473. static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
  1474. struct nfs_fattr *fattr, struct iattr *sattr,
  1475. struct nfs4_state *state)
  1476. {
  1477. struct nfs_server *server = NFS_SERVER(inode);
  1478. struct nfs_setattrargs arg = {
  1479. .fh = NFS_FH(inode),
  1480. .iap = sattr,
  1481. .server = server,
  1482. .bitmask = server->attr_bitmask,
  1483. };
  1484. struct nfs_setattrres res = {
  1485. .fattr = fattr,
  1486. .server = server,
  1487. };
  1488. struct rpc_message msg = {
  1489. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
  1490. .rpc_argp = &arg,
  1491. .rpc_resp = &res,
  1492. .rpc_cred = cred,
  1493. };
  1494. unsigned long timestamp = jiffies;
  1495. int status;
  1496. nfs_fattr_init(fattr);
  1497. if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
  1498. /* Use that stateid */
  1499. } else if (state != NULL) {
  1500. nfs4_copy_stateid(&arg.stateid, state, current->files);
  1501. } else
  1502. memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
  1503. status = nfs4_call_sync(server, &msg, &arg, &res, 1);
  1504. if (status == 0 && state != NULL)
  1505. renew_lease(server, timestamp);
  1506. return status;
  1507. }
  1508. static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
  1509. struct nfs_fattr *fattr, struct iattr *sattr,
  1510. struct nfs4_state *state)
  1511. {
  1512. struct nfs_server *server = NFS_SERVER(inode);
  1513. struct nfs4_exception exception = { };
  1514. int err;
  1515. do {
  1516. err = nfs4_handle_exception(server,
  1517. _nfs4_do_setattr(inode, cred, fattr, sattr, state),
  1518. &exception);
  1519. } while (exception.retry);
  1520. return err;
  1521. }
  1522. struct nfs4_closedata {
  1523. struct path path;
  1524. struct inode *inode;
  1525. struct nfs4_state *state;
  1526. struct nfs_closeargs arg;
  1527. struct nfs_closeres res;
  1528. struct nfs_fattr fattr;
  1529. unsigned long timestamp;
  1530. };
  1531. static void nfs4_free_closedata(void *data)
  1532. {
  1533. struct nfs4_closedata *calldata = data;
  1534. struct nfs4_state_owner *sp = calldata->state->owner;
  1535. nfs4_put_open_state(calldata->state);
  1536. nfs_free_seqid(calldata->arg.seqid);
  1537. nfs4_put_state_owner(sp);
  1538. path_put(&calldata->path);
  1539. kfree(calldata);
  1540. }
  1541. static void nfs4_close_done(struct rpc_task *task, void *data)
  1542. {
  1543. struct nfs4_closedata *calldata = data;
  1544. struct nfs4_state *state = calldata->state;
  1545. struct nfs_server *server = NFS_SERVER(calldata->inode);
  1546. nfs4_sequence_done(server, &calldata->res.seq_res, task->tk_status);
  1547. if (RPC_ASSASSINATED(task))
  1548. return;
  1549. /* hmm. we are done with the inode, and in the process of freeing
  1550. * the state_owner. we keep this around to process errors
  1551. */
  1552. switch (task->tk_status) {
  1553. case 0:
  1554. nfs_set_open_stateid(state, &calldata->res.stateid, 0);
  1555. renew_lease(server, calldata->timestamp);
  1556. break;
  1557. case -NFS4ERR_STALE_STATEID:
  1558. case -NFS4ERR_OLD_STATEID:
  1559. case -NFS4ERR_BAD_STATEID:
  1560. case -NFS4ERR_EXPIRED:
  1561. if (calldata->arg.fmode == 0)
  1562. break;
  1563. default:
  1564. if (nfs4_async_handle_error(task, server, state) == -EAGAIN) {
  1565. nfs4_restart_rpc(task, server->nfs_client);
  1566. return;
  1567. }
  1568. }
  1569. nfs4_sequence_free_slot(server->nfs_client, &calldata->res.seq_res);
  1570. nfs_refresh_inode(calldata->inode, calldata->res.fattr);
  1571. }
  1572. static void nfs4_close_prepare(struct rpc_task *task, void *data)
  1573. {
  1574. struct nfs4_closedata *calldata = data;
  1575. struct nfs4_state *state = calldata->state;
  1576. int clear_rd, clear_wr, clear_rdwr;
  1577. if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
  1578. return;
  1579. clear_rd = clear_wr = clear_rdwr = 0;
  1580. spin_lock(&state->owner->so_lock);
  1581. /* Calculate the change in open mode */
  1582. if (state->n_rdwr == 0) {
  1583. if (state->n_rdonly == 0) {
  1584. clear_rd |= test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags);
  1585. clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
  1586. }
  1587. if (state->n_wronly == 0) {
  1588. clear_wr |= test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags);
  1589. clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
  1590. }
  1591. }
  1592. spin_unlock(&state->owner->so_lock);
  1593. if (!clear_rd && !clear_wr && !clear_rdwr) {
  1594. /* Note: exit _without_ calling nfs4_close_done */
  1595. task->tk_action = NULL;
  1596. return;
  1597. }
  1598. nfs_fattr_init(calldata->res.fattr);
  1599. if (test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0) {
  1600. task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
  1601. calldata->arg.fmode = FMODE_READ;
  1602. } else if (test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0) {
  1603. task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
  1604. calldata->arg.fmode = FMODE_WRITE;
  1605. }
  1606. calldata->timestamp = jiffies;
  1607. if (nfs4_setup_sequence((NFS_SERVER(calldata->inode))->nfs_client,
  1608. &calldata->arg.seq_args, &calldata->res.seq_res,
  1609. 1, task))
  1610. return;
  1611. rpc_call_start(task);
  1612. }
  1613. static const struct rpc_call_ops nfs4_close_ops = {
  1614. .rpc_call_prepare = nfs4_close_prepare,
  1615. .rpc_call_done = nfs4_close_done,
  1616. .rpc_release = nfs4_free_closedata,
  1617. };
  1618. /*
  1619. * It is possible for data to be read/written from a mem-mapped file
  1620. * after the sys_close call (which hits the vfs layer as a flush).
  1621. * This means that we can't safely call nfsv4 close on a file until
  1622. * the inode is cleared. This in turn means that we are not good
  1623. * NFSv4 citizens - we do not indicate to the server to update the file's
  1624. * share state even when we are done with one of the three share
  1625. * stateid's in the inode.
  1626. *
  1627. * NOTE: Caller must be holding the sp->so_owner semaphore!
  1628. */
  1629. int nfs4_do_close(struct path *path, struct nfs4_state *state, int wait)
  1630. {
  1631. struct nfs_server *server = NFS_SERVER(state->inode);
  1632. struct nfs4_closedata *calldata;
  1633. struct nfs4_state_owner *sp = state->owner;
  1634. struct rpc_task *task;
  1635. struct rpc_message msg = {
  1636. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
  1637. .rpc_cred = state->owner->so_cred,
  1638. };
  1639. struct rpc_task_setup task_setup_data = {
  1640. .rpc_client = server->client,
  1641. .rpc_message = &msg,
  1642. .callback_ops = &nfs4_close_ops,
  1643. .workqueue = nfsiod_workqueue,
  1644. .flags = RPC_TASK_ASYNC,
  1645. };
  1646. int status = -ENOMEM;
  1647. calldata = kzalloc(sizeof(*calldata), GFP_KERNEL);
  1648. if (calldata == NULL)
  1649. goto out;
  1650. calldata->inode = state->inode;
  1651. calldata->state = state;
  1652. calldata->arg.fh = NFS_FH(state->inode);
  1653. calldata->arg.stateid = &state->open_stateid;
  1654. if (nfs4_has_session(server->nfs_client))
  1655. memset(calldata->arg.stateid->data, 0, 4); /* clear seqid */
  1656. /* Serialization for the sequence id */
  1657. calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
  1658. if (calldata->arg.seqid == NULL)
  1659. goto out_free_calldata;
  1660. calldata->arg.fmode = 0;
  1661. calldata->arg.bitmask = server->cache_consistency_bitmask;
  1662. calldata->res.fattr = &calldata->fattr;
  1663. calldata->res.seqid = calldata->arg.seqid;
  1664. calldata->res.server = server;
  1665. calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
  1666. calldata->path.mnt = mntget(path->mnt);
  1667. calldata->path.dentry = dget(path->dentry);
  1668. msg.rpc_argp = &calldata->arg,
  1669. msg.rpc_resp = &calldata->res,
  1670. task_setup_data.callback_data = calldata;
  1671. task = rpc_run_task(&task_setup_data);
  1672. if (IS_ERR(task))
  1673. return PTR_ERR(task);
  1674. status = 0;
  1675. if (wait)
  1676. status = rpc_wait_for_completion_task(task);
  1677. rpc_put_task(task);
  1678. return status;
  1679. out_free_calldata:
  1680. kfree(calldata);
  1681. out:
  1682. nfs4_put_open_state(state);
  1683. nfs4_put_state_owner(sp);
  1684. return status;
  1685. }
  1686. static int nfs4_intent_set_file(struct nameidata *nd, struct path *path, struct nfs4_state *state, fmode_t fmode)
  1687. {
  1688. struct file *filp;
  1689. int ret;
  1690. /* If the open_intent is for execute, we have an extra check to make */
  1691. if (fmode & FMODE_EXEC) {
  1692. ret = nfs_may_open(state->inode,
  1693. state->owner->so_cred,
  1694. nd->intent.open.flags);
  1695. if (ret < 0)
  1696. goto out_close;
  1697. }
  1698. filp = lookup_instantiate_filp(nd, path->dentry, NULL);
  1699. if (!IS_ERR(filp)) {
  1700. struct nfs_open_context *ctx;
  1701. ctx = nfs_file_open_context(filp);
  1702. ctx->state = state;
  1703. return 0;
  1704. }
  1705. ret = PTR_ERR(filp);
  1706. out_close:
  1707. nfs4_close_sync(path, state, fmode & (FMODE_READ|FMODE_WRITE));
  1708. return ret;
  1709. }
  1710. struct dentry *
  1711. nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
  1712. {
  1713. struct path path = {
  1714. .mnt = nd->path.mnt,
  1715. .dentry = dentry,
  1716. };
  1717. struct dentry *parent;
  1718. struct iattr attr;
  1719. struct rpc_cred *cred;
  1720. struct nfs4_state *state;
  1721. struct dentry *res;
  1722. fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
  1723. if (nd->flags & LOOKUP_CREATE) {
  1724. attr.ia_mode = nd->intent.open.create_mode;
  1725. attr.ia_valid = ATTR_MODE;
  1726. if (!IS_POSIXACL(dir))
  1727. attr.ia_mode &= ~current_umask();
  1728. } else {
  1729. attr.ia_valid = 0;
  1730. BUG_ON(nd->intent.open.flags & O_CREAT);
  1731. }
  1732. cred = rpc_lookup_cred();
  1733. if (IS_ERR(cred))
  1734. return (struct dentry *)cred;
  1735. parent = dentry->d_parent;
  1736. /* Protect against concurrent sillydeletes */
  1737. nfs_block_sillyrename(parent);
  1738. state = nfs4_do_open(dir, &path, fmode, nd->intent.open.flags, &attr, cred);
  1739. put_rpccred(cred);
  1740. if (IS_ERR(state)) {
  1741. if (PTR_ERR(state) == -ENOENT) {
  1742. d_add(dentry, NULL);
  1743. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1744. }
  1745. nfs_unblock_sillyrename(parent);
  1746. return (struct dentry *)state;
  1747. }
  1748. res = d_add_unique(dentry, igrab(state->inode));
  1749. if (res != NULL)
  1750. path.dentry = res;
  1751. nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
  1752. nfs_unblock_sillyrename(parent);
  1753. nfs4_intent_set_file(nd, &path, state, fmode);
  1754. return res;
  1755. }
  1756. int
  1757. nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
  1758. {
  1759. struct path path = {
  1760. .mnt = nd->path.mnt,
  1761. .dentry = dentry,
  1762. };
  1763. struct rpc_cred *cred;
  1764. struct nfs4_state *state;
  1765. fmode_t fmode = openflags & (FMODE_READ | FMODE_WRITE);
  1766. cred = rpc_lookup_cred();
  1767. if (IS_ERR(cred))
  1768. return PTR_ERR(cred);
  1769. state = nfs4_do_open(dir, &path, fmode, openflags, NULL, cred);
  1770. put_rpccred(cred);
  1771. if (IS_ERR(state)) {
  1772. switch (PTR_ERR(state)) {
  1773. case -EPERM:
  1774. case -EACCES:
  1775. case -EDQUOT:
  1776. case -ENOSPC:
  1777. case -EROFS:
  1778. lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
  1779. return 1;
  1780. default:
  1781. goto out_drop;
  1782. }
  1783. }
  1784. if (state->inode == dentry->d_inode) {
  1785. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1786. nfs4_intent_set_file(nd, &path, state, fmode);
  1787. return 1;
  1788. }
  1789. nfs4_close_sync(&path, state, fmode);
  1790. out_drop:
  1791. d_drop(dentry);
  1792. return 0;
  1793. }
  1794. void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
  1795. {
  1796. if (ctx->state == NULL)
  1797. return;
  1798. if (is_sync)
  1799. nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
  1800. else
  1801. nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
  1802. }
  1803. static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
  1804. {
  1805. struct nfs4_server_caps_arg args = {
  1806. .fhandle = fhandle,
  1807. };
  1808. struct nfs4_server_caps_res res = {};
  1809. struct rpc_message msg = {
  1810. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
  1811. .rpc_argp = &args,
  1812. .rpc_resp = &res,
  1813. };
  1814. int status;
  1815. status = nfs4_call_sync(server, &msg, &args, &res, 0);
  1816. if (status == 0) {
  1817. memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
  1818. server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
  1819. NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
  1820. NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
  1821. NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
  1822. NFS_CAP_CTIME|NFS_CAP_MTIME);
  1823. if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
  1824. server->caps |= NFS_CAP_ACLS;
  1825. if (res.has_links != 0)
  1826. server->caps |= NFS_CAP_HARDLINKS;
  1827. if (res.has_symlinks != 0)
  1828. server->caps |= NFS_CAP_SYMLINKS;
  1829. if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
  1830. server->caps |= NFS_CAP_FILEID;
  1831. if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
  1832. server->caps |= NFS_CAP_MODE;
  1833. if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
  1834. server->caps |= NFS_CAP_NLINK;
  1835. if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
  1836. server->caps |= NFS_CAP_OWNER;
  1837. if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
  1838. server->caps |= NFS_CAP_OWNER_GROUP;
  1839. if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
  1840. server->caps |= NFS_CAP_ATIME;
  1841. if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
  1842. server->caps |= NFS_CAP_CTIME;
  1843. if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
  1844. server->caps |= NFS_CAP_MTIME;
  1845. memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
  1846. server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
  1847. server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
  1848. server->acl_bitmask = res.acl_bitmask;
  1849. }
  1850. return status;
  1851. }
  1852. int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
  1853. {
  1854. struct nfs4_exception exception = { };
  1855. int err;
  1856. do {
  1857. err = nfs4_handle_exception(server,
  1858. _nfs4_server_capabilities(server, fhandle),
  1859. &exception);
  1860. } while (exception.retry);
  1861. return err;
  1862. }
  1863. static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
  1864. struct nfs_fsinfo *info)
  1865. {
  1866. struct nfs4_lookup_root_arg args = {
  1867. .bitmask = nfs4_fattr_bitmap,
  1868. };
  1869. struct nfs4_lookup_res res = {
  1870. .server = server,
  1871. .fattr = info->fattr,
  1872. .fh = fhandle,
  1873. };
  1874. struct rpc_message msg = {
  1875. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
  1876. .rpc_argp = &args,
  1877. .rpc_resp = &res,
  1878. };
  1879. nfs_fattr_init(info->fattr);
  1880. return nfs4_call_sync(server, &msg, &args, &res, 0);
  1881. }
  1882. static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
  1883. struct nfs_fsinfo *info)
  1884. {
  1885. struct nfs4_exception exception = { };
  1886. int err;
  1887. do {
  1888. err = nfs4_handle_exception(server,
  1889. _nfs4_lookup_root(server, fhandle, info),
  1890. &exception);
  1891. } while (exception.retry);
  1892. return err;
  1893. }
  1894. /*
  1895. * get the file handle for the "/" directory on the server
  1896. */
  1897. static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
  1898. struct nfs_fsinfo *info)
  1899. {
  1900. int status;
  1901. status = nfs4_lookup_root(server, fhandle, info);
  1902. if (status == 0)
  1903. status = nfs4_server_capabilities(server, fhandle);
  1904. if (status == 0)
  1905. status = nfs4_do_fsinfo(server, fhandle, info);
  1906. return nfs4_map_errors(status);
  1907. }
  1908. /*
  1909. * Get locations and (maybe) other attributes of a referral.
  1910. * Note that we'll actually follow the referral later when
  1911. * we detect fsid mismatch in inode revalidation
  1912. */
  1913. static int nfs4_get_referral(struct inode *dir, const struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle)
  1914. {
  1915. int status = -ENOMEM;
  1916. struct page *page = NULL;
  1917. struct nfs4_fs_locations *locations = NULL;
  1918. page = alloc_page(GFP_KERNEL);
  1919. if (page == NULL)
  1920. goto out;
  1921. locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
  1922. if (locations == NULL)
  1923. goto out;
  1924. status = nfs4_proc_fs_locations(dir, name, locations, page);
  1925. if (status != 0)
  1926. goto out;
  1927. /* Make sure server returned a different fsid for the referral */
  1928. if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
  1929. dprintk("%s: server did not return a different fsid for a referral at %s\n", __func__, name->name);
  1930. status = -EIO;
  1931. goto out;
  1932. }
  1933. memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
  1934. fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
  1935. if (!fattr->mode)
  1936. fattr->mode = S_IFDIR;
  1937. memset(fhandle, 0, sizeof(struct nfs_fh));
  1938. out:
  1939. if (page)
  1940. __free_page(page);
  1941. if (locations)
  1942. kfree(locations);
  1943. return status;
  1944. }
  1945. static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
  1946. {
  1947. struct nfs4_getattr_arg args = {
  1948. .fh = fhandle,
  1949. .bitmask = server->attr_bitmask,
  1950. };
  1951. struct nfs4_getattr_res res = {
  1952. .fattr = fattr,
  1953. .server = server,
  1954. };
  1955. struct rpc_message msg = {
  1956. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
  1957. .rpc_argp = &args,
  1958. .rpc_resp = &res,
  1959. };
  1960. nfs_fattr_init(fattr);
  1961. return nfs4_call_sync(server, &msg, &args, &res, 0);
  1962. }
  1963. static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
  1964. {
  1965. struct nfs4_exception exception = { };
  1966. int err;
  1967. do {
  1968. err = nfs4_handle_exception(server,
  1969. _nfs4_proc_getattr(server, fhandle, fattr),
  1970. &exception);
  1971. } while (exception.retry);
  1972. return err;
  1973. }
  1974. /*
  1975. * The file is not closed if it is opened due to the a request to change
  1976. * the size of the file. The open call will not be needed once the
  1977. * VFS layer lookup-intents are implemented.
  1978. *
  1979. * Close is called when the inode is destroyed.
  1980. * If we haven't opened the file for O_WRONLY, we
  1981. * need to in the size_change case to obtain a stateid.
  1982. *
  1983. * Got race?
  1984. * Because OPEN is always done by name in nfsv4, it is
  1985. * possible that we opened a different file by the same
  1986. * name. We can recognize this race condition, but we
  1987. * can't do anything about it besides returning an error.
  1988. *
  1989. * This will be fixed with VFS changes (lookup-intent).
  1990. */
  1991. static int
  1992. nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
  1993. struct iattr *sattr)
  1994. {
  1995. struct inode *inode = dentry->d_inode;
  1996. struct rpc_cred *cred = NULL;
  1997. struct nfs4_state *state = NULL;
  1998. int status;
  1999. nfs_fattr_init(fattr);
  2000. /* Search for an existing open(O_WRITE) file */
  2001. if (sattr->ia_valid & ATTR_FILE) {
  2002. struct nfs_open_context *ctx;
  2003. ctx = nfs_file_open_context(sattr->ia_file);
  2004. if (ctx) {
  2005. cred = ctx->cred;
  2006. state = ctx->state;
  2007. }
  2008. }
  2009. status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
  2010. if (status == 0)
  2011. nfs_setattr_update_inode(inode, sattr);
  2012. return status;
  2013. }
  2014. static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
  2015. const struct qstr *name, struct nfs_fh *fhandle,
  2016. struct nfs_fattr *fattr)
  2017. {
  2018. int status;
  2019. struct nfs4_lookup_arg args = {
  2020. .bitmask = server->attr_bitmask,
  2021. .dir_fh = dirfh,
  2022. .name = name,
  2023. };
  2024. struct nfs4_lookup_res res = {
  2025. .server = server,
  2026. .fattr = fattr,
  2027. .fh = fhandle,
  2028. };
  2029. struct rpc_message msg = {
  2030. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
  2031. .rpc_argp = &args,
  2032. .rpc_resp = &res,
  2033. };
  2034. nfs_fattr_init(fattr);
  2035. dprintk("NFS call lookupfh %s\n", name->name);
  2036. status = nfs4_call_sync(server, &msg, &args, &res, 0);
  2037. dprintk("NFS reply lookupfh: %d\n", status);
  2038. return status;
  2039. }
  2040. static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
  2041. struct qstr *name, struct nfs_fh *fhandle,
  2042. struct nfs_fattr *fattr)
  2043. {
  2044. struct nfs4_exception exception = { };
  2045. int err;
  2046. do {
  2047. err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
  2048. /* FIXME: !!!! */
  2049. if (err == -NFS4ERR_MOVED) {
  2050. err = -EREMOTE;
  2051. break;
  2052. }
  2053. err = nfs4_handle_exception(server, err, &exception);
  2054. } while (exception.retry);
  2055. return err;
  2056. }
  2057. static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
  2058. struct nfs_fh *fhandle, struct nfs_fattr *fattr)
  2059. {
  2060. int status;
  2061. dprintk("NFS call lookup %s\n", name->name);
  2062. status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
  2063. if (status == -NFS4ERR_MOVED)
  2064. status = nfs4_get_referral(dir, name, fattr, fhandle);
  2065. dprintk("NFS reply lookup: %d\n", status);
  2066. return status;
  2067. }
  2068. static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
  2069. {
  2070. struct nfs4_exception exception = { };
  2071. int err;
  2072. do {
  2073. err = nfs4_handle_exception(NFS_SERVER(dir),
  2074. _nfs4_proc_lookup(dir, name, fhandle, fattr),
  2075. &exception);
  2076. } while (exception.retry);
  2077. return err;
  2078. }
  2079. static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
  2080. {
  2081. struct nfs_server *server = NFS_SERVER(inode);
  2082. struct nfs_fattr fattr;
  2083. struct nfs4_accessargs args = {
  2084. .fh = NFS_FH(inode),
  2085. .bitmask = server->attr_bitmask,
  2086. };
  2087. struct nfs4_accessres res = {
  2088. .server = server,
  2089. .fattr = &fattr,
  2090. };
  2091. struct rpc_message msg = {
  2092. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
  2093. .rpc_argp = &args,
  2094. .rpc_resp = &res,
  2095. .rpc_cred = entry->cred,
  2096. };
  2097. int mode = entry->mask;
  2098. int status;
  2099. /*
  2100. * Determine which access bits we want to ask for...
  2101. */
  2102. if (mode & MAY_READ)
  2103. args.access |= NFS4_ACCESS_READ;
  2104. if (S_ISDIR(inode->i_mode)) {
  2105. if (mode & MAY_WRITE)
  2106. args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
  2107. if (mode & MAY_EXEC)
  2108. args.access |= NFS4_ACCESS_LOOKUP;
  2109. } else {
  2110. if (mode & MAY_WRITE)
  2111. args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
  2112. if (mode & MAY_EXEC)
  2113. args.access |= NFS4_ACCESS_EXECUTE;
  2114. }
  2115. nfs_fattr_init(&fattr);
  2116. status = nfs4_call_sync(server, &msg, &args, &res, 0);
  2117. if (!status) {
  2118. entry->mask = 0;
  2119. if (res.access & NFS4_ACCESS_READ)
  2120. entry->mask |= MAY_READ;
  2121. if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
  2122. entry->mask |= MAY_WRITE;
  2123. if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
  2124. entry->mask |= MAY_EXEC;
  2125. nfs_refresh_inode(inode, &fattr);
  2126. }
  2127. return status;
  2128. }
  2129. static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
  2130. {
  2131. struct nfs4_exception exception = { };
  2132. int err;
  2133. do {
  2134. err = nfs4_handle_exception(NFS_SERVER(inode),
  2135. _nfs4_proc_access(inode, entry),
  2136. &exception);
  2137. } while (exception.retry);
  2138. return err;
  2139. }
  2140. /*
  2141. * TODO: For the time being, we don't try to get any attributes
  2142. * along with any of the zero-copy operations READ, READDIR,
  2143. * READLINK, WRITE.
  2144. *
  2145. * In the case of the first three, we want to put the GETATTR
  2146. * after the read-type operation -- this is because it is hard
  2147. * to predict the length of a GETATTR response in v4, and thus
  2148. * align the READ data correctly. This means that the GETATTR
  2149. * may end up partially falling into the page cache, and we should
  2150. * shift it into the 'tail' of the xdr_buf before processing.
  2151. * To do this efficiently, we need to know the total length
  2152. * of data received, which doesn't seem to be available outside
  2153. * of the RPC layer.
  2154. *
  2155. * In the case of WRITE, we also want to put the GETATTR after
  2156. * the operation -- in this case because we want to make sure
  2157. * we get the post-operation mtime and size. This means that
  2158. * we can't use xdr_encode_pages() as written: we need a variant
  2159. * of it which would leave room in the 'tail' iovec.
  2160. *
  2161. * Both of these changes to the XDR layer would in fact be quite
  2162. * minor, but I decided to leave them for a subsequent patch.
  2163. */
  2164. static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
  2165. unsigned int pgbase, unsigned int pglen)
  2166. {
  2167. struct nfs4_readlink args = {
  2168. .fh = NFS_FH(inode),
  2169. .pgbase = pgbase,
  2170. .pglen = pglen,
  2171. .pages = &page,
  2172. };
  2173. struct nfs4_readlink_res res;
  2174. struct rpc_message msg = {
  2175. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
  2176. .rpc_argp = &args,
  2177. .rpc_resp = &res,
  2178. };
  2179. return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
  2180. }
  2181. static int nfs4_proc_readlink(struct inode *inode, struct page *page,
  2182. unsigned int pgbase, unsigned int pglen)
  2183. {
  2184. struct nfs4_exception exception = { };
  2185. int err;
  2186. do {
  2187. err = nfs4_handle_exception(NFS_SERVER(inode),
  2188. _nfs4_proc_readlink(inode, page, pgbase, pglen),
  2189. &exception);
  2190. } while (exception.retry);
  2191. return err;
  2192. }
  2193. /*
  2194. * Got race?
  2195. * We will need to arrange for the VFS layer to provide an atomic open.
  2196. * Until then, this create/open method is prone to inefficiency and race
  2197. * conditions due to the lookup, create, and open VFS calls from sys_open()
  2198. * placed on the wire.
  2199. *
  2200. * Given the above sorry state of affairs, I'm simply sending an OPEN.
  2201. * The file will be opened again in the subsequent VFS open call
  2202. * (nfs4_proc_file_open).
  2203. *
  2204. * The open for read will just hang around to be used by any process that
  2205. * opens the file O_RDONLY. This will all be resolved with the VFS changes.
  2206. */
  2207. static int
  2208. nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
  2209. int flags, struct nameidata *nd)
  2210. {
  2211. struct path path = {
  2212. .mnt = nd->path.mnt,
  2213. .dentry = dentry,
  2214. };
  2215. struct nfs4_state *state;
  2216. struct rpc_cred *cred;
  2217. fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE);
  2218. int status = 0;
  2219. cred = rpc_lookup_cred();
  2220. if (IS_ERR(cred)) {
  2221. status = PTR_ERR(cred);
  2222. goto out;
  2223. }
  2224. state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred);
  2225. d_drop(dentry);
  2226. if (IS_ERR(state)) {
  2227. status = PTR_ERR(state);
  2228. goto out_putcred;
  2229. }
  2230. d_add(dentry, igrab(state->inode));
  2231. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  2232. if (flags & O_EXCL) {
  2233. struct nfs_fattr fattr;
  2234. status = nfs4_do_setattr(state->inode, cred, &fattr, sattr, state);
  2235. if (status == 0)
  2236. nfs_setattr_update_inode(state->inode, sattr);
  2237. nfs_post_op_update_inode(state->inode, &fattr);
  2238. }
  2239. if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
  2240. status = nfs4_intent_set_file(nd, &path, state, fmode);
  2241. else
  2242. nfs4_close_sync(&path, state, fmode);
  2243. out_putcred:
  2244. put_rpccred(cred);
  2245. out:
  2246. return status;
  2247. }
  2248. static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
  2249. {
  2250. struct nfs_server *server = NFS_SERVER(dir);
  2251. struct nfs_removeargs args = {
  2252. .fh = NFS_FH(dir),
  2253. .name.len = name->len,
  2254. .name.name = name->name,
  2255. .bitmask = server->attr_bitmask,
  2256. };
  2257. struct nfs_removeres res = {
  2258. .server = server,
  2259. };
  2260. struct rpc_message msg = {
  2261. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
  2262. .rpc_argp = &args,
  2263. .rpc_resp = &res,
  2264. };
  2265. int status;
  2266. nfs_fattr_init(&res.dir_attr);
  2267. status = nfs4_call_sync(server, &msg, &args, &res, 1);
  2268. if (status == 0) {
  2269. update_changeattr(dir, &res.cinfo);
  2270. nfs_post_op_update_inode(dir, &res.dir_attr);
  2271. }
  2272. return status;
  2273. }
  2274. static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
  2275. {
  2276. struct nfs4_exception exception = { };
  2277. int err;
  2278. do {
  2279. err = nfs4_handle_exception(NFS_SERVER(dir),
  2280. _nfs4_proc_remove(dir, name),
  2281. &exception);
  2282. } while (exception.retry);
  2283. return err;
  2284. }
  2285. static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
  2286. {
  2287. struct nfs_server *server = NFS_SERVER(dir);
  2288. struct nfs_removeargs *args = msg->rpc_argp;
  2289. struct nfs_removeres *res = msg->rpc_resp;
  2290. args->bitmask = server->cache_consistency_bitmask;
  2291. res->server = server;
  2292. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
  2293. }
  2294. static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
  2295. {
  2296. struct nfs_removeres *res = task->tk_msg.rpc_resp;
  2297. nfs4_sequence_done(res->server, &res->seq_res, task->tk_status);
  2298. if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
  2299. return 0;
  2300. nfs4_sequence_free_slot(res->server->nfs_client, &res->seq_res);
  2301. update_changeattr(dir, &res->cinfo);
  2302. nfs_post_op_update_inode(dir, &res->dir_attr);
  2303. return 1;
  2304. }
  2305. static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
  2306. struct inode *new_dir, struct qstr *new_name)
  2307. {
  2308. struct nfs_server *server = NFS_SERVER(old_dir);
  2309. struct nfs4_rename_arg arg = {
  2310. .old_dir = NFS_FH(old_dir),
  2311. .new_dir = NFS_FH(new_dir),
  2312. .old_name = old_name,
  2313. .new_name = new_name,
  2314. .bitmask = server->attr_bitmask,
  2315. };
  2316. struct nfs_fattr old_fattr, new_fattr;
  2317. struct nfs4_rename_res res = {
  2318. .server = server,
  2319. .old_fattr = &old_fattr,
  2320. .new_fattr = &new_fattr,
  2321. };
  2322. struct rpc_message msg = {
  2323. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
  2324. .rpc_argp = &arg,
  2325. .rpc_resp = &res,
  2326. };
  2327. int status;
  2328. nfs_fattr_init(res.old_fattr);
  2329. nfs_fattr_init(res.new_fattr);
  2330. status = nfs4_call_sync(server, &msg, &arg, &res, 1);
  2331. if (!status) {
  2332. update_changeattr(old_dir, &res.old_cinfo);
  2333. nfs_post_op_update_inode(old_dir, res.old_fattr);
  2334. update_changeattr(new_dir, &res.new_cinfo);
  2335. nfs_post_op_update_inode(new_dir, res.new_fattr);
  2336. }
  2337. return status;
  2338. }
  2339. static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
  2340. struct inode *new_dir, struct qstr *new_name)
  2341. {
  2342. struct nfs4_exception exception = { };
  2343. int err;
  2344. do {
  2345. err = nfs4_handle_exception(NFS_SERVER(old_dir),
  2346. _nfs4_proc_rename(old_dir, old_name,
  2347. new_dir, new_name),
  2348. &exception);
  2349. } while (exception.retry);
  2350. return err;
  2351. }
  2352. static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
  2353. {
  2354. struct nfs_server *server = NFS_SERVER(inode);
  2355. struct nfs4_link_arg arg = {
  2356. .fh = NFS_FH(inode),
  2357. .dir_fh = NFS_FH(dir),
  2358. .name = name,
  2359. .bitmask = server->attr_bitmask,
  2360. };
  2361. struct nfs_fattr fattr, dir_attr;
  2362. struct nfs4_link_res res = {
  2363. .server = server,
  2364. .fattr = &fattr,
  2365. .dir_attr = &dir_attr,
  2366. };
  2367. struct rpc_message msg = {
  2368. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
  2369. .rpc_argp = &arg,
  2370. .rpc_resp = &res,
  2371. };
  2372. int status;
  2373. nfs_fattr_init(res.fattr);
  2374. nfs_fattr_init(res.dir_attr);
  2375. status = nfs4_call_sync(server, &msg, &arg, &res, 1);
  2376. if (!status) {
  2377. update_changeattr(dir, &res.cinfo);
  2378. nfs_post_op_update_inode(dir, res.dir_attr);
  2379. nfs_post_op_update_inode(inode, res.fattr);
  2380. }
  2381. return status;
  2382. }
  2383. static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
  2384. {
  2385. struct nfs4_exception exception = { };
  2386. int err;
  2387. do {
  2388. err = nfs4_handle_exception(NFS_SERVER(inode),
  2389. _nfs4_proc_link(inode, dir, name),
  2390. &exception);
  2391. } while (exception.retry);
  2392. return err;
  2393. }
  2394. struct nfs4_createdata {
  2395. struct rpc_message msg;
  2396. struct nfs4_create_arg arg;
  2397. struct nfs4_create_res res;
  2398. struct nfs_fh fh;
  2399. struct nfs_fattr fattr;
  2400. struct nfs_fattr dir_fattr;
  2401. };
  2402. static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
  2403. struct qstr *name, struct iattr *sattr, u32 ftype)
  2404. {
  2405. struct nfs4_createdata *data;
  2406. data = kzalloc(sizeof(*data), GFP_KERNEL);
  2407. if (data != NULL) {
  2408. struct nfs_server *server = NFS_SERVER(dir);
  2409. data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
  2410. data->msg.rpc_argp = &data->arg;
  2411. data->msg.rpc_resp = &data->res;
  2412. data->arg.dir_fh = NFS_FH(dir);
  2413. data->arg.server = server;
  2414. data->arg.name = name;
  2415. data->arg.attrs = sattr;
  2416. data->arg.ftype = ftype;
  2417. data->arg.bitmask = server->attr_bitmask;
  2418. data->res.server = server;
  2419. data->res.fh = &data->fh;
  2420. data->res.fattr = &data->fattr;
  2421. data->res.dir_fattr = &data->dir_fattr;
  2422. nfs_fattr_init(data->res.fattr);
  2423. nfs_fattr_init(data->res.dir_fattr);
  2424. }
  2425. return data;
  2426. }
  2427. static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
  2428. {
  2429. int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
  2430. &data->arg, &data->res, 1);
  2431. if (status == 0) {
  2432. update_changeattr(dir, &data->res.dir_cinfo);
  2433. nfs_post_op_update_inode(dir, data->res.dir_fattr);
  2434. status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
  2435. }
  2436. return status;
  2437. }
  2438. static void nfs4_free_createdata(struct nfs4_createdata *data)
  2439. {
  2440. kfree(data);
  2441. }
  2442. static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
  2443. struct page *page, unsigned int len, struct iattr *sattr)
  2444. {
  2445. struct nfs4_createdata *data;
  2446. int status = -ENAMETOOLONG;
  2447. if (len > NFS4_MAXPATHLEN)
  2448. goto out;
  2449. status = -ENOMEM;
  2450. data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
  2451. if (data == NULL)
  2452. goto out;
  2453. data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
  2454. data->arg.u.symlink.pages = &page;
  2455. data->arg.u.symlink.len = len;
  2456. status = nfs4_do_create(dir, dentry, data);
  2457. nfs4_free_createdata(data);
  2458. out:
  2459. return status;
  2460. }
  2461. static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
  2462. struct page *page, unsigned int len, struct iattr *sattr)
  2463. {
  2464. struct nfs4_exception exception = { };
  2465. int err;
  2466. do {
  2467. err = nfs4_handle_exception(NFS_SERVER(dir),
  2468. _nfs4_proc_symlink(dir, dentry, page,
  2469. len, sattr),
  2470. &exception);
  2471. } while (exception.retry);
  2472. return err;
  2473. }
  2474. static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
  2475. struct iattr *sattr)
  2476. {
  2477. struct nfs4_createdata *data;
  2478. int status = -ENOMEM;
  2479. data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
  2480. if (data == NULL)
  2481. goto out;
  2482. status = nfs4_do_create(dir, dentry, data);
  2483. nfs4_free_createdata(data);
  2484. out:
  2485. return status;
  2486. }
  2487. static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
  2488. struct iattr *sattr)
  2489. {
  2490. struct nfs4_exception exception = { };
  2491. int err;
  2492. do {
  2493. err = nfs4_handle_exception(NFS_SERVER(dir),
  2494. _nfs4_proc_mkdir(dir, dentry, sattr),
  2495. &exception);
  2496. } while (exception.retry);
  2497. return err;
  2498. }
  2499. static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
  2500. u64 cookie, struct page *page, unsigned int count, int plus)
  2501. {
  2502. struct inode *dir = dentry->d_inode;
  2503. struct nfs4_readdir_arg args = {
  2504. .fh = NFS_FH(dir),
  2505. .pages = &page,
  2506. .pgbase = 0,
  2507. .count = count,
  2508. .bitmask = NFS_SERVER(dentry->d_inode)->cache_consistency_bitmask,
  2509. };
  2510. struct nfs4_readdir_res res;
  2511. struct rpc_message msg = {
  2512. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
  2513. .rpc_argp = &args,
  2514. .rpc_resp = &res,
  2515. .rpc_cred = cred,
  2516. };
  2517. int status;
  2518. dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
  2519. dentry->d_parent->d_name.name,
  2520. dentry->d_name.name,
  2521. (unsigned long long)cookie);
  2522. nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
  2523. res.pgbase = args.pgbase;
  2524. status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
  2525. if (status == 0)
  2526. memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
  2527. nfs_invalidate_atime(dir);
  2528. dprintk("%s: returns %d\n", __func__, status);
  2529. return status;
  2530. }
  2531. static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
  2532. u64 cookie, struct page *page, unsigned int count, int plus)
  2533. {
  2534. struct nfs4_exception exception = { };
  2535. int err;
  2536. do {
  2537. err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
  2538. _nfs4_proc_readdir(dentry, cred, cookie,
  2539. page, count, plus),
  2540. &exception);
  2541. } while (exception.retry);
  2542. return err;
  2543. }
  2544. static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
  2545. struct iattr *sattr, dev_t rdev)
  2546. {
  2547. struct nfs4_createdata *data;
  2548. int mode = sattr->ia_mode;
  2549. int status = -ENOMEM;
  2550. BUG_ON(!(sattr->ia_valid & ATTR_MODE));
  2551. BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
  2552. data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
  2553. if (data == NULL)
  2554. goto out;
  2555. if (S_ISFIFO(mode))
  2556. data->arg.ftype = NF4FIFO;
  2557. else if (S_ISBLK(mode)) {
  2558. data->arg.ftype = NF4BLK;
  2559. data->arg.u.device.specdata1 = MAJOR(rdev);
  2560. data->arg.u.device.specdata2 = MINOR(rdev);
  2561. }
  2562. else if (S_ISCHR(mode)) {
  2563. data->arg.ftype = NF4CHR;
  2564. data->arg.u.device.specdata1 = MAJOR(rdev);
  2565. data->arg.u.device.specdata2 = MINOR(rdev);
  2566. }
  2567. status = nfs4_do_create(dir, dentry, data);
  2568. nfs4_free_createdata(data);
  2569. out:
  2570. return status;
  2571. }
  2572. static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
  2573. struct iattr *sattr, dev_t rdev)
  2574. {
  2575. struct nfs4_exception exception = { };
  2576. int err;
  2577. do {
  2578. err = nfs4_handle_exception(NFS_SERVER(dir),
  2579. _nfs4_proc_mknod(dir, dentry, sattr, rdev),
  2580. &exception);
  2581. } while (exception.retry);
  2582. return err;
  2583. }
  2584. static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
  2585. struct nfs_fsstat *fsstat)
  2586. {
  2587. struct nfs4_statfs_arg args = {
  2588. .fh = fhandle,
  2589. .bitmask = server->attr_bitmask,
  2590. };
  2591. struct nfs4_statfs_res res = {
  2592. .fsstat = fsstat,
  2593. };
  2594. struct rpc_message msg = {
  2595. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
  2596. .rpc_argp = &args,
  2597. .rpc_resp = &res,
  2598. };
  2599. nfs_fattr_init(fsstat->fattr);
  2600. return nfs4_call_sync(server, &msg, &args, &res, 0);
  2601. }
  2602. static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
  2603. {
  2604. struct nfs4_exception exception = { };
  2605. int err;
  2606. do {
  2607. err = nfs4_handle_exception(server,
  2608. _nfs4_proc_statfs(server, fhandle, fsstat),
  2609. &exception);
  2610. } while (exception.retry);
  2611. return err;
  2612. }
  2613. static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
  2614. struct nfs_fsinfo *fsinfo)
  2615. {
  2616. struct nfs4_fsinfo_arg args = {
  2617. .fh = fhandle,
  2618. .bitmask = server->attr_bitmask,
  2619. };
  2620. struct nfs4_fsinfo_res res = {
  2621. .fsinfo = fsinfo,
  2622. };
  2623. struct rpc_message msg = {
  2624. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
  2625. .rpc_argp = &args,
  2626. .rpc_resp = &res,
  2627. };
  2628. return nfs4_call_sync(server, &msg, &args, &res, 0);
  2629. }
  2630. static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
  2631. {
  2632. struct nfs4_exception exception = { };
  2633. int err;
  2634. do {
  2635. err = nfs4_handle_exception(server,
  2636. _nfs4_do_fsinfo(server, fhandle, fsinfo),
  2637. &exception);
  2638. } while (exception.retry);
  2639. return err;
  2640. }
  2641. static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
  2642. {
  2643. nfs_fattr_init(fsinfo->fattr);
  2644. return nfs4_do_fsinfo(server, fhandle, fsinfo);
  2645. }
  2646. static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
  2647. struct nfs_pathconf *pathconf)
  2648. {
  2649. struct nfs4_pathconf_arg args = {
  2650. .fh = fhandle,
  2651. .bitmask = server->attr_bitmask,
  2652. };
  2653. struct nfs4_pathconf_res res = {
  2654. .pathconf = pathconf,
  2655. };
  2656. struct rpc_message msg = {
  2657. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
  2658. .rpc_argp = &args,
  2659. .rpc_resp = &res,
  2660. };
  2661. /* None of the pathconf attributes are mandatory to implement */
  2662. if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
  2663. memset(pathconf, 0, sizeof(*pathconf));
  2664. return 0;
  2665. }
  2666. nfs_fattr_init(pathconf->fattr);
  2667. return nfs4_call_sync(server, &msg, &args, &res, 0);
  2668. }
  2669. static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
  2670. struct nfs_pathconf *pathconf)
  2671. {
  2672. struct nfs4_exception exception = { };
  2673. int err;
  2674. do {
  2675. err = nfs4_handle_exception(server,
  2676. _nfs4_proc_pathconf(server, fhandle, pathconf),
  2677. &exception);
  2678. } while (exception.retry);
  2679. return err;
  2680. }
  2681. static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
  2682. {
  2683. struct nfs_server *server = NFS_SERVER(data->inode);
  2684. dprintk("--> %s\n", __func__);
  2685. /* nfs4_sequence_free_slot called in the read rpc_call_done */
  2686. nfs4_sequence_done(server, &data->res.seq_res, task->tk_status);
  2687. if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
  2688. nfs4_restart_rpc(task, server->nfs_client);
  2689. return -EAGAIN;
  2690. }
  2691. nfs_invalidate_atime(data->inode);
  2692. if (task->tk_status > 0)
  2693. renew_lease(server, data->timestamp);
  2694. return 0;
  2695. }
  2696. static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
  2697. {
  2698. data->timestamp = jiffies;
  2699. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
  2700. }
  2701. static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
  2702. {
  2703. struct inode *inode = data->inode;
  2704. /* slot is freed in nfs_writeback_done */
  2705. nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
  2706. task->tk_status);
  2707. if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
  2708. nfs4_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
  2709. return -EAGAIN;
  2710. }
  2711. if (task->tk_status >= 0) {
  2712. renew_lease(NFS_SERVER(inode), data->timestamp);
  2713. nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
  2714. }
  2715. return 0;
  2716. }
  2717. static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
  2718. {
  2719. struct nfs_server *server = NFS_SERVER(data->inode);
  2720. data->args.bitmask = server->cache_consistency_bitmask;
  2721. data->res.server = server;
  2722. data->timestamp = jiffies;
  2723. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
  2724. }
  2725. static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
  2726. {
  2727. struct inode *inode = data->inode;
  2728. nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
  2729. task->tk_status);
  2730. if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
  2731. nfs4_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
  2732. return -EAGAIN;
  2733. }
  2734. nfs4_sequence_free_slot(NFS_SERVER(inode)->nfs_client,
  2735. &data->res.seq_res);
  2736. nfs_refresh_inode(inode, data->res.fattr);
  2737. return 0;
  2738. }
  2739. static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
  2740. {
  2741. struct nfs_server *server = NFS_SERVER(data->inode);
  2742. data->args.bitmask = server->cache_consistency_bitmask;
  2743. data->res.server = server;
  2744. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
  2745. }
  2746. /*
  2747. * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
  2748. * standalone procedure for queueing an asynchronous RENEW.
  2749. */
  2750. static void nfs4_renew_done(struct rpc_task *task, void *data)
  2751. {
  2752. struct nfs_client *clp = (struct nfs_client *)task->tk_msg.rpc_argp;
  2753. unsigned long timestamp = (unsigned long)data;
  2754. if (task->tk_status < 0) {
  2755. /* Unless we're shutting down, schedule state recovery! */
  2756. if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
  2757. nfs4_schedule_state_recovery(clp);
  2758. return;
  2759. }
  2760. spin_lock(&clp->cl_lock);
  2761. if (time_before(clp->cl_last_renewal,timestamp))
  2762. clp->cl_last_renewal = timestamp;
  2763. spin_unlock(&clp->cl_lock);
  2764. }
  2765. static const struct rpc_call_ops nfs4_renew_ops = {
  2766. .rpc_call_done = nfs4_renew_done,
  2767. };
  2768. int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
  2769. {
  2770. struct rpc_message msg = {
  2771. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
  2772. .rpc_argp = clp,
  2773. .rpc_cred = cred,
  2774. };
  2775. return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
  2776. &nfs4_renew_ops, (void *)jiffies);
  2777. }
  2778. int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
  2779. {
  2780. struct rpc_message msg = {
  2781. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
  2782. .rpc_argp = clp,
  2783. .rpc_cred = cred,
  2784. };
  2785. unsigned long now = jiffies;
  2786. int status;
  2787. status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
  2788. if (status < 0)
  2789. return status;
  2790. spin_lock(&clp->cl_lock);
  2791. if (time_before(clp->cl_last_renewal,now))
  2792. clp->cl_last_renewal = now;
  2793. spin_unlock(&clp->cl_lock);
  2794. return 0;
  2795. }
  2796. static inline int nfs4_server_supports_acls(struct nfs_server *server)
  2797. {
  2798. return (server->caps & NFS_CAP_ACLS)
  2799. && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
  2800. && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
  2801. }
  2802. /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
  2803. * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
  2804. * the stack.
  2805. */
  2806. #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
  2807. static void buf_to_pages(const void *buf, size_t buflen,
  2808. struct page **pages, unsigned int *pgbase)
  2809. {
  2810. const void *p = buf;
  2811. *pgbase = offset_in_page(buf);
  2812. p -= *pgbase;
  2813. while (p < buf + buflen) {
  2814. *(pages++) = virt_to_page(p);
  2815. p += PAGE_CACHE_SIZE;
  2816. }
  2817. }
  2818. struct nfs4_cached_acl {
  2819. int cached;
  2820. size_t len;
  2821. char data[0];
  2822. };
  2823. static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
  2824. {
  2825. struct nfs_inode *nfsi = NFS_I(inode);
  2826. spin_lock(&inode->i_lock);
  2827. kfree(nfsi->nfs4_acl);
  2828. nfsi->nfs4_acl = acl;
  2829. spin_unlock(&inode->i_lock);
  2830. }
  2831. static void nfs4_zap_acl_attr(struct inode *inode)
  2832. {
  2833. nfs4_set_cached_acl(inode, NULL);
  2834. }
  2835. static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
  2836. {
  2837. struct nfs_inode *nfsi = NFS_I(inode);
  2838. struct nfs4_cached_acl *acl;
  2839. int ret = -ENOENT;
  2840. spin_lock(&inode->i_lock);
  2841. acl = nfsi->nfs4_acl;
  2842. if (acl == NULL)
  2843. goto out;
  2844. if (buf == NULL) /* user is just asking for length */
  2845. goto out_len;
  2846. if (acl->cached == 0)
  2847. goto out;
  2848. ret = -ERANGE; /* see getxattr(2) man page */
  2849. if (acl->len > buflen)
  2850. goto out;
  2851. memcpy(buf, acl->data, acl->len);
  2852. out_len:
  2853. ret = acl->len;
  2854. out:
  2855. spin_unlock(&inode->i_lock);
  2856. return ret;
  2857. }
  2858. static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
  2859. {
  2860. struct nfs4_cached_acl *acl;
  2861. if (buf && acl_len <= PAGE_SIZE) {
  2862. acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
  2863. if (acl == NULL)
  2864. goto out;
  2865. acl->cached = 1;
  2866. memcpy(acl->data, buf, acl_len);
  2867. } else {
  2868. acl = kmalloc(sizeof(*acl), GFP_KERNEL);
  2869. if (acl == NULL)
  2870. goto out;
  2871. acl->cached = 0;
  2872. }
  2873. acl->len = acl_len;
  2874. out:
  2875. nfs4_set_cached_acl(inode, acl);
  2876. }
  2877. static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
  2878. {
  2879. struct page *pages[NFS4ACL_MAXPAGES];
  2880. struct nfs_getaclargs args = {
  2881. .fh = NFS_FH(inode),
  2882. .acl_pages = pages,
  2883. .acl_len = buflen,
  2884. };
  2885. struct nfs_getaclres res = {
  2886. .acl_len = buflen,
  2887. };
  2888. void *resp_buf;
  2889. struct rpc_message msg = {
  2890. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
  2891. .rpc_argp = &args,
  2892. .rpc_resp = &res,
  2893. };
  2894. struct page *localpage = NULL;
  2895. int ret;
  2896. if (buflen < PAGE_SIZE) {
  2897. /* As long as we're doing a round trip to the server anyway,
  2898. * let's be prepared for a page of acl data. */
  2899. localpage = alloc_page(GFP_KERNEL);
  2900. resp_buf = page_address(localpage);
  2901. if (localpage == NULL)
  2902. return -ENOMEM;
  2903. args.acl_pages[0] = localpage;
  2904. args.acl_pgbase = 0;
  2905. args.acl_len = PAGE_SIZE;
  2906. } else {
  2907. resp_buf = buf;
  2908. buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
  2909. }
  2910. ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
  2911. if (ret)
  2912. goto out_free;
  2913. if (res.acl_len > args.acl_len)
  2914. nfs4_write_cached_acl(inode, NULL, res.acl_len);
  2915. else
  2916. nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
  2917. if (buf) {
  2918. ret = -ERANGE;
  2919. if (res.acl_len > buflen)
  2920. goto out_free;
  2921. if (localpage)
  2922. memcpy(buf, resp_buf, res.acl_len);
  2923. }
  2924. ret = res.acl_len;
  2925. out_free:
  2926. if (localpage)
  2927. __free_page(localpage);
  2928. return ret;
  2929. }
  2930. static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
  2931. {
  2932. struct nfs4_exception exception = { };
  2933. ssize_t ret;
  2934. do {
  2935. ret = __nfs4_get_acl_uncached(inode, buf, buflen);
  2936. if (ret >= 0)
  2937. break;
  2938. ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
  2939. } while (exception.retry);
  2940. return ret;
  2941. }
  2942. static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
  2943. {
  2944. struct nfs_server *server = NFS_SERVER(inode);
  2945. int ret;
  2946. if (!nfs4_server_supports_acls(server))
  2947. return -EOPNOTSUPP;
  2948. ret = nfs_revalidate_inode(server, inode);
  2949. if (ret < 0)
  2950. return ret;
  2951. ret = nfs4_read_cached_acl(inode, buf, buflen);
  2952. if (ret != -ENOENT)
  2953. return ret;
  2954. return nfs4_get_acl_uncached(inode, buf, buflen);
  2955. }
  2956. static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
  2957. {
  2958. struct nfs_server *server = NFS_SERVER(inode);
  2959. struct page *pages[NFS4ACL_MAXPAGES];
  2960. struct nfs_setaclargs arg = {
  2961. .fh = NFS_FH(inode),
  2962. .acl_pages = pages,
  2963. .acl_len = buflen,
  2964. };
  2965. struct nfs_setaclres res;
  2966. struct rpc_message msg = {
  2967. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
  2968. .rpc_argp = &arg,
  2969. .rpc_resp = &res,
  2970. };
  2971. int ret;
  2972. if (!nfs4_server_supports_acls(server))
  2973. return -EOPNOTSUPP;
  2974. nfs_inode_return_delegation(inode);
  2975. buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
  2976. ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
  2977. nfs_access_zap_cache(inode);
  2978. nfs_zap_acl_cache(inode);
  2979. return ret;
  2980. }
  2981. static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
  2982. {
  2983. struct nfs4_exception exception = { };
  2984. int err;
  2985. do {
  2986. err = nfs4_handle_exception(NFS_SERVER(inode),
  2987. __nfs4_proc_set_acl(inode, buf, buflen),
  2988. &exception);
  2989. } while (exception.retry);
  2990. return err;
  2991. }
  2992. static int
  2993. _nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs_client *clp, struct nfs4_state *state)
  2994. {
  2995. if (!clp || task->tk_status >= 0)
  2996. return 0;
  2997. switch(task->tk_status) {
  2998. case -NFS4ERR_ADMIN_REVOKED:
  2999. case -NFS4ERR_BAD_STATEID:
  3000. case -NFS4ERR_OPENMODE:
  3001. if (state == NULL)
  3002. break;
  3003. nfs4_state_mark_reclaim_nograce(clp, state);
  3004. case -NFS4ERR_STALE_CLIENTID:
  3005. case -NFS4ERR_STALE_STATEID:
  3006. case -NFS4ERR_EXPIRED:
  3007. rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
  3008. nfs4_schedule_state_recovery(clp);
  3009. if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
  3010. rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
  3011. task->tk_status = 0;
  3012. return -EAGAIN;
  3013. #if defined(CONFIG_NFS_V4_1)
  3014. case -NFS4ERR_BADSESSION:
  3015. case -NFS4ERR_BADSLOT:
  3016. case -NFS4ERR_BAD_HIGH_SLOT:
  3017. case -NFS4ERR_DEADSESSION:
  3018. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  3019. case -NFS4ERR_SEQ_FALSE_RETRY:
  3020. case -NFS4ERR_SEQ_MISORDERED:
  3021. dprintk("%s ERROR %d, Reset session\n", __func__,
  3022. task->tk_status);
  3023. set_bit(NFS4CLNT_SESSION_SETUP, &clp->cl_state);
  3024. task->tk_status = 0;
  3025. return -EAGAIN;
  3026. #endif /* CONFIG_NFS_V4_1 */
  3027. case -NFS4ERR_DELAY:
  3028. if (server)
  3029. nfs_inc_server_stats(server, NFSIOS_DELAY);
  3030. case -NFS4ERR_GRACE:
  3031. rpc_delay(task, NFS4_POLL_RETRY_MAX);
  3032. task->tk_status = 0;
  3033. return -EAGAIN;
  3034. case -NFS4ERR_OLD_STATEID:
  3035. task->tk_status = 0;
  3036. return -EAGAIN;
  3037. }
  3038. task->tk_status = nfs4_map_errors(task->tk_status);
  3039. return 0;
  3040. }
  3041. static int
  3042. nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
  3043. {
  3044. return _nfs4_async_handle_error(task, server, server->nfs_client, state);
  3045. }
  3046. int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
  3047. {
  3048. nfs4_verifier sc_verifier;
  3049. struct nfs4_setclientid setclientid = {
  3050. .sc_verifier = &sc_verifier,
  3051. .sc_prog = program,
  3052. };
  3053. struct rpc_message msg = {
  3054. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
  3055. .rpc_argp = &setclientid,
  3056. .rpc_resp = clp,
  3057. .rpc_cred = cred,
  3058. };
  3059. __be32 *p;
  3060. int loop = 0;
  3061. int status;
  3062. p = (__be32*)sc_verifier.data;
  3063. *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
  3064. *p = htonl((u32)clp->cl_boot_time.tv_nsec);
  3065. for(;;) {
  3066. setclientid.sc_name_len = scnprintf(setclientid.sc_name,
  3067. sizeof(setclientid.sc_name), "%s/%s %s %s %u",
  3068. clp->cl_ipaddr,
  3069. rpc_peeraddr2str(clp->cl_rpcclient,
  3070. RPC_DISPLAY_ADDR),
  3071. rpc_peeraddr2str(clp->cl_rpcclient,
  3072. RPC_DISPLAY_PROTO),
  3073. clp->cl_rpcclient->cl_auth->au_ops->au_name,
  3074. clp->cl_id_uniquifier);
  3075. setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
  3076. sizeof(setclientid.sc_netid),
  3077. rpc_peeraddr2str(clp->cl_rpcclient,
  3078. RPC_DISPLAY_NETID));
  3079. setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
  3080. sizeof(setclientid.sc_uaddr), "%s.%u.%u",
  3081. clp->cl_ipaddr, port >> 8, port & 255);
  3082. status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
  3083. if (status != -NFS4ERR_CLID_INUSE)
  3084. break;
  3085. if (signalled())
  3086. break;
  3087. if (loop++ & 1)
  3088. ssleep(clp->cl_lease_time + 1);
  3089. else
  3090. if (++clp->cl_id_uniquifier == 0)
  3091. break;
  3092. }
  3093. return status;
  3094. }
  3095. static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
  3096. {
  3097. struct nfs_fsinfo fsinfo;
  3098. struct rpc_message msg = {
  3099. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
  3100. .rpc_argp = clp,
  3101. .rpc_resp = &fsinfo,
  3102. .rpc_cred = cred,
  3103. };
  3104. unsigned long now;
  3105. int status;
  3106. now = jiffies;
  3107. status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
  3108. if (status == 0) {
  3109. spin_lock(&clp->cl_lock);
  3110. clp->cl_lease_time = fsinfo.lease_time * HZ;
  3111. clp->cl_last_renewal = now;
  3112. spin_unlock(&clp->cl_lock);
  3113. }
  3114. return status;
  3115. }
  3116. int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
  3117. {
  3118. long timeout = 0;
  3119. int err;
  3120. do {
  3121. err = _nfs4_proc_setclientid_confirm(clp, cred);
  3122. switch (err) {
  3123. case 0:
  3124. return err;
  3125. case -NFS4ERR_RESOURCE:
  3126. /* The IBM lawyers misread another document! */
  3127. case -NFS4ERR_DELAY:
  3128. err = nfs4_delay(clp->cl_rpcclient, &timeout);
  3129. }
  3130. } while (err == 0);
  3131. return err;
  3132. }
  3133. struct nfs4_delegreturndata {
  3134. struct nfs4_delegreturnargs args;
  3135. struct nfs4_delegreturnres res;
  3136. struct nfs_fh fh;
  3137. nfs4_stateid stateid;
  3138. unsigned long timestamp;
  3139. struct nfs_fattr fattr;
  3140. int rpc_status;
  3141. };
  3142. static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
  3143. {
  3144. struct nfs4_delegreturndata *data = calldata;
  3145. nfs4_sequence_done_free_slot(data->res.server, &data->res.seq_res,
  3146. task->tk_status);
  3147. data->rpc_status = task->tk_status;
  3148. if (data->rpc_status == 0)
  3149. renew_lease(data->res.server, data->timestamp);
  3150. }
  3151. static void nfs4_delegreturn_release(void *calldata)
  3152. {
  3153. kfree(calldata);
  3154. }
  3155. #if defined(CONFIG_NFS_V4_1)
  3156. static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
  3157. {
  3158. struct nfs4_delegreturndata *d_data;
  3159. d_data = (struct nfs4_delegreturndata *)data;
  3160. if (nfs4_setup_sequence(d_data->res.server->nfs_client,
  3161. &d_data->args.seq_args,
  3162. &d_data->res.seq_res, 1, task))
  3163. return;
  3164. rpc_call_start(task);
  3165. }
  3166. #endif /* CONFIG_NFS_V4_1 */
  3167. static const struct rpc_call_ops nfs4_delegreturn_ops = {
  3168. #if defined(CONFIG_NFS_V4_1)
  3169. .rpc_call_prepare = nfs4_delegreturn_prepare,
  3170. #endif /* CONFIG_NFS_V4_1 */
  3171. .rpc_call_done = nfs4_delegreturn_done,
  3172. .rpc_release = nfs4_delegreturn_release,
  3173. };
  3174. static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
  3175. {
  3176. struct nfs4_delegreturndata *data;
  3177. struct nfs_server *server = NFS_SERVER(inode);
  3178. struct rpc_task *task;
  3179. struct rpc_message msg = {
  3180. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
  3181. .rpc_cred = cred,
  3182. };
  3183. struct rpc_task_setup task_setup_data = {
  3184. .rpc_client = server->client,
  3185. .rpc_message = &msg,
  3186. .callback_ops = &nfs4_delegreturn_ops,
  3187. .flags = RPC_TASK_ASYNC,
  3188. };
  3189. int status = 0;
  3190. data = kzalloc(sizeof(*data), GFP_KERNEL);
  3191. if (data == NULL)
  3192. return -ENOMEM;
  3193. data->args.fhandle = &data->fh;
  3194. data->args.stateid = &data->stateid;
  3195. data->args.bitmask = server->attr_bitmask;
  3196. nfs_copy_fh(&data->fh, NFS_FH(inode));
  3197. memcpy(&data->stateid, stateid, sizeof(data->stateid));
  3198. data->res.fattr = &data->fattr;
  3199. data->res.server = server;
  3200. data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
  3201. nfs_fattr_init(data->res.fattr);
  3202. data->timestamp = jiffies;
  3203. data->rpc_status = 0;
  3204. task_setup_data.callback_data = data;
  3205. msg.rpc_argp = &data->args,
  3206. msg.rpc_resp = &data->res,
  3207. task = rpc_run_task(&task_setup_data);
  3208. if (IS_ERR(task))
  3209. return PTR_ERR(task);
  3210. if (!issync)
  3211. goto out;
  3212. status = nfs4_wait_for_completion_rpc_task(task);
  3213. if (status != 0)
  3214. goto out;
  3215. status = data->rpc_status;
  3216. if (status != 0)
  3217. goto out;
  3218. nfs_refresh_inode(inode, &data->fattr);
  3219. out:
  3220. rpc_put_task(task);
  3221. return status;
  3222. }
  3223. int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
  3224. {
  3225. struct nfs_server *server = NFS_SERVER(inode);
  3226. struct nfs4_exception exception = { };
  3227. int err;
  3228. do {
  3229. err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
  3230. switch (err) {
  3231. case -NFS4ERR_STALE_STATEID:
  3232. case -NFS4ERR_EXPIRED:
  3233. case 0:
  3234. return 0;
  3235. }
  3236. err = nfs4_handle_exception(server, err, &exception);
  3237. } while (exception.retry);
  3238. return err;
  3239. }
  3240. #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
  3241. #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
  3242. /*
  3243. * sleep, with exponential backoff, and retry the LOCK operation.
  3244. */
  3245. static unsigned long
  3246. nfs4_set_lock_task_retry(unsigned long timeout)
  3247. {
  3248. schedule_timeout_killable(timeout);
  3249. timeout <<= 1;
  3250. if (timeout > NFS4_LOCK_MAXTIMEOUT)
  3251. return NFS4_LOCK_MAXTIMEOUT;
  3252. return timeout;
  3253. }
  3254. static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  3255. {
  3256. struct inode *inode = state->inode;
  3257. struct nfs_server *server = NFS_SERVER(inode);
  3258. struct nfs_client *clp = server->nfs_client;
  3259. struct nfs_lockt_args arg = {
  3260. .fh = NFS_FH(inode),
  3261. .fl = request,
  3262. };
  3263. struct nfs_lockt_res res = {
  3264. .denied = request,
  3265. };
  3266. struct rpc_message msg = {
  3267. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
  3268. .rpc_argp = &arg,
  3269. .rpc_resp = &res,
  3270. .rpc_cred = state->owner->so_cred,
  3271. };
  3272. struct nfs4_lock_state *lsp;
  3273. int status;
  3274. arg.lock_owner.clientid = clp->cl_clientid;
  3275. status = nfs4_set_lock_state(state, request);
  3276. if (status != 0)
  3277. goto out;
  3278. lsp = request->fl_u.nfs4_fl.owner;
  3279. arg.lock_owner.id = lsp->ls_id.id;
  3280. status = nfs4_call_sync(server, &msg, &arg, &res, 1);
  3281. switch (status) {
  3282. case 0:
  3283. request->fl_type = F_UNLCK;
  3284. break;
  3285. case -NFS4ERR_DENIED:
  3286. status = 0;
  3287. }
  3288. request->fl_ops->fl_release_private(request);
  3289. out:
  3290. return status;
  3291. }
  3292. static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  3293. {
  3294. struct nfs4_exception exception = { };
  3295. int err;
  3296. do {
  3297. err = nfs4_handle_exception(NFS_SERVER(state->inode),
  3298. _nfs4_proc_getlk(state, cmd, request),
  3299. &exception);
  3300. } while (exception.retry);
  3301. return err;
  3302. }
  3303. static int do_vfs_lock(struct file *file, struct file_lock *fl)
  3304. {
  3305. int res = 0;
  3306. switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
  3307. case FL_POSIX:
  3308. res = posix_lock_file_wait(file, fl);
  3309. break;
  3310. case FL_FLOCK:
  3311. res = flock_lock_file_wait(file, fl);
  3312. break;
  3313. default:
  3314. BUG();
  3315. }
  3316. return res;
  3317. }
  3318. struct nfs4_unlockdata {
  3319. struct nfs_locku_args arg;
  3320. struct nfs_locku_res res;
  3321. struct nfs4_lock_state *lsp;
  3322. struct nfs_open_context *ctx;
  3323. struct file_lock fl;
  3324. const struct nfs_server *server;
  3325. unsigned long timestamp;
  3326. };
  3327. static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
  3328. struct nfs_open_context *ctx,
  3329. struct nfs4_lock_state *lsp,
  3330. struct nfs_seqid *seqid)
  3331. {
  3332. struct nfs4_unlockdata *p;
  3333. struct inode *inode = lsp->ls_state->inode;
  3334. p = kzalloc(sizeof(*p), GFP_KERNEL);
  3335. if (p == NULL)
  3336. return NULL;
  3337. p->arg.fh = NFS_FH(inode);
  3338. p->arg.fl = &p->fl;
  3339. p->arg.seqid = seqid;
  3340. p->res.seqid = seqid;
  3341. p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
  3342. p->arg.stateid = &lsp->ls_stateid;
  3343. p->lsp = lsp;
  3344. atomic_inc(&lsp->ls_count);
  3345. /* Ensure we don't close file until we're done freeing locks! */
  3346. p->ctx = get_nfs_open_context(ctx);
  3347. memcpy(&p->fl, fl, sizeof(p->fl));
  3348. p->server = NFS_SERVER(inode);
  3349. return p;
  3350. }
  3351. static void nfs4_locku_release_calldata(void *data)
  3352. {
  3353. struct nfs4_unlockdata *calldata = data;
  3354. nfs_free_seqid(calldata->arg.seqid);
  3355. nfs4_put_lock_state(calldata->lsp);
  3356. put_nfs_open_context(calldata->ctx);
  3357. kfree(calldata);
  3358. }
  3359. static void nfs4_locku_done(struct rpc_task *task, void *data)
  3360. {
  3361. struct nfs4_unlockdata *calldata = data;
  3362. nfs4_sequence_done(calldata->server, &calldata->res.seq_res,
  3363. task->tk_status);
  3364. if (RPC_ASSASSINATED(task))
  3365. return;
  3366. switch (task->tk_status) {
  3367. case 0:
  3368. memcpy(calldata->lsp->ls_stateid.data,
  3369. calldata->res.stateid.data,
  3370. sizeof(calldata->lsp->ls_stateid.data));
  3371. renew_lease(calldata->server, calldata->timestamp);
  3372. break;
  3373. case -NFS4ERR_BAD_STATEID:
  3374. case -NFS4ERR_OLD_STATEID:
  3375. case -NFS4ERR_STALE_STATEID:
  3376. case -NFS4ERR_EXPIRED:
  3377. break;
  3378. default:
  3379. if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
  3380. nfs4_restart_rpc(task,
  3381. calldata->server->nfs_client);
  3382. }
  3383. nfs4_sequence_free_slot(calldata->server->nfs_client,
  3384. &calldata->res.seq_res);
  3385. }
  3386. static void nfs4_locku_prepare(struct rpc_task *task, void *data)
  3387. {
  3388. struct nfs4_unlockdata *calldata = data;
  3389. if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
  3390. return;
  3391. if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
  3392. /* Note: exit _without_ running nfs4_locku_done */
  3393. task->tk_action = NULL;
  3394. return;
  3395. }
  3396. calldata->timestamp = jiffies;
  3397. if (nfs4_setup_sequence(calldata->server->nfs_client,
  3398. &calldata->arg.seq_args,
  3399. &calldata->res.seq_res, 1, task))
  3400. return;
  3401. rpc_call_start(task);
  3402. }
  3403. static const struct rpc_call_ops nfs4_locku_ops = {
  3404. .rpc_call_prepare = nfs4_locku_prepare,
  3405. .rpc_call_done = nfs4_locku_done,
  3406. .rpc_release = nfs4_locku_release_calldata,
  3407. };
  3408. static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
  3409. struct nfs_open_context *ctx,
  3410. struct nfs4_lock_state *lsp,
  3411. struct nfs_seqid *seqid)
  3412. {
  3413. struct nfs4_unlockdata *data;
  3414. struct rpc_message msg = {
  3415. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
  3416. .rpc_cred = ctx->cred,
  3417. };
  3418. struct rpc_task_setup task_setup_data = {
  3419. .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
  3420. .rpc_message = &msg,
  3421. .callback_ops = &nfs4_locku_ops,
  3422. .workqueue = nfsiod_workqueue,
  3423. .flags = RPC_TASK_ASYNC,
  3424. };
  3425. /* Ensure this is an unlock - when canceling a lock, the
  3426. * canceled lock is passed in, and it won't be an unlock.
  3427. */
  3428. fl->fl_type = F_UNLCK;
  3429. data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
  3430. if (data == NULL) {
  3431. nfs_free_seqid(seqid);
  3432. return ERR_PTR(-ENOMEM);
  3433. }
  3434. msg.rpc_argp = &data->arg,
  3435. msg.rpc_resp = &data->res,
  3436. task_setup_data.callback_data = data;
  3437. return rpc_run_task(&task_setup_data);
  3438. }
  3439. static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
  3440. {
  3441. struct nfs_inode *nfsi = NFS_I(state->inode);
  3442. struct nfs_seqid *seqid;
  3443. struct nfs4_lock_state *lsp;
  3444. struct rpc_task *task;
  3445. int status = 0;
  3446. unsigned char fl_flags = request->fl_flags;
  3447. status = nfs4_set_lock_state(state, request);
  3448. /* Unlock _before_ we do the RPC call */
  3449. request->fl_flags |= FL_EXISTS;
  3450. down_read(&nfsi->rwsem);
  3451. if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
  3452. up_read(&nfsi->rwsem);
  3453. goto out;
  3454. }
  3455. up_read(&nfsi->rwsem);
  3456. if (status != 0)
  3457. goto out;
  3458. /* Is this a delegated lock? */
  3459. if (test_bit(NFS_DELEGATED_STATE, &state->flags))
  3460. goto out;
  3461. lsp = request->fl_u.nfs4_fl.owner;
  3462. seqid = nfs_alloc_seqid(&lsp->ls_seqid);
  3463. status = -ENOMEM;
  3464. if (seqid == NULL)
  3465. goto out;
  3466. task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
  3467. status = PTR_ERR(task);
  3468. if (IS_ERR(task))
  3469. goto out;
  3470. status = nfs4_wait_for_completion_rpc_task(task);
  3471. rpc_put_task(task);
  3472. out:
  3473. request->fl_flags = fl_flags;
  3474. return status;
  3475. }
  3476. struct nfs4_lockdata {
  3477. struct nfs_lock_args arg;
  3478. struct nfs_lock_res res;
  3479. struct nfs4_lock_state *lsp;
  3480. struct nfs_open_context *ctx;
  3481. struct file_lock fl;
  3482. unsigned long timestamp;
  3483. int rpc_status;
  3484. int cancelled;
  3485. struct nfs_server *server;
  3486. };
  3487. static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
  3488. struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
  3489. {
  3490. struct nfs4_lockdata *p;
  3491. struct inode *inode = lsp->ls_state->inode;
  3492. struct nfs_server *server = NFS_SERVER(inode);
  3493. p = kzalloc(sizeof(*p), GFP_KERNEL);
  3494. if (p == NULL)
  3495. return NULL;
  3496. p->arg.fh = NFS_FH(inode);
  3497. p->arg.fl = &p->fl;
  3498. p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid);
  3499. if (p->arg.open_seqid == NULL)
  3500. goto out_free;
  3501. p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
  3502. if (p->arg.lock_seqid == NULL)
  3503. goto out_free_seqid;
  3504. p->arg.lock_stateid = &lsp->ls_stateid;
  3505. p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
  3506. p->arg.lock_owner.id = lsp->ls_id.id;
  3507. p->res.lock_seqid = p->arg.lock_seqid;
  3508. p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
  3509. p->lsp = lsp;
  3510. p->server = server;
  3511. atomic_inc(&lsp->ls_count);
  3512. p->ctx = get_nfs_open_context(ctx);
  3513. memcpy(&p->fl, fl, sizeof(p->fl));
  3514. return p;
  3515. out_free_seqid:
  3516. nfs_free_seqid(p->arg.open_seqid);
  3517. out_free:
  3518. kfree(p);
  3519. return NULL;
  3520. }
  3521. static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
  3522. {
  3523. struct nfs4_lockdata *data = calldata;
  3524. struct nfs4_state *state = data->lsp->ls_state;
  3525. dprintk("%s: begin!\n", __func__);
  3526. if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
  3527. return;
  3528. /* Do we need to do an open_to_lock_owner? */
  3529. if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
  3530. if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
  3531. return;
  3532. data->arg.open_stateid = &state->stateid;
  3533. data->arg.new_lock_owner = 1;
  3534. data->res.open_seqid = data->arg.open_seqid;
  3535. } else
  3536. data->arg.new_lock_owner = 0;
  3537. data->timestamp = jiffies;
  3538. if (nfs4_setup_sequence(data->server->nfs_client, &data->arg.seq_args,
  3539. &data->res.seq_res, 1, task))
  3540. return;
  3541. rpc_call_start(task);
  3542. dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
  3543. }
  3544. static void nfs4_lock_done(struct rpc_task *task, void *calldata)
  3545. {
  3546. struct nfs4_lockdata *data = calldata;
  3547. dprintk("%s: begin!\n", __func__);
  3548. nfs4_sequence_done_free_slot(data->server, &data->res.seq_res,
  3549. task->tk_status);
  3550. data->rpc_status = task->tk_status;
  3551. if (RPC_ASSASSINATED(task))
  3552. goto out;
  3553. if (data->arg.new_lock_owner != 0) {
  3554. if (data->rpc_status == 0)
  3555. nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
  3556. else
  3557. goto out;
  3558. }
  3559. if (data->rpc_status == 0) {
  3560. memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
  3561. sizeof(data->lsp->ls_stateid.data));
  3562. data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
  3563. renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
  3564. }
  3565. out:
  3566. dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
  3567. }
  3568. static void nfs4_lock_release(void *calldata)
  3569. {
  3570. struct nfs4_lockdata *data = calldata;
  3571. dprintk("%s: begin!\n", __func__);
  3572. nfs_free_seqid(data->arg.open_seqid);
  3573. if (data->cancelled != 0) {
  3574. struct rpc_task *task;
  3575. task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
  3576. data->arg.lock_seqid);
  3577. if (!IS_ERR(task))
  3578. rpc_put_task(task);
  3579. dprintk("%s: cancelling lock!\n", __func__);
  3580. } else
  3581. nfs_free_seqid(data->arg.lock_seqid);
  3582. nfs4_put_lock_state(data->lsp);
  3583. put_nfs_open_context(data->ctx);
  3584. kfree(data);
  3585. dprintk("%s: done!\n", __func__);
  3586. }
  3587. static const struct rpc_call_ops nfs4_lock_ops = {
  3588. .rpc_call_prepare = nfs4_lock_prepare,
  3589. .rpc_call_done = nfs4_lock_done,
  3590. .rpc_release = nfs4_lock_release,
  3591. };
  3592. static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int reclaim)
  3593. {
  3594. struct nfs4_lockdata *data;
  3595. struct rpc_task *task;
  3596. struct rpc_message msg = {
  3597. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
  3598. .rpc_cred = state->owner->so_cred,
  3599. };
  3600. struct rpc_task_setup task_setup_data = {
  3601. .rpc_client = NFS_CLIENT(state->inode),
  3602. .rpc_message = &msg,
  3603. .callback_ops = &nfs4_lock_ops,
  3604. .workqueue = nfsiod_workqueue,
  3605. .flags = RPC_TASK_ASYNC,
  3606. };
  3607. int ret;
  3608. dprintk("%s: begin!\n", __func__);
  3609. data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
  3610. fl->fl_u.nfs4_fl.owner);
  3611. if (data == NULL)
  3612. return -ENOMEM;
  3613. if (IS_SETLKW(cmd))
  3614. data->arg.block = 1;
  3615. if (reclaim != 0)
  3616. data->arg.reclaim = 1;
  3617. msg.rpc_argp = &data->arg,
  3618. msg.rpc_resp = &data->res,
  3619. task_setup_data.callback_data = data;
  3620. task = rpc_run_task(&task_setup_data);
  3621. if (IS_ERR(task))
  3622. return PTR_ERR(task);
  3623. ret = nfs4_wait_for_completion_rpc_task(task);
  3624. if (ret == 0) {
  3625. ret = data->rpc_status;
  3626. } else
  3627. data->cancelled = 1;
  3628. rpc_put_task(task);
  3629. dprintk("%s: done, ret = %d!\n", __func__, ret);
  3630. return ret;
  3631. }
  3632. static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
  3633. {
  3634. struct nfs_server *server = NFS_SERVER(state->inode);
  3635. struct nfs4_exception exception = { };
  3636. int err;
  3637. do {
  3638. /* Cache the lock if possible... */
  3639. if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
  3640. return 0;
  3641. err = _nfs4_do_setlk(state, F_SETLK, request, 1);
  3642. if (err != -NFS4ERR_DELAY)
  3643. break;
  3644. nfs4_handle_exception(server, err, &exception);
  3645. } while (exception.retry);
  3646. return err;
  3647. }
  3648. static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
  3649. {
  3650. struct nfs_server *server = NFS_SERVER(state->inode);
  3651. struct nfs4_exception exception = { };
  3652. int err;
  3653. err = nfs4_set_lock_state(state, request);
  3654. if (err != 0)
  3655. return err;
  3656. do {
  3657. if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
  3658. return 0;
  3659. err = _nfs4_do_setlk(state, F_SETLK, request, 0);
  3660. if (err != -NFS4ERR_DELAY)
  3661. break;
  3662. nfs4_handle_exception(server, err, &exception);
  3663. } while (exception.retry);
  3664. return err;
  3665. }
  3666. static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  3667. {
  3668. struct nfs_inode *nfsi = NFS_I(state->inode);
  3669. unsigned char fl_flags = request->fl_flags;
  3670. int status;
  3671. /* Is this a delegated open? */
  3672. status = nfs4_set_lock_state(state, request);
  3673. if (status != 0)
  3674. goto out;
  3675. request->fl_flags |= FL_ACCESS;
  3676. status = do_vfs_lock(request->fl_file, request);
  3677. if (status < 0)
  3678. goto out;
  3679. down_read(&nfsi->rwsem);
  3680. if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
  3681. /* Yes: cache locks! */
  3682. /* ...but avoid races with delegation recall... */
  3683. request->fl_flags = fl_flags & ~FL_SLEEP;
  3684. status = do_vfs_lock(request->fl_file, request);
  3685. goto out_unlock;
  3686. }
  3687. status = _nfs4_do_setlk(state, cmd, request, 0);
  3688. if (status != 0)
  3689. goto out_unlock;
  3690. /* Note: we always want to sleep here! */
  3691. request->fl_flags = fl_flags | FL_SLEEP;
  3692. if (do_vfs_lock(request->fl_file, request) < 0)
  3693. printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
  3694. out_unlock:
  3695. up_read(&nfsi->rwsem);
  3696. out:
  3697. request->fl_flags = fl_flags;
  3698. return status;
  3699. }
  3700. static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  3701. {
  3702. struct nfs4_exception exception = { };
  3703. int err;
  3704. do {
  3705. err = _nfs4_proc_setlk(state, cmd, request);
  3706. if (err == -NFS4ERR_DENIED)
  3707. err = -EAGAIN;
  3708. err = nfs4_handle_exception(NFS_SERVER(state->inode),
  3709. err, &exception);
  3710. } while (exception.retry);
  3711. return err;
  3712. }
  3713. static int
  3714. nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
  3715. {
  3716. struct nfs_open_context *ctx;
  3717. struct nfs4_state *state;
  3718. unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
  3719. int status;
  3720. /* verify open state */
  3721. ctx = nfs_file_open_context(filp);
  3722. state = ctx->state;
  3723. if (request->fl_start < 0 || request->fl_end < 0)
  3724. return -EINVAL;
  3725. if (IS_GETLK(cmd)) {
  3726. if (state != NULL)
  3727. return nfs4_proc_getlk(state, F_GETLK, request);
  3728. return 0;
  3729. }
  3730. if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
  3731. return -EINVAL;
  3732. if (request->fl_type == F_UNLCK) {
  3733. if (state != NULL)
  3734. return nfs4_proc_unlck(state, cmd, request);
  3735. return 0;
  3736. }
  3737. if (state == NULL)
  3738. return -ENOLCK;
  3739. do {
  3740. status = nfs4_proc_setlk(state, cmd, request);
  3741. if ((status != -EAGAIN) || IS_SETLK(cmd))
  3742. break;
  3743. timeout = nfs4_set_lock_task_retry(timeout);
  3744. status = -ERESTARTSYS;
  3745. if (signalled())
  3746. break;
  3747. } while(status < 0);
  3748. return status;
  3749. }
  3750. int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
  3751. {
  3752. struct nfs_server *server = NFS_SERVER(state->inode);
  3753. struct nfs4_exception exception = { };
  3754. int err;
  3755. err = nfs4_set_lock_state(state, fl);
  3756. if (err != 0)
  3757. goto out;
  3758. do {
  3759. err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
  3760. switch (err) {
  3761. default:
  3762. printk(KERN_ERR "%s: unhandled error %d.\n",
  3763. __func__, err);
  3764. case 0:
  3765. case -ESTALE:
  3766. goto out;
  3767. case -NFS4ERR_EXPIRED:
  3768. case -NFS4ERR_STALE_CLIENTID:
  3769. case -NFS4ERR_STALE_STATEID:
  3770. nfs4_schedule_state_recovery(server->nfs_client);
  3771. goto out;
  3772. case -ERESTARTSYS:
  3773. /*
  3774. * The show must go on: exit, but mark the
  3775. * stateid as needing recovery.
  3776. */
  3777. case -NFS4ERR_ADMIN_REVOKED:
  3778. case -NFS4ERR_BAD_STATEID:
  3779. case -NFS4ERR_OPENMODE:
  3780. nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
  3781. err = 0;
  3782. goto out;
  3783. case -ENOMEM:
  3784. case -NFS4ERR_DENIED:
  3785. /* kill_proc(fl->fl_pid, SIGLOST, 1); */
  3786. err = 0;
  3787. goto out;
  3788. case -NFS4ERR_DELAY:
  3789. break;
  3790. }
  3791. err = nfs4_handle_exception(server, err, &exception);
  3792. } while (exception.retry);
  3793. out:
  3794. return err;
  3795. }
  3796. #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
  3797. int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
  3798. size_t buflen, int flags)
  3799. {
  3800. struct inode *inode = dentry->d_inode;
  3801. if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
  3802. return -EOPNOTSUPP;
  3803. return nfs4_proc_set_acl(inode, buf, buflen);
  3804. }
  3805. /* The getxattr man page suggests returning -ENODATA for unknown attributes,
  3806. * and that's what we'll do for e.g. user attributes that haven't been set.
  3807. * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
  3808. * attributes in kernel-managed attribute namespaces. */
  3809. ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
  3810. size_t buflen)
  3811. {
  3812. struct inode *inode = dentry->d_inode;
  3813. if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
  3814. return -EOPNOTSUPP;
  3815. return nfs4_proc_get_acl(inode, buf, buflen);
  3816. }
  3817. ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
  3818. {
  3819. size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
  3820. if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
  3821. return 0;
  3822. if (buf && buflen < len)
  3823. return -ERANGE;
  3824. if (buf)
  3825. memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
  3826. return len;
  3827. }
  3828. static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
  3829. {
  3830. if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
  3831. (fattr->valid & NFS_ATTR_FATTR_FSID) &&
  3832. (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
  3833. return;
  3834. fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
  3835. NFS_ATTR_FATTR_NLINK;
  3836. fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
  3837. fattr->nlink = 2;
  3838. }
  3839. int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
  3840. struct nfs4_fs_locations *fs_locations, struct page *page)
  3841. {
  3842. struct nfs_server *server = NFS_SERVER(dir);
  3843. u32 bitmask[2] = {
  3844. [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
  3845. [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
  3846. };
  3847. struct nfs4_fs_locations_arg args = {
  3848. .dir_fh = NFS_FH(dir),
  3849. .name = name,
  3850. .page = page,
  3851. .bitmask = bitmask,
  3852. };
  3853. struct nfs4_fs_locations_res res = {
  3854. .fs_locations = fs_locations,
  3855. };
  3856. struct rpc_message msg = {
  3857. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
  3858. .rpc_argp = &args,
  3859. .rpc_resp = &res,
  3860. };
  3861. int status;
  3862. dprintk("%s: start\n", __func__);
  3863. nfs_fattr_init(&fs_locations->fattr);
  3864. fs_locations->server = server;
  3865. fs_locations->nlocations = 0;
  3866. status = nfs4_call_sync(server, &msg, &args, &res, 0);
  3867. nfs_fixup_referral_attributes(&fs_locations->fattr);
  3868. dprintk("%s: returned status = %d\n", __func__, status);
  3869. return status;
  3870. }
  3871. #ifdef CONFIG_NFS_V4_1
  3872. /*
  3873. * nfs4_proc_exchange_id()
  3874. *
  3875. * Since the clientid has expired, all compounds using sessions
  3876. * associated with the stale clientid will be returning
  3877. * NFS4ERR_BADSESSION in the sequence operation, and will therefore
  3878. * be in some phase of session reset.
  3879. */
  3880. static int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
  3881. {
  3882. nfs4_verifier verifier;
  3883. struct nfs41_exchange_id_args args = {
  3884. .client = clp,
  3885. .flags = clp->cl_exchange_flags,
  3886. };
  3887. struct nfs41_exchange_id_res res = {
  3888. .client = clp,
  3889. };
  3890. int status;
  3891. struct rpc_message msg = {
  3892. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
  3893. .rpc_argp = &args,
  3894. .rpc_resp = &res,
  3895. .rpc_cred = cred,
  3896. };
  3897. __be32 *p;
  3898. dprintk("--> %s\n", __func__);
  3899. BUG_ON(clp == NULL);
  3900. p = (u32 *)verifier.data;
  3901. *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
  3902. *p = htonl((u32)clp->cl_boot_time.tv_nsec);
  3903. args.verifier = &verifier;
  3904. while (1) {
  3905. args.id_len = scnprintf(args.id, sizeof(args.id),
  3906. "%s/%s %u",
  3907. clp->cl_ipaddr,
  3908. rpc_peeraddr2str(clp->cl_rpcclient,
  3909. RPC_DISPLAY_ADDR),
  3910. clp->cl_id_uniquifier);
  3911. status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
  3912. if (status != NFS4ERR_CLID_INUSE)
  3913. break;
  3914. if (signalled())
  3915. break;
  3916. if (++clp->cl_id_uniquifier == 0)
  3917. break;
  3918. }
  3919. dprintk("<-- %s status= %d\n", __func__, status);
  3920. return status;
  3921. }
  3922. struct nfs4_get_lease_time_data {
  3923. struct nfs4_get_lease_time_args *args;
  3924. struct nfs4_get_lease_time_res *res;
  3925. struct nfs_client *clp;
  3926. };
  3927. static void nfs4_get_lease_time_prepare(struct rpc_task *task,
  3928. void *calldata)
  3929. {
  3930. int ret;
  3931. struct nfs4_get_lease_time_data *data =
  3932. (struct nfs4_get_lease_time_data *)calldata;
  3933. dprintk("--> %s\n", __func__);
  3934. /* just setup sequence, do not trigger session recovery
  3935. since we're invoked within one */
  3936. ret = nfs41_setup_sequence(data->clp->cl_session,
  3937. &data->args->la_seq_args,
  3938. &data->res->lr_seq_res, 0, task);
  3939. BUG_ON(ret == -EAGAIN);
  3940. rpc_call_start(task);
  3941. dprintk("<-- %s\n", __func__);
  3942. }
  3943. /*
  3944. * Called from nfs4_state_manager thread for session setup, so don't recover
  3945. * from sequence operation or clientid errors.
  3946. */
  3947. static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
  3948. {
  3949. struct nfs4_get_lease_time_data *data =
  3950. (struct nfs4_get_lease_time_data *)calldata;
  3951. dprintk("--> %s\n", __func__);
  3952. nfs41_sequence_done(data->clp, &data->res->lr_seq_res, task->tk_status);
  3953. switch (task->tk_status) {
  3954. case -NFS4ERR_DELAY:
  3955. case -NFS4ERR_GRACE:
  3956. dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
  3957. rpc_delay(task, NFS4_POLL_RETRY_MIN);
  3958. task->tk_status = 0;
  3959. nfs4_restart_rpc(task, data->clp);
  3960. return;
  3961. }
  3962. nfs41_sequence_free_slot(data->clp, &data->res->lr_seq_res);
  3963. dprintk("<-- %s\n", __func__);
  3964. }
  3965. struct rpc_call_ops nfs4_get_lease_time_ops = {
  3966. .rpc_call_prepare = nfs4_get_lease_time_prepare,
  3967. .rpc_call_done = nfs4_get_lease_time_done,
  3968. };
  3969. int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
  3970. {
  3971. struct rpc_task *task;
  3972. struct nfs4_get_lease_time_args args;
  3973. struct nfs4_get_lease_time_res res = {
  3974. .lr_fsinfo = fsinfo,
  3975. };
  3976. struct nfs4_get_lease_time_data data = {
  3977. .args = &args,
  3978. .res = &res,
  3979. .clp = clp,
  3980. };
  3981. struct rpc_message msg = {
  3982. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
  3983. .rpc_argp = &args,
  3984. .rpc_resp = &res,
  3985. };
  3986. struct rpc_task_setup task_setup = {
  3987. .rpc_client = clp->cl_rpcclient,
  3988. .rpc_message = &msg,
  3989. .callback_ops = &nfs4_get_lease_time_ops,
  3990. .callback_data = &data
  3991. };
  3992. int status;
  3993. res.lr_seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
  3994. dprintk("--> %s\n", __func__);
  3995. task = rpc_run_task(&task_setup);
  3996. if (IS_ERR(task))
  3997. status = PTR_ERR(task);
  3998. else {
  3999. status = task->tk_status;
  4000. rpc_put_task(task);
  4001. }
  4002. dprintk("<-- %s return %d\n", __func__, status);
  4003. return status;
  4004. }
  4005. /*
  4006. * Reset a slot table
  4007. */
  4008. static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, int max_slots,
  4009. int old_max_slots, int ivalue)
  4010. {
  4011. int i;
  4012. int ret = 0;
  4013. dprintk("--> %s: max_reqs=%u, tbl %p\n", __func__, max_slots, tbl);
  4014. /*
  4015. * Until we have dynamic slot table adjustment, insist
  4016. * upon the same slot table size
  4017. */
  4018. if (max_slots != old_max_slots) {
  4019. dprintk("%s reset slot table does't match old\n",
  4020. __func__);
  4021. ret = -EINVAL; /*XXX NFS4ERR_REQ_TOO_BIG ? */
  4022. goto out;
  4023. }
  4024. spin_lock(&tbl->slot_tbl_lock);
  4025. for (i = 0; i < max_slots; ++i)
  4026. tbl->slots[i].seq_nr = ivalue;
  4027. tbl->highest_used_slotid = -1;
  4028. spin_unlock(&tbl->slot_tbl_lock);
  4029. dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
  4030. tbl, tbl->slots, tbl->max_slots);
  4031. out:
  4032. dprintk("<-- %s: return %d\n", __func__, ret);
  4033. return ret;
  4034. }
  4035. /*
  4036. * Reset the forechannel and backchannel slot tables
  4037. */
  4038. static int nfs4_reset_slot_tables(struct nfs4_session *session)
  4039. {
  4040. int status;
  4041. status = nfs4_reset_slot_table(&session->fc_slot_table,
  4042. session->fc_attrs.max_reqs,
  4043. session->fc_slot_table.max_slots,
  4044. 1);
  4045. if (status)
  4046. return status;
  4047. status = nfs4_reset_slot_table(&session->bc_slot_table,
  4048. session->bc_attrs.max_reqs,
  4049. session->bc_slot_table.max_slots,
  4050. 0);
  4051. return status;
  4052. }
  4053. /* Destroy the slot table */
  4054. static void nfs4_destroy_slot_tables(struct nfs4_session *session)
  4055. {
  4056. if (session->fc_slot_table.slots != NULL) {
  4057. kfree(session->fc_slot_table.slots);
  4058. session->fc_slot_table.slots = NULL;
  4059. }
  4060. if (session->bc_slot_table.slots != NULL) {
  4061. kfree(session->bc_slot_table.slots);
  4062. session->bc_slot_table.slots = NULL;
  4063. }
  4064. return;
  4065. }
  4066. /*
  4067. * Initialize slot table
  4068. */
  4069. static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
  4070. int max_slots, int ivalue)
  4071. {
  4072. int i;
  4073. struct nfs4_slot *slot;
  4074. int ret = -ENOMEM;
  4075. BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
  4076. dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
  4077. slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_KERNEL);
  4078. if (!slot)
  4079. goto out;
  4080. for (i = 0; i < max_slots; ++i)
  4081. slot[i].seq_nr = ivalue;
  4082. ret = 0;
  4083. spin_lock(&tbl->slot_tbl_lock);
  4084. if (tbl->slots != NULL) {
  4085. spin_unlock(&tbl->slot_tbl_lock);
  4086. dprintk("%s: slot table already initialized. tbl=%p slots=%p\n",
  4087. __func__, tbl, tbl->slots);
  4088. WARN_ON(1);
  4089. goto out_free;
  4090. }
  4091. tbl->max_slots = max_slots;
  4092. tbl->slots = slot;
  4093. tbl->highest_used_slotid = -1; /* no slot is currently used */
  4094. spin_unlock(&tbl->slot_tbl_lock);
  4095. dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
  4096. tbl, tbl->slots, tbl->max_slots);
  4097. out:
  4098. dprintk("<-- %s: return %d\n", __func__, ret);
  4099. return ret;
  4100. out_free:
  4101. kfree(slot);
  4102. goto out;
  4103. }
  4104. /*
  4105. * Initialize the forechannel and backchannel tables
  4106. */
  4107. static int nfs4_init_slot_tables(struct nfs4_session *session)
  4108. {
  4109. int status;
  4110. status = nfs4_init_slot_table(&session->fc_slot_table,
  4111. session->fc_attrs.max_reqs, 1);
  4112. if (status)
  4113. return status;
  4114. status = nfs4_init_slot_table(&session->bc_slot_table,
  4115. session->bc_attrs.max_reqs, 0);
  4116. if (status)
  4117. nfs4_destroy_slot_tables(session);
  4118. return status;
  4119. }
  4120. struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
  4121. {
  4122. struct nfs4_session *session;
  4123. struct nfs4_slot_table *tbl;
  4124. session = kzalloc(sizeof(struct nfs4_session), GFP_KERNEL);
  4125. if (!session)
  4126. return NULL;
  4127. set_bit(NFS4CLNT_SESSION_SETUP, &clp->cl_state);
  4128. /*
  4129. * The create session reply races with the server back
  4130. * channel probe. Mark the client NFS_CS_SESSION_INITING
  4131. * so that the client back channel can find the
  4132. * nfs_client struct
  4133. */
  4134. clp->cl_cons_state = NFS_CS_SESSION_INITING;
  4135. tbl = &session->fc_slot_table;
  4136. spin_lock_init(&tbl->slot_tbl_lock);
  4137. rpc_init_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
  4138. tbl = &session->bc_slot_table;
  4139. spin_lock_init(&tbl->slot_tbl_lock);
  4140. rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
  4141. session->clp = clp;
  4142. return session;
  4143. }
  4144. void nfs4_destroy_session(struct nfs4_session *session)
  4145. {
  4146. nfs4_proc_destroy_session(session);
  4147. dprintk("%s Destroy backchannel for xprt %p\n",
  4148. __func__, session->clp->cl_rpcclient->cl_xprt);
  4149. xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
  4150. NFS41_BC_MIN_CALLBACKS);
  4151. nfs4_destroy_slot_tables(session);
  4152. kfree(session);
  4153. }
  4154. /*
  4155. * Initialize the values to be used by the client in CREATE_SESSION
  4156. * If nfs4_init_session set the fore channel request and response sizes,
  4157. * use them.
  4158. *
  4159. * Set the back channel max_resp_sz_cached to zero to force the client to
  4160. * always set csa_cachethis to FALSE because the current implementation
  4161. * of the back channel DRC only supports caching the CB_SEQUENCE operation.
  4162. */
  4163. static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
  4164. {
  4165. struct nfs4_session *session = args->client->cl_session;
  4166. unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
  4167. mxresp_sz = session->fc_attrs.max_resp_sz;
  4168. if (mxrqst_sz == 0)
  4169. mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
  4170. if (mxresp_sz == 0)
  4171. mxresp_sz = NFS_MAX_FILE_IO_SIZE;
  4172. /* Fore channel attributes */
  4173. args->fc_attrs.headerpadsz = 0;
  4174. args->fc_attrs.max_rqst_sz = mxrqst_sz;
  4175. args->fc_attrs.max_resp_sz = mxresp_sz;
  4176. args->fc_attrs.max_resp_sz_cached = mxresp_sz;
  4177. args->fc_attrs.max_ops = NFS4_MAX_OPS;
  4178. args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
  4179. dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
  4180. "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
  4181. __func__,
  4182. args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
  4183. args->fc_attrs.max_resp_sz_cached, args->fc_attrs.max_ops,
  4184. args->fc_attrs.max_reqs);
  4185. /* Back channel attributes */
  4186. args->bc_attrs.headerpadsz = 0;
  4187. args->bc_attrs.max_rqst_sz = PAGE_SIZE;
  4188. args->bc_attrs.max_resp_sz = PAGE_SIZE;
  4189. args->bc_attrs.max_resp_sz_cached = 0;
  4190. args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
  4191. args->bc_attrs.max_reqs = 1;
  4192. dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
  4193. "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
  4194. __func__,
  4195. args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
  4196. args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
  4197. args->bc_attrs.max_reqs);
  4198. }
  4199. static int _verify_channel_attr(char *chan, char *attr_name, u32 sent, u32 rcvd)
  4200. {
  4201. if (rcvd <= sent)
  4202. return 0;
  4203. printk(KERN_WARNING "%s: Session INVALID: %s channel %s increased. "
  4204. "sent=%u rcvd=%u\n", __func__, chan, attr_name, sent, rcvd);
  4205. return -EINVAL;
  4206. }
  4207. #define _verify_fore_channel_attr(_name_) \
  4208. _verify_channel_attr("fore", #_name_, \
  4209. args->fc_attrs._name_, \
  4210. session->fc_attrs._name_)
  4211. #define _verify_back_channel_attr(_name_) \
  4212. _verify_channel_attr("back", #_name_, \
  4213. args->bc_attrs._name_, \
  4214. session->bc_attrs._name_)
  4215. /*
  4216. * The server is not allowed to increase the fore channel header pad size,
  4217. * maximum response size, or maximum number of operations.
  4218. *
  4219. * The back channel attributes are only negotiatied down: We send what the
  4220. * (back channel) server insists upon.
  4221. */
  4222. static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
  4223. struct nfs4_session *session)
  4224. {
  4225. int ret = 0;
  4226. ret |= _verify_fore_channel_attr(headerpadsz);
  4227. ret |= _verify_fore_channel_attr(max_resp_sz);
  4228. ret |= _verify_fore_channel_attr(max_ops);
  4229. ret |= _verify_back_channel_attr(headerpadsz);
  4230. ret |= _verify_back_channel_attr(max_rqst_sz);
  4231. ret |= _verify_back_channel_attr(max_resp_sz);
  4232. ret |= _verify_back_channel_attr(max_resp_sz_cached);
  4233. ret |= _verify_back_channel_attr(max_ops);
  4234. ret |= _verify_back_channel_attr(max_reqs);
  4235. return ret;
  4236. }
  4237. static int _nfs4_proc_create_session(struct nfs_client *clp)
  4238. {
  4239. struct nfs4_session *session = clp->cl_session;
  4240. struct nfs41_create_session_args args = {
  4241. .client = clp,
  4242. .cb_program = NFS4_CALLBACK,
  4243. };
  4244. struct nfs41_create_session_res res = {
  4245. .client = clp,
  4246. };
  4247. struct rpc_message msg = {
  4248. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
  4249. .rpc_argp = &args,
  4250. .rpc_resp = &res,
  4251. };
  4252. int status;
  4253. nfs4_init_channel_attrs(&args);
  4254. args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
  4255. status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
  4256. if (!status)
  4257. /* Verify the session's negotiated channel_attrs values */
  4258. status = nfs4_verify_channel_attrs(&args, session);
  4259. if (!status) {
  4260. /* Increment the clientid slot sequence id */
  4261. clp->cl_seqid++;
  4262. }
  4263. return status;
  4264. }
  4265. /*
  4266. * Issues a CREATE_SESSION operation to the server.
  4267. * It is the responsibility of the caller to verify the session is
  4268. * expired before calling this routine.
  4269. */
  4270. int nfs4_proc_create_session(struct nfs_client *clp, int reset)
  4271. {
  4272. int status;
  4273. unsigned *ptr;
  4274. struct nfs_fsinfo fsinfo;
  4275. struct nfs4_session *session = clp->cl_session;
  4276. dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
  4277. status = _nfs4_proc_create_session(clp);
  4278. if (status)
  4279. goto out;
  4280. /* Init or reset the fore channel */
  4281. if (reset)
  4282. status = nfs4_reset_slot_tables(session);
  4283. else
  4284. status = nfs4_init_slot_tables(session);
  4285. dprintk("fore channel slot table initialization returned %d\n", status);
  4286. if (status)
  4287. goto out;
  4288. ptr = (unsigned *)&session->sess_id.data[0];
  4289. dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
  4290. clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
  4291. if (reset)
  4292. /* Lease time is aleady set */
  4293. goto out;
  4294. /* Get the lease time */
  4295. status = nfs4_proc_get_lease_time(clp, &fsinfo);
  4296. if (status == 0) {
  4297. /* Update lease time and schedule renewal */
  4298. spin_lock(&clp->cl_lock);
  4299. clp->cl_lease_time = fsinfo.lease_time * HZ;
  4300. clp->cl_last_renewal = jiffies;
  4301. clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  4302. spin_unlock(&clp->cl_lock);
  4303. nfs4_schedule_state_renewal(clp);
  4304. }
  4305. out:
  4306. dprintk("<-- %s\n", __func__);
  4307. return status;
  4308. }
  4309. /*
  4310. * Issue the over-the-wire RPC DESTROY_SESSION.
  4311. * The caller must serialize access to this routine.
  4312. */
  4313. int nfs4_proc_destroy_session(struct nfs4_session *session)
  4314. {
  4315. int status = 0;
  4316. struct rpc_message msg;
  4317. dprintk("--> nfs4_proc_destroy_session\n");
  4318. /* session is still being setup */
  4319. if (session->clp->cl_cons_state != NFS_CS_READY)
  4320. return status;
  4321. msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
  4322. msg.rpc_argp = session;
  4323. msg.rpc_resp = NULL;
  4324. msg.rpc_cred = NULL;
  4325. status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
  4326. if (status)
  4327. printk(KERN_WARNING
  4328. "Got error %d from the server on DESTROY_SESSION. "
  4329. "Session has been destroyed regardless...\n", status);
  4330. dprintk("<-- nfs4_proc_destroy_session\n");
  4331. return status;
  4332. }
  4333. int nfs4_init_session(struct nfs_server *server)
  4334. {
  4335. struct nfs_client *clp = server->nfs_client;
  4336. int ret;
  4337. if (!nfs4_has_session(clp))
  4338. return 0;
  4339. clp->cl_session->fc_attrs.max_rqst_sz = server->wsize;
  4340. clp->cl_session->fc_attrs.max_resp_sz = server->rsize;
  4341. ret = nfs4_recover_expired_lease(server);
  4342. if (!ret)
  4343. ret = nfs4_check_client_ready(clp);
  4344. return ret;
  4345. }
  4346. /*
  4347. * Renew the cl_session lease.
  4348. */
  4349. static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
  4350. {
  4351. struct nfs4_sequence_args args;
  4352. struct nfs4_sequence_res res;
  4353. struct rpc_message msg = {
  4354. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
  4355. .rpc_argp = &args,
  4356. .rpc_resp = &res,
  4357. .rpc_cred = cred,
  4358. };
  4359. args.sa_cache_this = 0;
  4360. return nfs4_call_sync_sequence(clp, clp->cl_rpcclient, &msg, &args,
  4361. &res, 0);
  4362. }
  4363. void nfs41_sequence_call_done(struct rpc_task *task, void *data)
  4364. {
  4365. struct nfs_client *clp = (struct nfs_client *)data;
  4366. nfs41_sequence_done(clp, task->tk_msg.rpc_resp, task->tk_status);
  4367. if (task->tk_status < 0) {
  4368. dprintk("%s ERROR %d\n", __func__, task->tk_status);
  4369. if (_nfs4_async_handle_error(task, NULL, clp, NULL)
  4370. == -EAGAIN) {
  4371. nfs4_restart_rpc(task, clp);
  4372. return;
  4373. }
  4374. }
  4375. nfs41_sequence_free_slot(clp, task->tk_msg.rpc_resp);
  4376. dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
  4377. kfree(task->tk_msg.rpc_argp);
  4378. kfree(task->tk_msg.rpc_resp);
  4379. dprintk("<-- %s\n", __func__);
  4380. }
  4381. static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
  4382. {
  4383. struct nfs_client *clp;
  4384. struct nfs4_sequence_args *args;
  4385. struct nfs4_sequence_res *res;
  4386. clp = (struct nfs_client *)data;
  4387. args = task->tk_msg.rpc_argp;
  4388. res = task->tk_msg.rpc_resp;
  4389. if (nfs4_setup_sequence(clp, args, res, 0, task))
  4390. return;
  4391. rpc_call_start(task);
  4392. }
  4393. static const struct rpc_call_ops nfs41_sequence_ops = {
  4394. .rpc_call_done = nfs41_sequence_call_done,
  4395. .rpc_call_prepare = nfs41_sequence_prepare,
  4396. };
  4397. static int nfs41_proc_async_sequence(struct nfs_client *clp,
  4398. struct rpc_cred *cred)
  4399. {
  4400. struct nfs4_sequence_args *args;
  4401. struct nfs4_sequence_res *res;
  4402. struct rpc_message msg = {
  4403. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
  4404. .rpc_cred = cred,
  4405. };
  4406. args = kzalloc(sizeof(*args), GFP_KERNEL);
  4407. if (!args)
  4408. return -ENOMEM;
  4409. res = kzalloc(sizeof(*res), GFP_KERNEL);
  4410. if (!res) {
  4411. kfree(args);
  4412. return -ENOMEM;
  4413. }
  4414. res->sr_slotid = NFS4_MAX_SLOT_TABLE;
  4415. msg.rpc_argp = args;
  4416. msg.rpc_resp = res;
  4417. return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
  4418. &nfs41_sequence_ops, (void *)clp);
  4419. }
  4420. #endif /* CONFIG_NFS_V4_1 */
  4421. struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
  4422. .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
  4423. .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
  4424. .recover_open = nfs4_open_reclaim,
  4425. .recover_lock = nfs4_lock_reclaim,
  4426. .establish_clid = nfs4_init_clientid,
  4427. .get_clid_cred = nfs4_get_setclientid_cred,
  4428. };
  4429. #if defined(CONFIG_NFS_V4_1)
  4430. struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
  4431. .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
  4432. .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
  4433. .recover_open = nfs4_open_reclaim,
  4434. .recover_lock = nfs4_lock_reclaim,
  4435. .establish_clid = nfs4_proc_exchange_id,
  4436. .get_clid_cred = nfs4_get_exchange_id_cred,
  4437. };
  4438. #endif /* CONFIG_NFS_V4_1 */
  4439. struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
  4440. .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
  4441. .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
  4442. .recover_open = nfs4_open_expired,
  4443. .recover_lock = nfs4_lock_expired,
  4444. .establish_clid = nfs4_init_clientid,
  4445. .get_clid_cred = nfs4_get_setclientid_cred,
  4446. };
  4447. #if defined(CONFIG_NFS_V4_1)
  4448. struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
  4449. .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
  4450. .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
  4451. .recover_open = nfs4_open_expired,
  4452. .recover_lock = nfs4_lock_expired,
  4453. .establish_clid = nfs4_proc_exchange_id,
  4454. .get_clid_cred = nfs4_get_exchange_id_cred,
  4455. };
  4456. #endif /* CONFIG_NFS_V4_1 */
  4457. struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
  4458. .sched_state_renewal = nfs4_proc_async_renew,
  4459. .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
  4460. .renew_lease = nfs4_proc_renew,
  4461. };
  4462. #if defined(CONFIG_NFS_V4_1)
  4463. struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
  4464. .sched_state_renewal = nfs41_proc_async_sequence,
  4465. .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
  4466. .renew_lease = nfs4_proc_sequence,
  4467. };
  4468. #endif
  4469. /*
  4470. * Per minor version reboot and network partition recovery ops
  4471. */
  4472. struct nfs4_state_recovery_ops *nfs4_reboot_recovery_ops[] = {
  4473. &nfs40_reboot_recovery_ops,
  4474. #if defined(CONFIG_NFS_V4_1)
  4475. &nfs41_reboot_recovery_ops,
  4476. #endif
  4477. };
  4478. struct nfs4_state_recovery_ops *nfs4_nograce_recovery_ops[] = {
  4479. &nfs40_nograce_recovery_ops,
  4480. #if defined(CONFIG_NFS_V4_1)
  4481. &nfs41_nograce_recovery_ops,
  4482. #endif
  4483. };
  4484. struct nfs4_state_maintenance_ops *nfs4_state_renewal_ops[] = {
  4485. &nfs40_state_renewal_ops,
  4486. #if defined(CONFIG_NFS_V4_1)
  4487. &nfs41_state_renewal_ops,
  4488. #endif
  4489. };
  4490. static const struct inode_operations nfs4_file_inode_operations = {
  4491. .permission = nfs_permission,
  4492. .getattr = nfs_getattr,
  4493. .setattr = nfs_setattr,
  4494. .getxattr = nfs4_getxattr,
  4495. .setxattr = nfs4_setxattr,
  4496. .listxattr = nfs4_listxattr,
  4497. };
  4498. const struct nfs_rpc_ops nfs_v4_clientops = {
  4499. .version = 4, /* protocol version */
  4500. .dentry_ops = &nfs4_dentry_operations,
  4501. .dir_inode_ops = &nfs4_dir_inode_operations,
  4502. .file_inode_ops = &nfs4_file_inode_operations,
  4503. .getroot = nfs4_proc_get_root,
  4504. .getattr = nfs4_proc_getattr,
  4505. .setattr = nfs4_proc_setattr,
  4506. .lookupfh = nfs4_proc_lookupfh,
  4507. .lookup = nfs4_proc_lookup,
  4508. .access = nfs4_proc_access,
  4509. .readlink = nfs4_proc_readlink,
  4510. .create = nfs4_proc_create,
  4511. .remove = nfs4_proc_remove,
  4512. .unlink_setup = nfs4_proc_unlink_setup,
  4513. .unlink_done = nfs4_proc_unlink_done,
  4514. .rename = nfs4_proc_rename,
  4515. .link = nfs4_proc_link,
  4516. .symlink = nfs4_proc_symlink,
  4517. .mkdir = nfs4_proc_mkdir,
  4518. .rmdir = nfs4_proc_remove,
  4519. .readdir = nfs4_proc_readdir,
  4520. .mknod = nfs4_proc_mknod,
  4521. .statfs = nfs4_proc_statfs,
  4522. .fsinfo = nfs4_proc_fsinfo,
  4523. .pathconf = nfs4_proc_pathconf,
  4524. .set_capabilities = nfs4_server_capabilities,
  4525. .decode_dirent = nfs4_decode_dirent,
  4526. .read_setup = nfs4_proc_read_setup,
  4527. .read_done = nfs4_read_done,
  4528. .write_setup = nfs4_proc_write_setup,
  4529. .write_done = nfs4_write_done,
  4530. .commit_setup = nfs4_proc_commit_setup,
  4531. .commit_done = nfs4_commit_done,
  4532. .lock = nfs4_proc_lock,
  4533. .clear_acl_cache = nfs4_zap_acl_attr,
  4534. .close_context = nfs4_close_context,
  4535. };
  4536. /*
  4537. * Local variables:
  4538. * c-basic-offset: 8
  4539. * End:
  4540. */