nfs4proc.c 100 KB

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