nfs4proc.c 99 KB

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