nfs4proc.c 100 KB

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