nfs4proc.c 112 KB

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