nfs4proc.c 100 KB

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