nfs4proc.c 100 KB

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