nfs4proc.c 96 KB

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