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