nfs4proc.c 100 KB

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