nfs4proc.c 100 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744
  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. return (struct dentry *)state;
  1282. }
  1283. res = d_add_unique(dentry, igrab(state->inode));
  1284. if (res != NULL)
  1285. path.dentry = res;
  1286. nfs4_intent_set_file(nd, &path, state);
  1287. return res;
  1288. }
  1289. int
  1290. nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
  1291. {
  1292. struct path path = {
  1293. .mnt = nd->mnt,
  1294. .dentry = dentry,
  1295. };
  1296. struct rpc_cred *cred;
  1297. struct nfs4_state *state;
  1298. cred = rpcauth_lookupcred(NFS_CLIENT(dir)->cl_auth, 0);
  1299. if (IS_ERR(cred))
  1300. return PTR_ERR(cred);
  1301. state = nfs4_do_open(dir, &path, openflags, NULL, cred);
  1302. put_rpccred(cred);
  1303. if (IS_ERR(state)) {
  1304. switch (PTR_ERR(state)) {
  1305. case -EPERM:
  1306. case -EACCES:
  1307. case -EDQUOT:
  1308. case -ENOSPC:
  1309. case -EROFS:
  1310. lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
  1311. return 1;
  1312. default:
  1313. goto out_drop;
  1314. }
  1315. }
  1316. if (state->inode == dentry->d_inode) {
  1317. nfs4_intent_set_file(nd, &path, state);
  1318. return 1;
  1319. }
  1320. nfs4_close_state(&path, state, openflags);
  1321. out_drop:
  1322. d_drop(dentry);
  1323. return 0;
  1324. }
  1325. static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
  1326. {
  1327. struct nfs4_server_caps_res res = {};
  1328. struct rpc_message msg = {
  1329. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
  1330. .rpc_argp = fhandle,
  1331. .rpc_resp = &res,
  1332. };
  1333. int status;
  1334. status = rpc_call_sync(server->client, &msg, 0);
  1335. if (status == 0) {
  1336. memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
  1337. if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
  1338. server->caps |= NFS_CAP_ACLS;
  1339. if (res.has_links != 0)
  1340. server->caps |= NFS_CAP_HARDLINKS;
  1341. if (res.has_symlinks != 0)
  1342. server->caps |= NFS_CAP_SYMLINKS;
  1343. server->acl_bitmask = res.acl_bitmask;
  1344. }
  1345. return status;
  1346. }
  1347. int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
  1348. {
  1349. struct nfs4_exception exception = { };
  1350. int err;
  1351. do {
  1352. err = nfs4_handle_exception(server,
  1353. _nfs4_server_capabilities(server, fhandle),
  1354. &exception);
  1355. } while (exception.retry);
  1356. return err;
  1357. }
  1358. static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
  1359. struct nfs_fsinfo *info)
  1360. {
  1361. struct nfs4_lookup_root_arg args = {
  1362. .bitmask = nfs4_fattr_bitmap,
  1363. };
  1364. struct nfs4_lookup_res res = {
  1365. .server = server,
  1366. .fattr = info->fattr,
  1367. .fh = fhandle,
  1368. };
  1369. struct rpc_message msg = {
  1370. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
  1371. .rpc_argp = &args,
  1372. .rpc_resp = &res,
  1373. };
  1374. nfs_fattr_init(info->fattr);
  1375. return rpc_call_sync(server->client, &msg, 0);
  1376. }
  1377. static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
  1378. struct nfs_fsinfo *info)
  1379. {
  1380. struct nfs4_exception exception = { };
  1381. int err;
  1382. do {
  1383. err = nfs4_handle_exception(server,
  1384. _nfs4_lookup_root(server, fhandle, info),
  1385. &exception);
  1386. } while (exception.retry);
  1387. return err;
  1388. }
  1389. /*
  1390. * get the file handle for the "/" directory on the server
  1391. */
  1392. static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
  1393. struct nfs_fsinfo *info)
  1394. {
  1395. int status;
  1396. status = nfs4_lookup_root(server, fhandle, info);
  1397. if (status == 0)
  1398. status = nfs4_server_capabilities(server, fhandle);
  1399. if (status == 0)
  1400. status = nfs4_do_fsinfo(server, fhandle, info);
  1401. return nfs4_map_errors(status);
  1402. }
  1403. /*
  1404. * Get locations and (maybe) other attributes of a referral.
  1405. * Note that we'll actually follow the referral later when
  1406. * we detect fsid mismatch in inode revalidation
  1407. */
  1408. static int nfs4_get_referral(struct inode *dir, const struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle)
  1409. {
  1410. int status = -ENOMEM;
  1411. struct page *page = NULL;
  1412. struct nfs4_fs_locations *locations = NULL;
  1413. page = alloc_page(GFP_KERNEL);
  1414. if (page == NULL)
  1415. goto out;
  1416. locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
  1417. if (locations == NULL)
  1418. goto out;
  1419. status = nfs4_proc_fs_locations(dir, name, locations, page);
  1420. if (status != 0)
  1421. goto out;
  1422. /* Make sure server returned a different fsid for the referral */
  1423. if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
  1424. dprintk("%s: server did not return a different fsid for a referral at %s\n", __FUNCTION__, name->name);
  1425. status = -EIO;
  1426. goto out;
  1427. }
  1428. memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
  1429. fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
  1430. if (!fattr->mode)
  1431. fattr->mode = S_IFDIR;
  1432. memset(fhandle, 0, sizeof(struct nfs_fh));
  1433. out:
  1434. if (page)
  1435. __free_page(page);
  1436. if (locations)
  1437. kfree(locations);
  1438. return status;
  1439. }
  1440. static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
  1441. {
  1442. struct nfs4_getattr_arg args = {
  1443. .fh = fhandle,
  1444. .bitmask = server->attr_bitmask,
  1445. };
  1446. struct nfs4_getattr_res res = {
  1447. .fattr = fattr,
  1448. .server = server,
  1449. };
  1450. struct rpc_message msg = {
  1451. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
  1452. .rpc_argp = &args,
  1453. .rpc_resp = &res,
  1454. };
  1455. nfs_fattr_init(fattr);
  1456. return rpc_call_sync(server->client, &msg, 0);
  1457. }
  1458. static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
  1459. {
  1460. struct nfs4_exception exception = { };
  1461. int err;
  1462. do {
  1463. err = nfs4_handle_exception(server,
  1464. _nfs4_proc_getattr(server, fhandle, fattr),
  1465. &exception);
  1466. } while (exception.retry);
  1467. return err;
  1468. }
  1469. /*
  1470. * The file is not closed if it is opened due to the a request to change
  1471. * the size of the file. The open call will not be needed once the
  1472. * VFS layer lookup-intents are implemented.
  1473. *
  1474. * Close is called when the inode is destroyed.
  1475. * If we haven't opened the file for O_WRONLY, we
  1476. * need to in the size_change case to obtain a stateid.
  1477. *
  1478. * Got race?
  1479. * Because OPEN is always done by name in nfsv4, it is
  1480. * possible that we opened a different file by the same
  1481. * name. We can recognize this race condition, but we
  1482. * can't do anything about it besides returning an error.
  1483. *
  1484. * This will be fixed with VFS changes (lookup-intent).
  1485. */
  1486. static int
  1487. nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
  1488. struct iattr *sattr)
  1489. {
  1490. struct rpc_cred *cred;
  1491. struct inode *inode = dentry->d_inode;
  1492. struct nfs_open_context *ctx;
  1493. struct nfs4_state *state = NULL;
  1494. int status;
  1495. nfs_fattr_init(fattr);
  1496. cred = rpcauth_lookupcred(NFS_CLIENT(inode)->cl_auth, 0);
  1497. if (IS_ERR(cred))
  1498. return PTR_ERR(cred);
  1499. /* Search for an existing open(O_WRITE) file */
  1500. ctx = nfs_find_open_context(inode, cred, FMODE_WRITE);
  1501. if (ctx != NULL)
  1502. state = ctx->state;
  1503. status = nfs4_do_setattr(inode, fattr, sattr, state);
  1504. if (status == 0)
  1505. nfs_setattr_update_inode(inode, sattr);
  1506. if (ctx != NULL)
  1507. put_nfs_open_context(ctx);
  1508. put_rpccred(cred);
  1509. return status;
  1510. }
  1511. static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
  1512. const struct qstr *name, struct nfs_fh *fhandle,
  1513. struct nfs_fattr *fattr)
  1514. {
  1515. int status;
  1516. struct nfs4_lookup_arg args = {
  1517. .bitmask = server->attr_bitmask,
  1518. .dir_fh = dirfh,
  1519. .name = name,
  1520. };
  1521. struct nfs4_lookup_res res = {
  1522. .server = server,
  1523. .fattr = fattr,
  1524. .fh = fhandle,
  1525. };
  1526. struct rpc_message msg = {
  1527. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
  1528. .rpc_argp = &args,
  1529. .rpc_resp = &res,
  1530. };
  1531. nfs_fattr_init(fattr);
  1532. dprintk("NFS call lookupfh %s\n", name->name);
  1533. status = rpc_call_sync(server->client, &msg, 0);
  1534. dprintk("NFS reply lookupfh: %d\n", status);
  1535. return status;
  1536. }
  1537. static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
  1538. struct qstr *name, struct nfs_fh *fhandle,
  1539. struct nfs_fattr *fattr)
  1540. {
  1541. struct nfs4_exception exception = { };
  1542. int err;
  1543. do {
  1544. err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
  1545. /* FIXME: !!!! */
  1546. if (err == -NFS4ERR_MOVED) {
  1547. err = -EREMOTE;
  1548. break;
  1549. }
  1550. err = nfs4_handle_exception(server, err, &exception);
  1551. } while (exception.retry);
  1552. return err;
  1553. }
  1554. static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
  1555. struct nfs_fh *fhandle, struct nfs_fattr *fattr)
  1556. {
  1557. int status;
  1558. dprintk("NFS call lookup %s\n", name->name);
  1559. status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
  1560. if (status == -NFS4ERR_MOVED)
  1561. status = nfs4_get_referral(dir, name, fattr, fhandle);
  1562. dprintk("NFS reply lookup: %d\n", status);
  1563. return status;
  1564. }
  1565. static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
  1566. {
  1567. struct nfs4_exception exception = { };
  1568. int err;
  1569. do {
  1570. err = nfs4_handle_exception(NFS_SERVER(dir),
  1571. _nfs4_proc_lookup(dir, name, fhandle, fattr),
  1572. &exception);
  1573. } while (exception.retry);
  1574. return err;
  1575. }
  1576. static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
  1577. {
  1578. struct nfs_server *server = NFS_SERVER(inode);
  1579. struct nfs_fattr fattr;
  1580. struct nfs4_accessargs args = {
  1581. .fh = NFS_FH(inode),
  1582. .bitmask = server->attr_bitmask,
  1583. };
  1584. struct nfs4_accessres res = {
  1585. .server = server,
  1586. .fattr = &fattr,
  1587. };
  1588. struct rpc_message msg = {
  1589. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
  1590. .rpc_argp = &args,
  1591. .rpc_resp = &res,
  1592. .rpc_cred = entry->cred,
  1593. };
  1594. int mode = entry->mask;
  1595. int status;
  1596. /*
  1597. * Determine which access bits we want to ask for...
  1598. */
  1599. if (mode & MAY_READ)
  1600. args.access |= NFS4_ACCESS_READ;
  1601. if (S_ISDIR(inode->i_mode)) {
  1602. if (mode & MAY_WRITE)
  1603. args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
  1604. if (mode & MAY_EXEC)
  1605. args.access |= NFS4_ACCESS_LOOKUP;
  1606. } else {
  1607. if (mode & MAY_WRITE)
  1608. args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
  1609. if (mode & MAY_EXEC)
  1610. args.access |= NFS4_ACCESS_EXECUTE;
  1611. }
  1612. nfs_fattr_init(&fattr);
  1613. status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
  1614. if (!status) {
  1615. entry->mask = 0;
  1616. if (res.access & NFS4_ACCESS_READ)
  1617. entry->mask |= MAY_READ;
  1618. if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
  1619. entry->mask |= MAY_WRITE;
  1620. if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
  1621. entry->mask |= MAY_EXEC;
  1622. nfs_refresh_inode(inode, &fattr);
  1623. }
  1624. return status;
  1625. }
  1626. static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
  1627. {
  1628. struct nfs4_exception exception = { };
  1629. int err;
  1630. do {
  1631. err = nfs4_handle_exception(NFS_SERVER(inode),
  1632. _nfs4_proc_access(inode, entry),
  1633. &exception);
  1634. } while (exception.retry);
  1635. return err;
  1636. }
  1637. /*
  1638. * TODO: For the time being, we don't try to get any attributes
  1639. * along with any of the zero-copy operations READ, READDIR,
  1640. * READLINK, WRITE.
  1641. *
  1642. * In the case of the first three, we want to put the GETATTR
  1643. * after the read-type operation -- this is because it is hard
  1644. * to predict the length of a GETATTR response in v4, and thus
  1645. * align the READ data correctly. This means that the GETATTR
  1646. * may end up partially falling into the page cache, and we should
  1647. * shift it into the 'tail' of the xdr_buf before processing.
  1648. * To do this efficiently, we need to know the total length
  1649. * of data received, which doesn't seem to be available outside
  1650. * of the RPC layer.
  1651. *
  1652. * In the case of WRITE, we also want to put the GETATTR after
  1653. * the operation -- in this case because we want to make sure
  1654. * we get the post-operation mtime and size. This means that
  1655. * we can't use xdr_encode_pages() as written: we need a variant
  1656. * of it which would leave room in the 'tail' iovec.
  1657. *
  1658. * Both of these changes to the XDR layer would in fact be quite
  1659. * minor, but I decided to leave them for a subsequent patch.
  1660. */
  1661. static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
  1662. unsigned int pgbase, unsigned int pglen)
  1663. {
  1664. struct nfs4_readlink args = {
  1665. .fh = NFS_FH(inode),
  1666. .pgbase = pgbase,
  1667. .pglen = pglen,
  1668. .pages = &page,
  1669. };
  1670. struct rpc_message msg = {
  1671. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
  1672. .rpc_argp = &args,
  1673. .rpc_resp = NULL,
  1674. };
  1675. return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
  1676. }
  1677. static int nfs4_proc_readlink(struct inode *inode, struct page *page,
  1678. unsigned int pgbase, unsigned int pglen)
  1679. {
  1680. struct nfs4_exception exception = { };
  1681. int err;
  1682. do {
  1683. err = nfs4_handle_exception(NFS_SERVER(inode),
  1684. _nfs4_proc_readlink(inode, page, pgbase, pglen),
  1685. &exception);
  1686. } while (exception.retry);
  1687. return err;
  1688. }
  1689. /*
  1690. * Got race?
  1691. * We will need to arrange for the VFS layer to provide an atomic open.
  1692. * Until then, this create/open method is prone to inefficiency and race
  1693. * conditions due to the lookup, create, and open VFS calls from sys_open()
  1694. * placed on the wire.
  1695. *
  1696. * Given the above sorry state of affairs, I'm simply sending an OPEN.
  1697. * The file will be opened again in the subsequent VFS open call
  1698. * (nfs4_proc_file_open).
  1699. *
  1700. * The open for read will just hang around to be used by any process that
  1701. * opens the file O_RDONLY. This will all be resolved with the VFS changes.
  1702. */
  1703. static int
  1704. nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
  1705. int flags, struct nameidata *nd)
  1706. {
  1707. struct path path = {
  1708. .mnt = nd->mnt,
  1709. .dentry = dentry,
  1710. };
  1711. struct nfs4_state *state;
  1712. struct rpc_cred *cred;
  1713. int status = 0;
  1714. cred = rpcauth_lookupcred(NFS_CLIENT(dir)->cl_auth, 0);
  1715. if (IS_ERR(cred)) {
  1716. status = PTR_ERR(cred);
  1717. goto out;
  1718. }
  1719. state = nfs4_do_open(dir, &path, flags, sattr, cred);
  1720. put_rpccred(cred);
  1721. if (IS_ERR(state)) {
  1722. status = PTR_ERR(state);
  1723. goto out;
  1724. }
  1725. d_instantiate(dentry, igrab(state->inode));
  1726. if (flags & O_EXCL) {
  1727. struct nfs_fattr fattr;
  1728. status = nfs4_do_setattr(state->inode, &fattr, sattr, state);
  1729. if (status == 0)
  1730. nfs_setattr_update_inode(state->inode, sattr);
  1731. nfs_post_op_update_inode(state->inode, &fattr);
  1732. }
  1733. if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
  1734. status = nfs4_intent_set_file(nd, &path, state);
  1735. else
  1736. nfs4_close_state(&path, state, flags);
  1737. out:
  1738. return status;
  1739. }
  1740. static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
  1741. {
  1742. struct nfs_server *server = NFS_SERVER(dir);
  1743. struct nfs_removeargs args = {
  1744. .fh = NFS_FH(dir),
  1745. .name.len = name->len,
  1746. .name.name = name->name,
  1747. .bitmask = server->attr_bitmask,
  1748. };
  1749. struct nfs_removeres res = {
  1750. .server = server,
  1751. };
  1752. struct rpc_message msg = {
  1753. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
  1754. .rpc_argp = &args,
  1755. .rpc_resp = &res,
  1756. };
  1757. int status;
  1758. nfs_fattr_init(&res.dir_attr);
  1759. status = rpc_call_sync(server->client, &msg, 0);
  1760. if (status == 0) {
  1761. update_changeattr(dir, &res.cinfo);
  1762. nfs_post_op_update_inode(dir, &res.dir_attr);
  1763. }
  1764. return status;
  1765. }
  1766. static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
  1767. {
  1768. struct nfs4_exception exception = { };
  1769. int err;
  1770. do {
  1771. err = nfs4_handle_exception(NFS_SERVER(dir),
  1772. _nfs4_proc_remove(dir, name),
  1773. &exception);
  1774. } while (exception.retry);
  1775. return err;
  1776. }
  1777. static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
  1778. {
  1779. struct nfs_server *server = NFS_SERVER(dir);
  1780. struct nfs_removeargs *args = msg->rpc_argp;
  1781. struct nfs_removeres *res = msg->rpc_resp;
  1782. args->bitmask = server->attr_bitmask;
  1783. res->server = server;
  1784. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
  1785. }
  1786. static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
  1787. {
  1788. struct nfs_removeres *res = task->tk_msg.rpc_resp;
  1789. if (nfs4_async_handle_error(task, res->server) == -EAGAIN)
  1790. return 0;
  1791. update_changeattr(dir, &res->cinfo);
  1792. nfs_post_op_update_inode(dir, &res->dir_attr);
  1793. return 1;
  1794. }
  1795. static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
  1796. struct inode *new_dir, struct qstr *new_name)
  1797. {
  1798. struct nfs_server *server = NFS_SERVER(old_dir);
  1799. struct nfs4_rename_arg arg = {
  1800. .old_dir = NFS_FH(old_dir),
  1801. .new_dir = NFS_FH(new_dir),
  1802. .old_name = old_name,
  1803. .new_name = new_name,
  1804. .bitmask = server->attr_bitmask,
  1805. };
  1806. struct nfs_fattr old_fattr, new_fattr;
  1807. struct nfs4_rename_res res = {
  1808. .server = server,
  1809. .old_fattr = &old_fattr,
  1810. .new_fattr = &new_fattr,
  1811. };
  1812. struct rpc_message msg = {
  1813. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
  1814. .rpc_argp = &arg,
  1815. .rpc_resp = &res,
  1816. };
  1817. int status;
  1818. nfs_fattr_init(res.old_fattr);
  1819. nfs_fattr_init(res.new_fattr);
  1820. status = rpc_call_sync(server->client, &msg, 0);
  1821. if (!status) {
  1822. update_changeattr(old_dir, &res.old_cinfo);
  1823. nfs_post_op_update_inode(old_dir, res.old_fattr);
  1824. update_changeattr(new_dir, &res.new_cinfo);
  1825. nfs_post_op_update_inode(new_dir, res.new_fattr);
  1826. }
  1827. return status;
  1828. }
  1829. static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
  1830. struct inode *new_dir, struct qstr *new_name)
  1831. {
  1832. struct nfs4_exception exception = { };
  1833. int err;
  1834. do {
  1835. err = nfs4_handle_exception(NFS_SERVER(old_dir),
  1836. _nfs4_proc_rename(old_dir, old_name,
  1837. new_dir, new_name),
  1838. &exception);
  1839. } while (exception.retry);
  1840. return err;
  1841. }
  1842. static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
  1843. {
  1844. struct nfs_server *server = NFS_SERVER(inode);
  1845. struct nfs4_link_arg arg = {
  1846. .fh = NFS_FH(inode),
  1847. .dir_fh = NFS_FH(dir),
  1848. .name = name,
  1849. .bitmask = server->attr_bitmask,
  1850. };
  1851. struct nfs_fattr fattr, dir_attr;
  1852. struct nfs4_link_res res = {
  1853. .server = server,
  1854. .fattr = &fattr,
  1855. .dir_attr = &dir_attr,
  1856. };
  1857. struct rpc_message msg = {
  1858. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
  1859. .rpc_argp = &arg,
  1860. .rpc_resp = &res,
  1861. };
  1862. int status;
  1863. nfs_fattr_init(res.fattr);
  1864. nfs_fattr_init(res.dir_attr);
  1865. status = rpc_call_sync(server->client, &msg, 0);
  1866. if (!status) {
  1867. update_changeattr(dir, &res.cinfo);
  1868. nfs_post_op_update_inode(dir, res.dir_attr);
  1869. nfs_post_op_update_inode(inode, res.fattr);
  1870. }
  1871. return status;
  1872. }
  1873. static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
  1874. {
  1875. struct nfs4_exception exception = { };
  1876. int err;
  1877. do {
  1878. err = nfs4_handle_exception(NFS_SERVER(inode),
  1879. _nfs4_proc_link(inode, dir, name),
  1880. &exception);
  1881. } while (exception.retry);
  1882. return err;
  1883. }
  1884. static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
  1885. struct page *page, unsigned int len, struct iattr *sattr)
  1886. {
  1887. struct nfs_server *server = NFS_SERVER(dir);
  1888. struct nfs_fh fhandle;
  1889. struct nfs_fattr fattr, dir_fattr;
  1890. struct nfs4_create_arg arg = {
  1891. .dir_fh = NFS_FH(dir),
  1892. .server = server,
  1893. .name = &dentry->d_name,
  1894. .attrs = sattr,
  1895. .ftype = NF4LNK,
  1896. .bitmask = server->attr_bitmask,
  1897. };
  1898. struct nfs4_create_res res = {
  1899. .server = server,
  1900. .fh = &fhandle,
  1901. .fattr = &fattr,
  1902. .dir_fattr = &dir_fattr,
  1903. };
  1904. struct rpc_message msg = {
  1905. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK],
  1906. .rpc_argp = &arg,
  1907. .rpc_resp = &res,
  1908. };
  1909. int status;
  1910. if (len > NFS4_MAXPATHLEN)
  1911. return -ENAMETOOLONG;
  1912. arg.u.symlink.pages = &page;
  1913. arg.u.symlink.len = len;
  1914. nfs_fattr_init(&fattr);
  1915. nfs_fattr_init(&dir_fattr);
  1916. status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
  1917. if (!status) {
  1918. update_changeattr(dir, &res.dir_cinfo);
  1919. nfs_post_op_update_inode(dir, res.dir_fattr);
  1920. status = nfs_instantiate(dentry, &fhandle, &fattr);
  1921. }
  1922. return status;
  1923. }
  1924. static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
  1925. struct page *page, unsigned int len, struct iattr *sattr)
  1926. {
  1927. struct nfs4_exception exception = { };
  1928. int err;
  1929. do {
  1930. err = nfs4_handle_exception(NFS_SERVER(dir),
  1931. _nfs4_proc_symlink(dir, dentry, page,
  1932. len, sattr),
  1933. &exception);
  1934. } while (exception.retry);
  1935. return err;
  1936. }
  1937. static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
  1938. struct iattr *sattr)
  1939. {
  1940. struct nfs_server *server = NFS_SERVER(dir);
  1941. struct nfs_fh fhandle;
  1942. struct nfs_fattr fattr, dir_fattr;
  1943. struct nfs4_create_arg arg = {
  1944. .dir_fh = NFS_FH(dir),
  1945. .server = server,
  1946. .name = &dentry->d_name,
  1947. .attrs = sattr,
  1948. .ftype = NF4DIR,
  1949. .bitmask = server->attr_bitmask,
  1950. };
  1951. struct nfs4_create_res res = {
  1952. .server = server,
  1953. .fh = &fhandle,
  1954. .fattr = &fattr,
  1955. .dir_fattr = &dir_fattr,
  1956. };
  1957. struct rpc_message msg = {
  1958. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
  1959. .rpc_argp = &arg,
  1960. .rpc_resp = &res,
  1961. };
  1962. int status;
  1963. nfs_fattr_init(&fattr);
  1964. nfs_fattr_init(&dir_fattr);
  1965. status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
  1966. if (!status) {
  1967. update_changeattr(dir, &res.dir_cinfo);
  1968. nfs_post_op_update_inode(dir, res.dir_fattr);
  1969. status = nfs_instantiate(dentry, &fhandle, &fattr);
  1970. }
  1971. return status;
  1972. }
  1973. static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
  1974. struct iattr *sattr)
  1975. {
  1976. struct nfs4_exception exception = { };
  1977. int err;
  1978. do {
  1979. err = nfs4_handle_exception(NFS_SERVER(dir),
  1980. _nfs4_proc_mkdir(dir, dentry, sattr),
  1981. &exception);
  1982. } while (exception.retry);
  1983. return err;
  1984. }
  1985. static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
  1986. u64 cookie, struct page *page, unsigned int count, int plus)
  1987. {
  1988. struct inode *dir = dentry->d_inode;
  1989. struct nfs4_readdir_arg args = {
  1990. .fh = NFS_FH(dir),
  1991. .pages = &page,
  1992. .pgbase = 0,
  1993. .count = count,
  1994. .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
  1995. };
  1996. struct nfs4_readdir_res res;
  1997. struct rpc_message msg = {
  1998. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
  1999. .rpc_argp = &args,
  2000. .rpc_resp = &res,
  2001. .rpc_cred = cred,
  2002. };
  2003. int status;
  2004. dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __FUNCTION__,
  2005. dentry->d_parent->d_name.name,
  2006. dentry->d_name.name,
  2007. (unsigned long long)cookie);
  2008. nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
  2009. res.pgbase = args.pgbase;
  2010. status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
  2011. if (status == 0)
  2012. memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
  2013. nfs_invalidate_atime(dir);
  2014. dprintk("%s: returns %d\n", __FUNCTION__, status);
  2015. return status;
  2016. }
  2017. static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
  2018. u64 cookie, struct page *page, unsigned int count, int plus)
  2019. {
  2020. struct nfs4_exception exception = { };
  2021. int err;
  2022. do {
  2023. err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
  2024. _nfs4_proc_readdir(dentry, cred, cookie,
  2025. page, count, plus),
  2026. &exception);
  2027. } while (exception.retry);
  2028. return err;
  2029. }
  2030. static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
  2031. struct iattr *sattr, dev_t rdev)
  2032. {
  2033. struct nfs_server *server = NFS_SERVER(dir);
  2034. struct nfs_fh fh;
  2035. struct nfs_fattr fattr, dir_fattr;
  2036. struct nfs4_create_arg arg = {
  2037. .dir_fh = NFS_FH(dir),
  2038. .server = server,
  2039. .name = &dentry->d_name,
  2040. .attrs = sattr,
  2041. .bitmask = server->attr_bitmask,
  2042. };
  2043. struct nfs4_create_res res = {
  2044. .server = server,
  2045. .fh = &fh,
  2046. .fattr = &fattr,
  2047. .dir_fattr = &dir_fattr,
  2048. };
  2049. struct rpc_message msg = {
  2050. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
  2051. .rpc_argp = &arg,
  2052. .rpc_resp = &res,
  2053. };
  2054. int status;
  2055. int mode = sattr->ia_mode;
  2056. nfs_fattr_init(&fattr);
  2057. nfs_fattr_init(&dir_fattr);
  2058. BUG_ON(!(sattr->ia_valid & ATTR_MODE));
  2059. BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
  2060. if (S_ISFIFO(mode))
  2061. arg.ftype = NF4FIFO;
  2062. else if (S_ISBLK(mode)) {
  2063. arg.ftype = NF4BLK;
  2064. arg.u.device.specdata1 = MAJOR(rdev);
  2065. arg.u.device.specdata2 = MINOR(rdev);
  2066. }
  2067. else if (S_ISCHR(mode)) {
  2068. arg.ftype = NF4CHR;
  2069. arg.u.device.specdata1 = MAJOR(rdev);
  2070. arg.u.device.specdata2 = MINOR(rdev);
  2071. }
  2072. else
  2073. arg.ftype = NF4SOCK;
  2074. status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
  2075. if (status == 0) {
  2076. update_changeattr(dir, &res.dir_cinfo);
  2077. nfs_post_op_update_inode(dir, res.dir_fattr);
  2078. status = nfs_instantiate(dentry, &fh, &fattr);
  2079. }
  2080. return status;
  2081. }
  2082. static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
  2083. struct iattr *sattr, dev_t rdev)
  2084. {
  2085. struct nfs4_exception exception = { };
  2086. int err;
  2087. do {
  2088. err = nfs4_handle_exception(NFS_SERVER(dir),
  2089. _nfs4_proc_mknod(dir, dentry, sattr, rdev),
  2090. &exception);
  2091. } while (exception.retry);
  2092. return err;
  2093. }
  2094. static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
  2095. struct nfs_fsstat *fsstat)
  2096. {
  2097. struct nfs4_statfs_arg args = {
  2098. .fh = fhandle,
  2099. .bitmask = server->attr_bitmask,
  2100. };
  2101. struct rpc_message msg = {
  2102. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
  2103. .rpc_argp = &args,
  2104. .rpc_resp = fsstat,
  2105. };
  2106. nfs_fattr_init(fsstat->fattr);
  2107. return rpc_call_sync(server->client, &msg, 0);
  2108. }
  2109. static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
  2110. {
  2111. struct nfs4_exception exception = { };
  2112. int err;
  2113. do {
  2114. err = nfs4_handle_exception(server,
  2115. _nfs4_proc_statfs(server, fhandle, fsstat),
  2116. &exception);
  2117. } while (exception.retry);
  2118. return err;
  2119. }
  2120. static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
  2121. struct nfs_fsinfo *fsinfo)
  2122. {
  2123. struct nfs4_fsinfo_arg args = {
  2124. .fh = fhandle,
  2125. .bitmask = server->attr_bitmask,
  2126. };
  2127. struct rpc_message msg = {
  2128. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
  2129. .rpc_argp = &args,
  2130. .rpc_resp = fsinfo,
  2131. };
  2132. return rpc_call_sync(server->client, &msg, 0);
  2133. }
  2134. static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
  2135. {
  2136. struct nfs4_exception exception = { };
  2137. int err;
  2138. do {
  2139. err = nfs4_handle_exception(server,
  2140. _nfs4_do_fsinfo(server, fhandle, fsinfo),
  2141. &exception);
  2142. } while (exception.retry);
  2143. return err;
  2144. }
  2145. static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
  2146. {
  2147. nfs_fattr_init(fsinfo->fattr);
  2148. return nfs4_do_fsinfo(server, fhandle, fsinfo);
  2149. }
  2150. static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
  2151. struct nfs_pathconf *pathconf)
  2152. {
  2153. struct nfs4_pathconf_arg args = {
  2154. .fh = fhandle,
  2155. .bitmask = server->attr_bitmask,
  2156. };
  2157. struct rpc_message msg = {
  2158. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
  2159. .rpc_argp = &args,
  2160. .rpc_resp = pathconf,
  2161. };
  2162. /* None of the pathconf attributes are mandatory to implement */
  2163. if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
  2164. memset(pathconf, 0, sizeof(*pathconf));
  2165. return 0;
  2166. }
  2167. nfs_fattr_init(pathconf->fattr);
  2168. return rpc_call_sync(server->client, &msg, 0);
  2169. }
  2170. static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
  2171. struct nfs_pathconf *pathconf)
  2172. {
  2173. struct nfs4_exception exception = { };
  2174. int err;
  2175. do {
  2176. err = nfs4_handle_exception(server,
  2177. _nfs4_proc_pathconf(server, fhandle, pathconf),
  2178. &exception);
  2179. } while (exception.retry);
  2180. return err;
  2181. }
  2182. static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
  2183. {
  2184. struct nfs_server *server = NFS_SERVER(data->inode);
  2185. if (nfs4_async_handle_error(task, server) == -EAGAIN) {
  2186. rpc_restart_call(task);
  2187. return -EAGAIN;
  2188. }
  2189. nfs_invalidate_atime(data->inode);
  2190. if (task->tk_status > 0)
  2191. renew_lease(server, data->timestamp);
  2192. return 0;
  2193. }
  2194. static void nfs4_proc_read_setup(struct nfs_read_data *data)
  2195. {
  2196. struct rpc_message msg = {
  2197. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
  2198. .rpc_argp = &data->args,
  2199. .rpc_resp = &data->res,
  2200. .rpc_cred = data->cred,
  2201. };
  2202. data->timestamp = jiffies;
  2203. rpc_call_setup(&data->task, &msg, 0);
  2204. }
  2205. static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
  2206. {
  2207. struct inode *inode = data->inode;
  2208. if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
  2209. rpc_restart_call(task);
  2210. return -EAGAIN;
  2211. }
  2212. if (task->tk_status >= 0) {
  2213. renew_lease(NFS_SERVER(inode), data->timestamp);
  2214. nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
  2215. }
  2216. return 0;
  2217. }
  2218. static void nfs4_proc_write_setup(struct nfs_write_data *data, int how)
  2219. {
  2220. struct rpc_message msg = {
  2221. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
  2222. .rpc_argp = &data->args,
  2223. .rpc_resp = &data->res,
  2224. .rpc_cred = data->cred,
  2225. };
  2226. struct inode *inode = data->inode;
  2227. struct nfs_server *server = NFS_SERVER(inode);
  2228. int stable;
  2229. if (how & FLUSH_STABLE) {
  2230. if (!NFS_I(inode)->ncommit)
  2231. stable = NFS_FILE_SYNC;
  2232. else
  2233. stable = NFS_DATA_SYNC;
  2234. } else
  2235. stable = NFS_UNSTABLE;
  2236. data->args.stable = stable;
  2237. data->args.bitmask = server->attr_bitmask;
  2238. data->res.server = server;
  2239. data->timestamp = jiffies;
  2240. /* Finalize the task. */
  2241. rpc_call_setup(&data->task, &msg, 0);
  2242. }
  2243. static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
  2244. {
  2245. struct inode *inode = data->inode;
  2246. if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
  2247. rpc_restart_call(task);
  2248. return -EAGAIN;
  2249. }
  2250. if (task->tk_status >= 0)
  2251. nfs_post_op_update_inode(inode, data->res.fattr);
  2252. return 0;
  2253. }
  2254. static void nfs4_proc_commit_setup(struct nfs_write_data *data, int how)
  2255. {
  2256. struct rpc_message msg = {
  2257. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
  2258. .rpc_argp = &data->args,
  2259. .rpc_resp = &data->res,
  2260. .rpc_cred = data->cred,
  2261. };
  2262. struct nfs_server *server = NFS_SERVER(data->inode);
  2263. data->args.bitmask = server->attr_bitmask;
  2264. data->res.server = server;
  2265. rpc_call_setup(&data->task, &msg, 0);
  2266. }
  2267. /*
  2268. * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
  2269. * standalone procedure for queueing an asynchronous RENEW.
  2270. */
  2271. static void nfs4_renew_done(struct rpc_task *task, void *data)
  2272. {
  2273. struct nfs_client *clp = (struct nfs_client *)task->tk_msg.rpc_argp;
  2274. unsigned long timestamp = (unsigned long)data;
  2275. if (task->tk_status < 0) {
  2276. switch (task->tk_status) {
  2277. case -NFS4ERR_STALE_CLIENTID:
  2278. case -NFS4ERR_EXPIRED:
  2279. case -NFS4ERR_CB_PATH_DOWN:
  2280. nfs4_schedule_state_recovery(clp);
  2281. }
  2282. return;
  2283. }
  2284. spin_lock(&clp->cl_lock);
  2285. if (time_before(clp->cl_last_renewal,timestamp))
  2286. clp->cl_last_renewal = timestamp;
  2287. spin_unlock(&clp->cl_lock);
  2288. }
  2289. static const struct rpc_call_ops nfs4_renew_ops = {
  2290. .rpc_call_done = nfs4_renew_done,
  2291. };
  2292. int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
  2293. {
  2294. struct rpc_message msg = {
  2295. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
  2296. .rpc_argp = clp,
  2297. .rpc_cred = cred,
  2298. };
  2299. return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
  2300. &nfs4_renew_ops, (void *)jiffies);
  2301. }
  2302. int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
  2303. {
  2304. struct rpc_message msg = {
  2305. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
  2306. .rpc_argp = clp,
  2307. .rpc_cred = cred,
  2308. };
  2309. unsigned long now = jiffies;
  2310. int status;
  2311. status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
  2312. if (status < 0)
  2313. return status;
  2314. spin_lock(&clp->cl_lock);
  2315. if (time_before(clp->cl_last_renewal,now))
  2316. clp->cl_last_renewal = now;
  2317. spin_unlock(&clp->cl_lock);
  2318. return 0;
  2319. }
  2320. static inline int nfs4_server_supports_acls(struct nfs_server *server)
  2321. {
  2322. return (server->caps & NFS_CAP_ACLS)
  2323. && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
  2324. && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
  2325. }
  2326. /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
  2327. * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
  2328. * the stack.
  2329. */
  2330. #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
  2331. static void buf_to_pages(const void *buf, size_t buflen,
  2332. struct page **pages, unsigned int *pgbase)
  2333. {
  2334. const void *p = buf;
  2335. *pgbase = offset_in_page(buf);
  2336. p -= *pgbase;
  2337. while (p < buf + buflen) {
  2338. *(pages++) = virt_to_page(p);
  2339. p += PAGE_CACHE_SIZE;
  2340. }
  2341. }
  2342. struct nfs4_cached_acl {
  2343. int cached;
  2344. size_t len;
  2345. char data[0];
  2346. };
  2347. static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
  2348. {
  2349. struct nfs_inode *nfsi = NFS_I(inode);
  2350. spin_lock(&inode->i_lock);
  2351. kfree(nfsi->nfs4_acl);
  2352. nfsi->nfs4_acl = acl;
  2353. spin_unlock(&inode->i_lock);
  2354. }
  2355. static void nfs4_zap_acl_attr(struct inode *inode)
  2356. {
  2357. nfs4_set_cached_acl(inode, NULL);
  2358. }
  2359. static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
  2360. {
  2361. struct nfs_inode *nfsi = NFS_I(inode);
  2362. struct nfs4_cached_acl *acl;
  2363. int ret = -ENOENT;
  2364. spin_lock(&inode->i_lock);
  2365. acl = nfsi->nfs4_acl;
  2366. if (acl == NULL)
  2367. goto out;
  2368. if (buf == NULL) /* user is just asking for length */
  2369. goto out_len;
  2370. if (acl->cached == 0)
  2371. goto out;
  2372. ret = -ERANGE; /* see getxattr(2) man page */
  2373. if (acl->len > buflen)
  2374. goto out;
  2375. memcpy(buf, acl->data, acl->len);
  2376. out_len:
  2377. ret = acl->len;
  2378. out:
  2379. spin_unlock(&inode->i_lock);
  2380. return ret;
  2381. }
  2382. static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
  2383. {
  2384. struct nfs4_cached_acl *acl;
  2385. if (buf && acl_len <= PAGE_SIZE) {
  2386. acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
  2387. if (acl == NULL)
  2388. goto out;
  2389. acl->cached = 1;
  2390. memcpy(acl->data, buf, acl_len);
  2391. } else {
  2392. acl = kmalloc(sizeof(*acl), GFP_KERNEL);
  2393. if (acl == NULL)
  2394. goto out;
  2395. acl->cached = 0;
  2396. }
  2397. acl->len = acl_len;
  2398. out:
  2399. nfs4_set_cached_acl(inode, acl);
  2400. }
  2401. static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
  2402. {
  2403. struct page *pages[NFS4ACL_MAXPAGES];
  2404. struct nfs_getaclargs args = {
  2405. .fh = NFS_FH(inode),
  2406. .acl_pages = pages,
  2407. .acl_len = buflen,
  2408. };
  2409. size_t resp_len = buflen;
  2410. void *resp_buf;
  2411. struct rpc_message msg = {
  2412. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
  2413. .rpc_argp = &args,
  2414. .rpc_resp = &resp_len,
  2415. };
  2416. struct page *localpage = NULL;
  2417. int ret;
  2418. if (buflen < PAGE_SIZE) {
  2419. /* As long as we're doing a round trip to the server anyway,
  2420. * let's be prepared for a page of acl data. */
  2421. localpage = alloc_page(GFP_KERNEL);
  2422. resp_buf = page_address(localpage);
  2423. if (localpage == NULL)
  2424. return -ENOMEM;
  2425. args.acl_pages[0] = localpage;
  2426. args.acl_pgbase = 0;
  2427. resp_len = args.acl_len = PAGE_SIZE;
  2428. } else {
  2429. resp_buf = buf;
  2430. buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
  2431. }
  2432. ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
  2433. if (ret)
  2434. goto out_free;
  2435. if (resp_len > args.acl_len)
  2436. nfs4_write_cached_acl(inode, NULL, resp_len);
  2437. else
  2438. nfs4_write_cached_acl(inode, resp_buf, resp_len);
  2439. if (buf) {
  2440. ret = -ERANGE;
  2441. if (resp_len > buflen)
  2442. goto out_free;
  2443. if (localpage)
  2444. memcpy(buf, resp_buf, resp_len);
  2445. }
  2446. ret = resp_len;
  2447. out_free:
  2448. if (localpage)
  2449. __free_page(localpage);
  2450. return ret;
  2451. }
  2452. static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
  2453. {
  2454. struct nfs4_exception exception = { };
  2455. ssize_t ret;
  2456. do {
  2457. ret = __nfs4_get_acl_uncached(inode, buf, buflen);
  2458. if (ret >= 0)
  2459. break;
  2460. ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
  2461. } while (exception.retry);
  2462. return ret;
  2463. }
  2464. static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
  2465. {
  2466. struct nfs_server *server = NFS_SERVER(inode);
  2467. int ret;
  2468. if (!nfs4_server_supports_acls(server))
  2469. return -EOPNOTSUPP;
  2470. ret = nfs_revalidate_inode(server, inode);
  2471. if (ret < 0)
  2472. return ret;
  2473. ret = nfs4_read_cached_acl(inode, buf, buflen);
  2474. if (ret != -ENOENT)
  2475. return ret;
  2476. return nfs4_get_acl_uncached(inode, buf, buflen);
  2477. }
  2478. static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
  2479. {
  2480. struct nfs_server *server = NFS_SERVER(inode);
  2481. struct page *pages[NFS4ACL_MAXPAGES];
  2482. struct nfs_setaclargs arg = {
  2483. .fh = NFS_FH(inode),
  2484. .acl_pages = pages,
  2485. .acl_len = buflen,
  2486. };
  2487. struct rpc_message msg = {
  2488. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
  2489. .rpc_argp = &arg,
  2490. .rpc_resp = NULL,
  2491. };
  2492. int ret;
  2493. if (!nfs4_server_supports_acls(server))
  2494. return -EOPNOTSUPP;
  2495. nfs_inode_return_delegation(inode);
  2496. buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
  2497. ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
  2498. nfs_zap_caches(inode);
  2499. return ret;
  2500. }
  2501. static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
  2502. {
  2503. struct nfs4_exception exception = { };
  2504. int err;
  2505. do {
  2506. err = nfs4_handle_exception(NFS_SERVER(inode),
  2507. __nfs4_proc_set_acl(inode, buf, buflen),
  2508. &exception);
  2509. } while (exception.retry);
  2510. return err;
  2511. }
  2512. static int
  2513. nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server)
  2514. {
  2515. struct nfs_client *clp = server->nfs_client;
  2516. if (!clp || task->tk_status >= 0)
  2517. return 0;
  2518. switch(task->tk_status) {
  2519. case -NFS4ERR_STALE_CLIENTID:
  2520. case -NFS4ERR_STALE_STATEID:
  2521. case -NFS4ERR_EXPIRED:
  2522. rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL, NULL);
  2523. nfs4_schedule_state_recovery(clp);
  2524. if (test_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) == 0)
  2525. rpc_wake_up_task(task);
  2526. task->tk_status = 0;
  2527. return -EAGAIN;
  2528. case -NFS4ERR_DELAY:
  2529. nfs_inc_server_stats((struct nfs_server *) server,
  2530. NFSIOS_DELAY);
  2531. case -NFS4ERR_GRACE:
  2532. rpc_delay(task, NFS4_POLL_RETRY_MAX);
  2533. task->tk_status = 0;
  2534. return -EAGAIN;
  2535. case -NFS4ERR_OLD_STATEID:
  2536. task->tk_status = 0;
  2537. return -EAGAIN;
  2538. }
  2539. task->tk_status = nfs4_map_errors(task->tk_status);
  2540. return 0;
  2541. }
  2542. static int nfs4_wait_bit_interruptible(void *word)
  2543. {
  2544. if (signal_pending(current))
  2545. return -ERESTARTSYS;
  2546. schedule();
  2547. return 0;
  2548. }
  2549. static int nfs4_wait_clnt_recover(struct rpc_clnt *clnt, struct nfs_client *clp)
  2550. {
  2551. sigset_t oldset;
  2552. int res;
  2553. might_sleep();
  2554. rwsem_acquire(&clp->cl_sem.dep_map, 0, 0, _RET_IP_);
  2555. rpc_clnt_sigmask(clnt, &oldset);
  2556. res = wait_on_bit(&clp->cl_state, NFS4CLNT_STATE_RECOVER,
  2557. nfs4_wait_bit_interruptible,
  2558. TASK_INTERRUPTIBLE);
  2559. rpc_clnt_sigunmask(clnt, &oldset);
  2560. rwsem_release(&clp->cl_sem.dep_map, 1, _RET_IP_);
  2561. return res;
  2562. }
  2563. static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
  2564. {
  2565. sigset_t oldset;
  2566. int res = 0;
  2567. might_sleep();
  2568. if (*timeout <= 0)
  2569. *timeout = NFS4_POLL_RETRY_MIN;
  2570. if (*timeout > NFS4_POLL_RETRY_MAX)
  2571. *timeout = NFS4_POLL_RETRY_MAX;
  2572. rpc_clnt_sigmask(clnt, &oldset);
  2573. if (clnt->cl_intr) {
  2574. schedule_timeout_interruptible(*timeout);
  2575. if (signalled())
  2576. res = -ERESTARTSYS;
  2577. } else
  2578. schedule_timeout_uninterruptible(*timeout);
  2579. rpc_clnt_sigunmask(clnt, &oldset);
  2580. *timeout <<= 1;
  2581. return res;
  2582. }
  2583. /* This is the error handling routine for processes that are allowed
  2584. * to sleep.
  2585. */
  2586. static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
  2587. {
  2588. struct nfs_client *clp = server->nfs_client;
  2589. int ret = errorcode;
  2590. exception->retry = 0;
  2591. switch(errorcode) {
  2592. case 0:
  2593. return 0;
  2594. case -NFS4ERR_STALE_CLIENTID:
  2595. case -NFS4ERR_STALE_STATEID:
  2596. case -NFS4ERR_EXPIRED:
  2597. nfs4_schedule_state_recovery(clp);
  2598. ret = nfs4_wait_clnt_recover(server->client, clp);
  2599. if (ret == 0)
  2600. exception->retry = 1;
  2601. break;
  2602. case -NFS4ERR_FILE_OPEN:
  2603. case -NFS4ERR_GRACE:
  2604. case -NFS4ERR_DELAY:
  2605. ret = nfs4_delay(server->client, &exception->timeout);
  2606. if (ret != 0)
  2607. break;
  2608. case -NFS4ERR_OLD_STATEID:
  2609. exception->retry = 1;
  2610. }
  2611. /* We failed to handle the error */
  2612. return nfs4_map_errors(ret);
  2613. }
  2614. int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
  2615. {
  2616. nfs4_verifier sc_verifier;
  2617. struct nfs4_setclientid setclientid = {
  2618. .sc_verifier = &sc_verifier,
  2619. .sc_prog = program,
  2620. };
  2621. struct rpc_message msg = {
  2622. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
  2623. .rpc_argp = &setclientid,
  2624. .rpc_resp = clp,
  2625. .rpc_cred = cred,
  2626. };
  2627. __be32 *p;
  2628. int loop = 0;
  2629. int status;
  2630. p = (__be32*)sc_verifier.data;
  2631. *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
  2632. *p = htonl((u32)clp->cl_boot_time.tv_nsec);
  2633. for(;;) {
  2634. setclientid.sc_name_len = scnprintf(setclientid.sc_name,
  2635. sizeof(setclientid.sc_name), "%s/%u.%u.%u.%u %s %u",
  2636. clp->cl_ipaddr, NIPQUAD(clp->cl_addr.sin_addr),
  2637. cred->cr_ops->cr_name,
  2638. clp->cl_id_uniquifier);
  2639. setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
  2640. sizeof(setclientid.sc_netid), "tcp");
  2641. setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
  2642. sizeof(setclientid.sc_uaddr), "%s.%d.%d",
  2643. clp->cl_ipaddr, port >> 8, port & 255);
  2644. status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
  2645. if (status != -NFS4ERR_CLID_INUSE)
  2646. break;
  2647. if (signalled())
  2648. break;
  2649. if (loop++ & 1)
  2650. ssleep(clp->cl_lease_time + 1);
  2651. else
  2652. if (++clp->cl_id_uniquifier == 0)
  2653. break;
  2654. }
  2655. return status;
  2656. }
  2657. static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
  2658. {
  2659. struct nfs_fsinfo fsinfo;
  2660. struct rpc_message msg = {
  2661. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
  2662. .rpc_argp = clp,
  2663. .rpc_resp = &fsinfo,
  2664. .rpc_cred = cred,
  2665. };
  2666. unsigned long now;
  2667. int status;
  2668. now = jiffies;
  2669. status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
  2670. if (status == 0) {
  2671. spin_lock(&clp->cl_lock);
  2672. clp->cl_lease_time = fsinfo.lease_time * HZ;
  2673. clp->cl_last_renewal = now;
  2674. clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  2675. spin_unlock(&clp->cl_lock);
  2676. }
  2677. return status;
  2678. }
  2679. int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
  2680. {
  2681. long timeout;
  2682. int err;
  2683. do {
  2684. err = _nfs4_proc_setclientid_confirm(clp, cred);
  2685. switch (err) {
  2686. case 0:
  2687. return err;
  2688. case -NFS4ERR_RESOURCE:
  2689. /* The IBM lawyers misread another document! */
  2690. case -NFS4ERR_DELAY:
  2691. err = nfs4_delay(clp->cl_rpcclient, &timeout);
  2692. }
  2693. } while (err == 0);
  2694. return err;
  2695. }
  2696. struct nfs4_delegreturndata {
  2697. struct nfs4_delegreturnargs args;
  2698. struct nfs4_delegreturnres res;
  2699. struct nfs_fh fh;
  2700. nfs4_stateid stateid;
  2701. struct rpc_cred *cred;
  2702. unsigned long timestamp;
  2703. struct nfs_fattr fattr;
  2704. int rpc_status;
  2705. };
  2706. static void nfs4_delegreturn_prepare(struct rpc_task *task, void *calldata)
  2707. {
  2708. struct nfs4_delegreturndata *data = calldata;
  2709. struct rpc_message msg = {
  2710. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
  2711. .rpc_argp = &data->args,
  2712. .rpc_resp = &data->res,
  2713. .rpc_cred = data->cred,
  2714. };
  2715. nfs_fattr_init(data->res.fattr);
  2716. rpc_call_setup(task, &msg, 0);
  2717. }
  2718. static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
  2719. {
  2720. struct nfs4_delegreturndata *data = calldata;
  2721. data->rpc_status = task->tk_status;
  2722. if (data->rpc_status == 0)
  2723. renew_lease(data->res.server, data->timestamp);
  2724. }
  2725. static void nfs4_delegreturn_release(void *calldata)
  2726. {
  2727. struct nfs4_delegreturndata *data = calldata;
  2728. put_rpccred(data->cred);
  2729. kfree(calldata);
  2730. }
  2731. static const struct rpc_call_ops nfs4_delegreturn_ops = {
  2732. .rpc_call_prepare = nfs4_delegreturn_prepare,
  2733. .rpc_call_done = nfs4_delegreturn_done,
  2734. .rpc_release = nfs4_delegreturn_release,
  2735. };
  2736. static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
  2737. {
  2738. struct nfs4_delegreturndata *data;
  2739. struct nfs_server *server = NFS_SERVER(inode);
  2740. struct rpc_task *task;
  2741. int status;
  2742. data = kmalloc(sizeof(*data), GFP_KERNEL);
  2743. if (data == NULL)
  2744. return -ENOMEM;
  2745. data->args.fhandle = &data->fh;
  2746. data->args.stateid = &data->stateid;
  2747. data->args.bitmask = server->attr_bitmask;
  2748. nfs_copy_fh(&data->fh, NFS_FH(inode));
  2749. memcpy(&data->stateid, stateid, sizeof(data->stateid));
  2750. data->res.fattr = &data->fattr;
  2751. data->res.server = server;
  2752. data->cred = get_rpccred(cred);
  2753. data->timestamp = jiffies;
  2754. data->rpc_status = 0;
  2755. task = rpc_run_task(NFS_CLIENT(inode), RPC_TASK_ASYNC, &nfs4_delegreturn_ops, data);
  2756. if (IS_ERR(task))
  2757. return PTR_ERR(task);
  2758. status = nfs4_wait_for_completion_rpc_task(task);
  2759. if (status == 0) {
  2760. status = data->rpc_status;
  2761. if (status == 0)
  2762. nfs_post_op_update_inode(inode, &data->fattr);
  2763. }
  2764. rpc_put_task(task);
  2765. return status;
  2766. }
  2767. int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
  2768. {
  2769. struct nfs_server *server = NFS_SERVER(inode);
  2770. struct nfs4_exception exception = { };
  2771. int err;
  2772. do {
  2773. err = _nfs4_proc_delegreturn(inode, cred, stateid);
  2774. switch (err) {
  2775. case -NFS4ERR_STALE_STATEID:
  2776. case -NFS4ERR_EXPIRED:
  2777. case 0:
  2778. return 0;
  2779. }
  2780. err = nfs4_handle_exception(server, err, &exception);
  2781. } while (exception.retry);
  2782. return err;
  2783. }
  2784. #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
  2785. #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
  2786. /*
  2787. * sleep, with exponential backoff, and retry the LOCK operation.
  2788. */
  2789. static unsigned long
  2790. nfs4_set_lock_task_retry(unsigned long timeout)
  2791. {
  2792. schedule_timeout_interruptible(timeout);
  2793. timeout <<= 1;
  2794. if (timeout > NFS4_LOCK_MAXTIMEOUT)
  2795. return NFS4_LOCK_MAXTIMEOUT;
  2796. return timeout;
  2797. }
  2798. static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  2799. {
  2800. struct inode *inode = state->inode;
  2801. struct nfs_server *server = NFS_SERVER(inode);
  2802. struct nfs_client *clp = server->nfs_client;
  2803. struct nfs_lockt_args arg = {
  2804. .fh = NFS_FH(inode),
  2805. .fl = request,
  2806. };
  2807. struct nfs_lockt_res res = {
  2808. .denied = request,
  2809. };
  2810. struct rpc_message msg = {
  2811. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
  2812. .rpc_argp = &arg,
  2813. .rpc_resp = &res,
  2814. .rpc_cred = state->owner->so_cred,
  2815. };
  2816. struct nfs4_lock_state *lsp;
  2817. int status;
  2818. down_read(&clp->cl_sem);
  2819. arg.lock_owner.clientid = clp->cl_clientid;
  2820. status = nfs4_set_lock_state(state, request);
  2821. if (status != 0)
  2822. goto out;
  2823. lsp = request->fl_u.nfs4_fl.owner;
  2824. arg.lock_owner.id = lsp->ls_id.id;
  2825. status = rpc_call_sync(server->client, &msg, 0);
  2826. switch (status) {
  2827. case 0:
  2828. request->fl_type = F_UNLCK;
  2829. break;
  2830. case -NFS4ERR_DENIED:
  2831. status = 0;
  2832. }
  2833. request->fl_ops->fl_release_private(request);
  2834. out:
  2835. up_read(&clp->cl_sem);
  2836. return status;
  2837. }
  2838. static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  2839. {
  2840. struct nfs4_exception exception = { };
  2841. int err;
  2842. do {
  2843. err = nfs4_handle_exception(NFS_SERVER(state->inode),
  2844. _nfs4_proc_getlk(state, cmd, request),
  2845. &exception);
  2846. } while (exception.retry);
  2847. return err;
  2848. }
  2849. static int do_vfs_lock(struct file *file, struct file_lock *fl)
  2850. {
  2851. int res = 0;
  2852. switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
  2853. case FL_POSIX:
  2854. res = posix_lock_file_wait(file, fl);
  2855. break;
  2856. case FL_FLOCK:
  2857. res = flock_lock_file_wait(file, fl);
  2858. break;
  2859. default:
  2860. BUG();
  2861. }
  2862. return res;
  2863. }
  2864. struct nfs4_unlockdata {
  2865. struct nfs_locku_args arg;
  2866. struct nfs_locku_res res;
  2867. struct nfs4_lock_state *lsp;
  2868. struct nfs_open_context *ctx;
  2869. struct file_lock fl;
  2870. const struct nfs_server *server;
  2871. unsigned long timestamp;
  2872. };
  2873. static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
  2874. struct nfs_open_context *ctx,
  2875. struct nfs4_lock_state *lsp,
  2876. struct nfs_seqid *seqid)
  2877. {
  2878. struct nfs4_unlockdata *p;
  2879. struct inode *inode = lsp->ls_state->inode;
  2880. p = kmalloc(sizeof(*p), GFP_KERNEL);
  2881. if (p == NULL)
  2882. return NULL;
  2883. p->arg.fh = NFS_FH(inode);
  2884. p->arg.fl = &p->fl;
  2885. p->arg.seqid = seqid;
  2886. p->arg.stateid = &lsp->ls_stateid;
  2887. p->lsp = lsp;
  2888. atomic_inc(&lsp->ls_count);
  2889. /* Ensure we don't close file until we're done freeing locks! */
  2890. p->ctx = get_nfs_open_context(ctx);
  2891. memcpy(&p->fl, fl, sizeof(p->fl));
  2892. p->server = NFS_SERVER(inode);
  2893. return p;
  2894. }
  2895. static void nfs4_locku_release_calldata(void *data)
  2896. {
  2897. struct nfs4_unlockdata *calldata = data;
  2898. nfs_free_seqid(calldata->arg.seqid);
  2899. nfs4_put_lock_state(calldata->lsp);
  2900. put_nfs_open_context(calldata->ctx);
  2901. kfree(calldata);
  2902. }
  2903. static void nfs4_locku_done(struct rpc_task *task, void *data)
  2904. {
  2905. struct nfs4_unlockdata *calldata = data;
  2906. if (RPC_ASSASSINATED(task))
  2907. return;
  2908. nfs_increment_lock_seqid(task->tk_status, calldata->arg.seqid);
  2909. switch (task->tk_status) {
  2910. case 0:
  2911. memcpy(calldata->lsp->ls_stateid.data,
  2912. calldata->res.stateid.data,
  2913. sizeof(calldata->lsp->ls_stateid.data));
  2914. renew_lease(calldata->server, calldata->timestamp);
  2915. break;
  2916. case -NFS4ERR_STALE_STATEID:
  2917. case -NFS4ERR_EXPIRED:
  2918. break;
  2919. default:
  2920. if (nfs4_async_handle_error(task, calldata->server) == -EAGAIN)
  2921. rpc_restart_call(task);
  2922. }
  2923. }
  2924. static void nfs4_locku_prepare(struct rpc_task *task, void *data)
  2925. {
  2926. struct nfs4_unlockdata *calldata = data;
  2927. struct rpc_message msg = {
  2928. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
  2929. .rpc_argp = &calldata->arg,
  2930. .rpc_resp = &calldata->res,
  2931. .rpc_cred = calldata->lsp->ls_state->owner->so_cred,
  2932. };
  2933. if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
  2934. return;
  2935. if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
  2936. /* Note: exit _without_ running nfs4_locku_done */
  2937. task->tk_action = NULL;
  2938. return;
  2939. }
  2940. calldata->timestamp = jiffies;
  2941. rpc_call_setup(task, &msg, 0);
  2942. }
  2943. static const struct rpc_call_ops nfs4_locku_ops = {
  2944. .rpc_call_prepare = nfs4_locku_prepare,
  2945. .rpc_call_done = nfs4_locku_done,
  2946. .rpc_release = nfs4_locku_release_calldata,
  2947. };
  2948. static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
  2949. struct nfs_open_context *ctx,
  2950. struct nfs4_lock_state *lsp,
  2951. struct nfs_seqid *seqid)
  2952. {
  2953. struct nfs4_unlockdata *data;
  2954. /* Ensure this is an unlock - when canceling a lock, the
  2955. * canceled lock is passed in, and it won't be an unlock.
  2956. */
  2957. fl->fl_type = F_UNLCK;
  2958. data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
  2959. if (data == NULL) {
  2960. nfs_free_seqid(seqid);
  2961. return ERR_PTR(-ENOMEM);
  2962. }
  2963. return rpc_run_task(NFS_CLIENT(lsp->ls_state->inode), RPC_TASK_ASYNC, &nfs4_locku_ops, data);
  2964. }
  2965. static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
  2966. {
  2967. struct nfs_seqid *seqid;
  2968. struct nfs4_lock_state *lsp;
  2969. struct rpc_task *task;
  2970. int status = 0;
  2971. status = nfs4_set_lock_state(state, request);
  2972. /* Unlock _before_ we do the RPC call */
  2973. request->fl_flags |= FL_EXISTS;
  2974. if (do_vfs_lock(request->fl_file, request) == -ENOENT)
  2975. goto out;
  2976. if (status != 0)
  2977. goto out;
  2978. /* Is this a delegated lock? */
  2979. if (test_bit(NFS_DELEGATED_STATE, &state->flags))
  2980. goto out;
  2981. lsp = request->fl_u.nfs4_fl.owner;
  2982. seqid = nfs_alloc_seqid(&lsp->ls_seqid);
  2983. status = -ENOMEM;
  2984. if (seqid == NULL)
  2985. goto out;
  2986. task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
  2987. status = PTR_ERR(task);
  2988. if (IS_ERR(task))
  2989. goto out;
  2990. status = nfs4_wait_for_completion_rpc_task(task);
  2991. rpc_put_task(task);
  2992. out:
  2993. return status;
  2994. }
  2995. struct nfs4_lockdata {
  2996. struct nfs_lock_args arg;
  2997. struct nfs_lock_res res;
  2998. struct nfs4_lock_state *lsp;
  2999. struct nfs_open_context *ctx;
  3000. struct file_lock fl;
  3001. unsigned long timestamp;
  3002. int rpc_status;
  3003. int cancelled;
  3004. };
  3005. static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
  3006. struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
  3007. {
  3008. struct nfs4_lockdata *p;
  3009. struct inode *inode = lsp->ls_state->inode;
  3010. struct nfs_server *server = NFS_SERVER(inode);
  3011. p = kzalloc(sizeof(*p), GFP_KERNEL);
  3012. if (p == NULL)
  3013. return NULL;
  3014. p->arg.fh = NFS_FH(inode);
  3015. p->arg.fl = &p->fl;
  3016. p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
  3017. if (p->arg.lock_seqid == NULL)
  3018. goto out_free;
  3019. p->arg.lock_stateid = &lsp->ls_stateid;
  3020. p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
  3021. p->arg.lock_owner.id = lsp->ls_id.id;
  3022. p->lsp = lsp;
  3023. atomic_inc(&lsp->ls_count);
  3024. p->ctx = get_nfs_open_context(ctx);
  3025. memcpy(&p->fl, fl, sizeof(p->fl));
  3026. return p;
  3027. out_free:
  3028. kfree(p);
  3029. return NULL;
  3030. }
  3031. static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
  3032. {
  3033. struct nfs4_lockdata *data = calldata;
  3034. struct nfs4_state *state = data->lsp->ls_state;
  3035. struct nfs4_state_owner *sp = state->owner;
  3036. struct rpc_message msg = {
  3037. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
  3038. .rpc_argp = &data->arg,
  3039. .rpc_resp = &data->res,
  3040. .rpc_cred = sp->so_cred,
  3041. };
  3042. if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
  3043. return;
  3044. dprintk("%s: begin!\n", __FUNCTION__);
  3045. /* Do we need to do an open_to_lock_owner? */
  3046. if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
  3047. data->arg.open_seqid = nfs_alloc_seqid(&sp->so_seqid);
  3048. if (data->arg.open_seqid == NULL) {
  3049. data->rpc_status = -ENOMEM;
  3050. task->tk_action = NULL;
  3051. goto out;
  3052. }
  3053. data->arg.open_stateid = &state->stateid;
  3054. data->arg.new_lock_owner = 1;
  3055. }
  3056. data->timestamp = jiffies;
  3057. rpc_call_setup(task, &msg, 0);
  3058. out:
  3059. dprintk("%s: done!, ret = %d\n", __FUNCTION__, data->rpc_status);
  3060. }
  3061. static void nfs4_lock_done(struct rpc_task *task, void *calldata)
  3062. {
  3063. struct nfs4_lockdata *data = calldata;
  3064. dprintk("%s: begin!\n", __FUNCTION__);
  3065. data->rpc_status = task->tk_status;
  3066. if (RPC_ASSASSINATED(task))
  3067. goto out;
  3068. if (data->arg.new_lock_owner != 0) {
  3069. nfs_increment_open_seqid(data->rpc_status, data->arg.open_seqid);
  3070. if (data->rpc_status == 0)
  3071. nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
  3072. else
  3073. goto out;
  3074. }
  3075. if (data->rpc_status == 0) {
  3076. memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
  3077. sizeof(data->lsp->ls_stateid.data));
  3078. data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
  3079. renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
  3080. }
  3081. nfs_increment_lock_seqid(data->rpc_status, data->arg.lock_seqid);
  3082. out:
  3083. dprintk("%s: done, ret = %d!\n", __FUNCTION__, data->rpc_status);
  3084. }
  3085. static void nfs4_lock_release(void *calldata)
  3086. {
  3087. struct nfs4_lockdata *data = calldata;
  3088. dprintk("%s: begin!\n", __FUNCTION__);
  3089. if (data->arg.open_seqid != NULL)
  3090. nfs_free_seqid(data->arg.open_seqid);
  3091. if (data->cancelled != 0) {
  3092. struct rpc_task *task;
  3093. task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
  3094. data->arg.lock_seqid);
  3095. if (!IS_ERR(task))
  3096. rpc_put_task(task);
  3097. dprintk("%s: cancelling lock!\n", __FUNCTION__);
  3098. } else
  3099. nfs_free_seqid(data->arg.lock_seqid);
  3100. nfs4_put_lock_state(data->lsp);
  3101. put_nfs_open_context(data->ctx);
  3102. kfree(data);
  3103. dprintk("%s: done!\n", __FUNCTION__);
  3104. }
  3105. static const struct rpc_call_ops nfs4_lock_ops = {
  3106. .rpc_call_prepare = nfs4_lock_prepare,
  3107. .rpc_call_done = nfs4_lock_done,
  3108. .rpc_release = nfs4_lock_release,
  3109. };
  3110. static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int reclaim)
  3111. {
  3112. struct nfs4_lockdata *data;
  3113. struct rpc_task *task;
  3114. int ret;
  3115. dprintk("%s: begin!\n", __FUNCTION__);
  3116. data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
  3117. fl->fl_u.nfs4_fl.owner);
  3118. if (data == NULL)
  3119. return -ENOMEM;
  3120. if (IS_SETLKW(cmd))
  3121. data->arg.block = 1;
  3122. if (reclaim != 0)
  3123. data->arg.reclaim = 1;
  3124. task = rpc_run_task(NFS_CLIENT(state->inode), RPC_TASK_ASYNC,
  3125. &nfs4_lock_ops, data);
  3126. if (IS_ERR(task))
  3127. return PTR_ERR(task);
  3128. ret = nfs4_wait_for_completion_rpc_task(task);
  3129. if (ret == 0) {
  3130. ret = data->rpc_status;
  3131. if (ret == -NFS4ERR_DENIED)
  3132. ret = -EAGAIN;
  3133. } else
  3134. data->cancelled = 1;
  3135. rpc_put_task(task);
  3136. dprintk("%s: done, ret = %d!\n", __FUNCTION__, ret);
  3137. return ret;
  3138. }
  3139. static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
  3140. {
  3141. struct nfs_server *server = NFS_SERVER(state->inode);
  3142. struct nfs4_exception exception = { };
  3143. int err;
  3144. do {
  3145. /* Cache the lock if possible... */
  3146. if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
  3147. return 0;
  3148. err = _nfs4_do_setlk(state, F_SETLK, request, 1);
  3149. if (err != -NFS4ERR_DELAY)
  3150. break;
  3151. nfs4_handle_exception(server, err, &exception);
  3152. } while (exception.retry);
  3153. return err;
  3154. }
  3155. static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
  3156. {
  3157. struct nfs_server *server = NFS_SERVER(state->inode);
  3158. struct nfs4_exception exception = { };
  3159. int err;
  3160. err = nfs4_set_lock_state(state, request);
  3161. if (err != 0)
  3162. return err;
  3163. do {
  3164. if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
  3165. return 0;
  3166. err = _nfs4_do_setlk(state, F_SETLK, request, 0);
  3167. if (err != -NFS4ERR_DELAY)
  3168. break;
  3169. nfs4_handle_exception(server, err, &exception);
  3170. } while (exception.retry);
  3171. return err;
  3172. }
  3173. static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  3174. {
  3175. struct nfs_client *clp = state->owner->so_client;
  3176. unsigned char fl_flags = request->fl_flags;
  3177. int status;
  3178. /* Is this a delegated open? */
  3179. status = nfs4_set_lock_state(state, request);
  3180. if (status != 0)
  3181. goto out;
  3182. request->fl_flags |= FL_ACCESS;
  3183. status = do_vfs_lock(request->fl_file, request);
  3184. if (status < 0)
  3185. goto out;
  3186. down_read(&clp->cl_sem);
  3187. if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
  3188. struct nfs_inode *nfsi = NFS_I(state->inode);
  3189. /* Yes: cache locks! */
  3190. down_read(&nfsi->rwsem);
  3191. /* ...but avoid races with delegation recall... */
  3192. if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
  3193. request->fl_flags = fl_flags & ~FL_SLEEP;
  3194. status = do_vfs_lock(request->fl_file, request);
  3195. up_read(&nfsi->rwsem);
  3196. goto out_unlock;
  3197. }
  3198. up_read(&nfsi->rwsem);
  3199. }
  3200. status = _nfs4_do_setlk(state, cmd, request, 0);
  3201. if (status != 0)
  3202. goto out_unlock;
  3203. /* Note: we always want to sleep here! */
  3204. request->fl_flags = fl_flags | FL_SLEEP;
  3205. if (do_vfs_lock(request->fl_file, request) < 0)
  3206. printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __FUNCTION__);
  3207. out_unlock:
  3208. up_read(&clp->cl_sem);
  3209. out:
  3210. request->fl_flags = fl_flags;
  3211. return status;
  3212. }
  3213. static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  3214. {
  3215. struct nfs4_exception exception = { };
  3216. int err;
  3217. do {
  3218. err = nfs4_handle_exception(NFS_SERVER(state->inode),
  3219. _nfs4_proc_setlk(state, cmd, request),
  3220. &exception);
  3221. } while (exception.retry);
  3222. return err;
  3223. }
  3224. static int
  3225. nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
  3226. {
  3227. struct nfs_open_context *ctx;
  3228. struct nfs4_state *state;
  3229. unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
  3230. int status;
  3231. /* verify open state */
  3232. ctx = nfs_file_open_context(filp);
  3233. state = ctx->state;
  3234. if (request->fl_start < 0 || request->fl_end < 0)
  3235. return -EINVAL;
  3236. if (IS_GETLK(cmd))
  3237. return nfs4_proc_getlk(state, F_GETLK, request);
  3238. if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
  3239. return -EINVAL;
  3240. if (request->fl_type == F_UNLCK)
  3241. return nfs4_proc_unlck(state, cmd, request);
  3242. do {
  3243. status = nfs4_proc_setlk(state, cmd, request);
  3244. if ((status != -EAGAIN) || IS_SETLK(cmd))
  3245. break;
  3246. timeout = nfs4_set_lock_task_retry(timeout);
  3247. status = -ERESTARTSYS;
  3248. if (signalled())
  3249. break;
  3250. } while(status < 0);
  3251. return status;
  3252. }
  3253. int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
  3254. {
  3255. struct nfs_server *server = NFS_SERVER(state->inode);
  3256. struct nfs4_exception exception = { };
  3257. int err;
  3258. err = nfs4_set_lock_state(state, fl);
  3259. if (err != 0)
  3260. goto out;
  3261. do {
  3262. err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
  3263. if (err != -NFS4ERR_DELAY)
  3264. break;
  3265. err = nfs4_handle_exception(server, err, &exception);
  3266. } while (exception.retry);
  3267. out:
  3268. return err;
  3269. }
  3270. #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
  3271. int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
  3272. size_t buflen, int flags)
  3273. {
  3274. struct inode *inode = dentry->d_inode;
  3275. if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
  3276. return -EOPNOTSUPP;
  3277. if (!S_ISREG(inode->i_mode) &&
  3278. (!S_ISDIR(inode->i_mode) || inode->i_mode & S_ISVTX))
  3279. return -EPERM;
  3280. return nfs4_proc_set_acl(inode, buf, buflen);
  3281. }
  3282. /* The getxattr man page suggests returning -ENODATA for unknown attributes,
  3283. * and that's what we'll do for e.g. user attributes that haven't been set.
  3284. * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
  3285. * attributes in kernel-managed attribute namespaces. */
  3286. ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
  3287. size_t buflen)
  3288. {
  3289. struct inode *inode = dentry->d_inode;
  3290. if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
  3291. return -EOPNOTSUPP;
  3292. return nfs4_proc_get_acl(inode, buf, buflen);
  3293. }
  3294. ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
  3295. {
  3296. size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
  3297. if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
  3298. return 0;
  3299. if (buf && buflen < len)
  3300. return -ERANGE;
  3301. if (buf)
  3302. memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
  3303. return len;
  3304. }
  3305. int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
  3306. struct nfs4_fs_locations *fs_locations, struct page *page)
  3307. {
  3308. struct nfs_server *server = NFS_SERVER(dir);
  3309. u32 bitmask[2] = {
  3310. [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
  3311. [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
  3312. };
  3313. struct nfs4_fs_locations_arg args = {
  3314. .dir_fh = NFS_FH(dir),
  3315. .name = name,
  3316. .page = page,
  3317. .bitmask = bitmask,
  3318. };
  3319. struct rpc_message msg = {
  3320. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
  3321. .rpc_argp = &args,
  3322. .rpc_resp = fs_locations,
  3323. };
  3324. int status;
  3325. dprintk("%s: start\n", __FUNCTION__);
  3326. nfs_fattr_init(&fs_locations->fattr);
  3327. fs_locations->server = server;
  3328. fs_locations->nlocations = 0;
  3329. status = rpc_call_sync(server->client, &msg, 0);
  3330. dprintk("%s: returned status = %d\n", __FUNCTION__, status);
  3331. return status;
  3332. }
  3333. struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
  3334. .recover_open = nfs4_open_reclaim,
  3335. .recover_lock = nfs4_lock_reclaim,
  3336. };
  3337. struct nfs4_state_recovery_ops nfs4_network_partition_recovery_ops = {
  3338. .recover_open = nfs4_open_expired,
  3339. .recover_lock = nfs4_lock_expired,
  3340. };
  3341. static const struct inode_operations nfs4_file_inode_operations = {
  3342. .permission = nfs_permission,
  3343. .getattr = nfs_getattr,
  3344. .setattr = nfs_setattr,
  3345. .getxattr = nfs4_getxattr,
  3346. .setxattr = nfs4_setxattr,
  3347. .listxattr = nfs4_listxattr,
  3348. };
  3349. const struct nfs_rpc_ops nfs_v4_clientops = {
  3350. .version = 4, /* protocol version */
  3351. .dentry_ops = &nfs4_dentry_operations,
  3352. .dir_inode_ops = &nfs4_dir_inode_operations,
  3353. .file_inode_ops = &nfs4_file_inode_operations,
  3354. .getroot = nfs4_proc_get_root,
  3355. .getattr = nfs4_proc_getattr,
  3356. .setattr = nfs4_proc_setattr,
  3357. .lookupfh = nfs4_proc_lookupfh,
  3358. .lookup = nfs4_proc_lookup,
  3359. .access = nfs4_proc_access,
  3360. .readlink = nfs4_proc_readlink,
  3361. .create = nfs4_proc_create,
  3362. .remove = nfs4_proc_remove,
  3363. .unlink_setup = nfs4_proc_unlink_setup,
  3364. .unlink_done = nfs4_proc_unlink_done,
  3365. .rename = nfs4_proc_rename,
  3366. .link = nfs4_proc_link,
  3367. .symlink = nfs4_proc_symlink,
  3368. .mkdir = nfs4_proc_mkdir,
  3369. .rmdir = nfs4_proc_remove,
  3370. .readdir = nfs4_proc_readdir,
  3371. .mknod = nfs4_proc_mknod,
  3372. .statfs = nfs4_proc_statfs,
  3373. .fsinfo = nfs4_proc_fsinfo,
  3374. .pathconf = nfs4_proc_pathconf,
  3375. .set_capabilities = nfs4_server_capabilities,
  3376. .decode_dirent = nfs4_decode_dirent,
  3377. .read_setup = nfs4_proc_read_setup,
  3378. .read_done = nfs4_read_done,
  3379. .write_setup = nfs4_proc_write_setup,
  3380. .write_done = nfs4_write_done,
  3381. .commit_setup = nfs4_proc_commit_setup,
  3382. .commit_done = nfs4_commit_done,
  3383. .file_open = nfs_open,
  3384. .file_release = nfs_release,
  3385. .lock = nfs4_proc_lock,
  3386. .clear_acl_cache = nfs4_zap_acl_attr,
  3387. };
  3388. /*
  3389. * Local variables:
  3390. * c-basic-offset: 8
  3391. * End:
  3392. */