nfs4proc.c 100 KB

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