nfs4proc.c 101 KB

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