nfs4proc.c 136 KB

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