nfs4proc.c 100 KB

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