nfs4proc.c 100 KB

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