nfs4proc.c 100 KB

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