nfs4proc.c 87 KB

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