nfs4proc.c 130 KB

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