nfs4proc.c 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234
  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. nfs_fattr_init(fattr);
  1392. status = rpc_call_sync(server->client, &msg, rpcflags);
  1393. dprintk("NFS reply write: %d\n", status);
  1394. return status;
  1395. }
  1396. static int nfs4_proc_write(struct nfs_write_data *wdata)
  1397. {
  1398. struct nfs4_exception exception = { };
  1399. int err;
  1400. do {
  1401. err = nfs4_handle_exception(NFS_SERVER(wdata->inode),
  1402. _nfs4_proc_write(wdata),
  1403. &exception);
  1404. } while (exception.retry);
  1405. return err;
  1406. }
  1407. static int _nfs4_proc_commit(struct nfs_write_data *cdata)
  1408. {
  1409. struct inode *inode = cdata->inode;
  1410. struct nfs_fattr *fattr = cdata->res.fattr;
  1411. struct nfs_server *server = NFS_SERVER(inode);
  1412. struct rpc_message msg = {
  1413. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
  1414. .rpc_argp = &cdata->args,
  1415. .rpc_resp = &cdata->res,
  1416. .rpc_cred = cdata->cred,
  1417. };
  1418. int status;
  1419. dprintk("NFS call commit %d @ %Ld\n", cdata->args.count,
  1420. (long long) cdata->args.offset);
  1421. nfs_fattr_init(fattr);
  1422. status = rpc_call_sync(server->client, &msg, 0);
  1423. dprintk("NFS reply commit: %d\n", status);
  1424. return status;
  1425. }
  1426. static int nfs4_proc_commit(struct nfs_write_data *cdata)
  1427. {
  1428. struct nfs4_exception exception = { };
  1429. int err;
  1430. do {
  1431. err = nfs4_handle_exception(NFS_SERVER(cdata->inode),
  1432. _nfs4_proc_commit(cdata),
  1433. &exception);
  1434. } while (exception.retry);
  1435. return err;
  1436. }
  1437. /*
  1438. * Got race?
  1439. * We will need to arrange for the VFS layer to provide an atomic open.
  1440. * Until then, this create/open method is prone to inefficiency and race
  1441. * conditions due to the lookup, create, and open VFS calls from sys_open()
  1442. * placed on the wire.
  1443. *
  1444. * Given the above sorry state of affairs, I'm simply sending an OPEN.
  1445. * The file will be opened again in the subsequent VFS open call
  1446. * (nfs4_proc_file_open).
  1447. *
  1448. * The open for read will just hang around to be used by any process that
  1449. * opens the file O_RDONLY. This will all be resolved with the VFS changes.
  1450. */
  1451. static int
  1452. nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
  1453. int flags, struct nameidata *nd)
  1454. {
  1455. struct nfs4_state *state;
  1456. struct rpc_cred *cred;
  1457. int status = 0;
  1458. cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
  1459. if (IS_ERR(cred)) {
  1460. status = PTR_ERR(cred);
  1461. goto out;
  1462. }
  1463. state = nfs4_do_open(dir, dentry, flags, sattr, cred);
  1464. put_rpccred(cred);
  1465. if (IS_ERR(state)) {
  1466. status = PTR_ERR(state);
  1467. goto out;
  1468. }
  1469. d_instantiate(dentry, state->inode);
  1470. if (flags & O_EXCL) {
  1471. struct nfs_fattr fattr;
  1472. status = nfs4_do_setattr(NFS_SERVER(dir), &fattr,
  1473. NFS_FH(state->inode), sattr, state);
  1474. if (status == 0)
  1475. nfs_setattr_update_inode(state->inode, sattr);
  1476. }
  1477. if (status == 0 && nd != NULL && (nd->flags & LOOKUP_OPEN))
  1478. nfs4_intent_set_file(nd, dentry, state);
  1479. else
  1480. nfs4_close_state(state, flags);
  1481. out:
  1482. return status;
  1483. }
  1484. static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
  1485. {
  1486. struct nfs_server *server = NFS_SERVER(dir);
  1487. struct nfs4_remove_arg args = {
  1488. .fh = NFS_FH(dir),
  1489. .name = name,
  1490. .bitmask = server->attr_bitmask,
  1491. };
  1492. struct nfs_fattr dir_attr;
  1493. struct nfs4_remove_res res = {
  1494. .server = server,
  1495. .dir_attr = &dir_attr,
  1496. };
  1497. struct rpc_message msg = {
  1498. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
  1499. .rpc_argp = &args,
  1500. .rpc_resp = &res,
  1501. };
  1502. int status;
  1503. nfs_fattr_init(res.dir_attr);
  1504. status = rpc_call_sync(server->client, &msg, 0);
  1505. if (status == 0) {
  1506. update_changeattr(dir, &res.cinfo);
  1507. nfs_post_op_update_inode(dir, res.dir_attr);
  1508. }
  1509. return status;
  1510. }
  1511. static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
  1512. {
  1513. struct nfs4_exception exception = { };
  1514. int err;
  1515. do {
  1516. err = nfs4_handle_exception(NFS_SERVER(dir),
  1517. _nfs4_proc_remove(dir, name),
  1518. &exception);
  1519. } while (exception.retry);
  1520. return err;
  1521. }
  1522. struct unlink_desc {
  1523. struct nfs4_remove_arg args;
  1524. struct nfs4_remove_res res;
  1525. struct nfs_fattr dir_attr;
  1526. };
  1527. static int nfs4_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir,
  1528. struct qstr *name)
  1529. {
  1530. struct nfs_server *server = NFS_SERVER(dir->d_inode);
  1531. struct unlink_desc *up;
  1532. up = (struct unlink_desc *) kmalloc(sizeof(*up), GFP_KERNEL);
  1533. if (!up)
  1534. return -ENOMEM;
  1535. up->args.fh = NFS_FH(dir->d_inode);
  1536. up->args.name = name;
  1537. up->args.bitmask = server->attr_bitmask;
  1538. up->res.server = server;
  1539. up->res.dir_attr = &up->dir_attr;
  1540. msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
  1541. msg->rpc_argp = &up->args;
  1542. msg->rpc_resp = &up->res;
  1543. return 0;
  1544. }
  1545. static int nfs4_proc_unlink_done(struct dentry *dir, struct rpc_task *task)
  1546. {
  1547. struct rpc_message *msg = &task->tk_msg;
  1548. struct unlink_desc *up;
  1549. if (msg->rpc_resp != NULL) {
  1550. up = container_of(msg->rpc_resp, struct unlink_desc, res);
  1551. update_changeattr(dir->d_inode, &up->res.cinfo);
  1552. nfs_post_op_update_inode(dir->d_inode, up->res.dir_attr);
  1553. kfree(up);
  1554. msg->rpc_resp = NULL;
  1555. msg->rpc_argp = NULL;
  1556. }
  1557. return 0;
  1558. }
  1559. static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
  1560. struct inode *new_dir, struct qstr *new_name)
  1561. {
  1562. struct nfs_server *server = NFS_SERVER(old_dir);
  1563. struct nfs4_rename_arg arg = {
  1564. .old_dir = NFS_FH(old_dir),
  1565. .new_dir = NFS_FH(new_dir),
  1566. .old_name = old_name,
  1567. .new_name = new_name,
  1568. .bitmask = server->attr_bitmask,
  1569. };
  1570. struct nfs_fattr old_fattr, new_fattr;
  1571. struct nfs4_rename_res res = {
  1572. .server = server,
  1573. .old_fattr = &old_fattr,
  1574. .new_fattr = &new_fattr,
  1575. };
  1576. struct rpc_message msg = {
  1577. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
  1578. .rpc_argp = &arg,
  1579. .rpc_resp = &res,
  1580. };
  1581. int status;
  1582. nfs_fattr_init(res.old_fattr);
  1583. nfs_fattr_init(res.new_fattr);
  1584. status = rpc_call_sync(server->client, &msg, 0);
  1585. if (!status) {
  1586. update_changeattr(old_dir, &res.old_cinfo);
  1587. nfs_post_op_update_inode(old_dir, res.old_fattr);
  1588. update_changeattr(new_dir, &res.new_cinfo);
  1589. nfs_post_op_update_inode(new_dir, res.new_fattr);
  1590. }
  1591. return status;
  1592. }
  1593. static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
  1594. struct inode *new_dir, struct qstr *new_name)
  1595. {
  1596. struct nfs4_exception exception = { };
  1597. int err;
  1598. do {
  1599. err = nfs4_handle_exception(NFS_SERVER(old_dir),
  1600. _nfs4_proc_rename(old_dir, old_name,
  1601. new_dir, new_name),
  1602. &exception);
  1603. } while (exception.retry);
  1604. return err;
  1605. }
  1606. static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
  1607. {
  1608. struct nfs_server *server = NFS_SERVER(inode);
  1609. struct nfs4_link_arg arg = {
  1610. .fh = NFS_FH(inode),
  1611. .dir_fh = NFS_FH(dir),
  1612. .name = name,
  1613. .bitmask = server->attr_bitmask,
  1614. };
  1615. struct nfs_fattr fattr, dir_attr;
  1616. struct nfs4_link_res res = {
  1617. .server = server,
  1618. .fattr = &fattr,
  1619. .dir_attr = &dir_attr,
  1620. };
  1621. struct rpc_message msg = {
  1622. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
  1623. .rpc_argp = &arg,
  1624. .rpc_resp = &res,
  1625. };
  1626. int status;
  1627. nfs_fattr_init(res.fattr);
  1628. nfs_fattr_init(res.dir_attr);
  1629. status = rpc_call_sync(server->client, &msg, 0);
  1630. if (!status) {
  1631. update_changeattr(dir, &res.cinfo);
  1632. nfs_post_op_update_inode(dir, res.dir_attr);
  1633. nfs_refresh_inode(inode, res.fattr);
  1634. }
  1635. return status;
  1636. }
  1637. static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
  1638. {
  1639. struct nfs4_exception exception = { };
  1640. int err;
  1641. do {
  1642. err = nfs4_handle_exception(NFS_SERVER(inode),
  1643. _nfs4_proc_link(inode, dir, name),
  1644. &exception);
  1645. } while (exception.retry);
  1646. return err;
  1647. }
  1648. static int _nfs4_proc_symlink(struct inode *dir, struct qstr *name,
  1649. struct qstr *path, struct iattr *sattr, struct nfs_fh *fhandle,
  1650. struct nfs_fattr *fattr)
  1651. {
  1652. struct nfs_server *server = NFS_SERVER(dir);
  1653. struct nfs_fattr dir_fattr;
  1654. struct nfs4_create_arg arg = {
  1655. .dir_fh = NFS_FH(dir),
  1656. .server = server,
  1657. .name = name,
  1658. .attrs = sattr,
  1659. .ftype = NF4LNK,
  1660. .bitmask = server->attr_bitmask,
  1661. };
  1662. struct nfs4_create_res res = {
  1663. .server = server,
  1664. .fh = fhandle,
  1665. .fattr = fattr,
  1666. .dir_fattr = &dir_fattr,
  1667. };
  1668. struct rpc_message msg = {
  1669. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK],
  1670. .rpc_argp = &arg,
  1671. .rpc_resp = &res,
  1672. };
  1673. int status;
  1674. if (path->len > NFS4_MAXPATHLEN)
  1675. return -ENAMETOOLONG;
  1676. arg.u.symlink = path;
  1677. nfs_fattr_init(fattr);
  1678. nfs_fattr_init(&dir_fattr);
  1679. status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
  1680. if (!status)
  1681. update_changeattr(dir, &res.dir_cinfo);
  1682. nfs_post_op_update_inode(dir, res.dir_fattr);
  1683. return status;
  1684. }
  1685. static int nfs4_proc_symlink(struct inode *dir, struct qstr *name,
  1686. struct qstr *path, struct iattr *sattr, struct nfs_fh *fhandle,
  1687. struct nfs_fattr *fattr)
  1688. {
  1689. struct nfs4_exception exception = { };
  1690. int err;
  1691. do {
  1692. err = nfs4_handle_exception(NFS_SERVER(dir),
  1693. _nfs4_proc_symlink(dir, name, path, sattr,
  1694. fhandle, fattr),
  1695. &exception);
  1696. } while (exception.retry);
  1697. return err;
  1698. }
  1699. static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
  1700. struct iattr *sattr)
  1701. {
  1702. struct nfs_server *server = NFS_SERVER(dir);
  1703. struct nfs_fh fhandle;
  1704. struct nfs_fattr fattr, dir_fattr;
  1705. struct nfs4_create_arg arg = {
  1706. .dir_fh = NFS_FH(dir),
  1707. .server = server,
  1708. .name = &dentry->d_name,
  1709. .attrs = sattr,
  1710. .ftype = NF4DIR,
  1711. .bitmask = server->attr_bitmask,
  1712. };
  1713. struct nfs4_create_res res = {
  1714. .server = server,
  1715. .fh = &fhandle,
  1716. .fattr = &fattr,
  1717. .dir_fattr = &dir_fattr,
  1718. };
  1719. struct rpc_message msg = {
  1720. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
  1721. .rpc_argp = &arg,
  1722. .rpc_resp = &res,
  1723. };
  1724. int status;
  1725. nfs_fattr_init(&fattr);
  1726. nfs_fattr_init(&dir_fattr);
  1727. status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
  1728. if (!status) {
  1729. update_changeattr(dir, &res.dir_cinfo);
  1730. nfs_post_op_update_inode(dir, res.dir_fattr);
  1731. status = nfs_instantiate(dentry, &fhandle, &fattr);
  1732. }
  1733. return status;
  1734. }
  1735. static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
  1736. struct iattr *sattr)
  1737. {
  1738. struct nfs4_exception exception = { };
  1739. int err;
  1740. do {
  1741. err = nfs4_handle_exception(NFS_SERVER(dir),
  1742. _nfs4_proc_mkdir(dir, dentry, sattr),
  1743. &exception);
  1744. } while (exception.retry);
  1745. return err;
  1746. }
  1747. static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
  1748. u64 cookie, struct page *page, unsigned int count, int plus)
  1749. {
  1750. struct inode *dir = dentry->d_inode;
  1751. struct nfs4_readdir_arg args = {
  1752. .fh = NFS_FH(dir),
  1753. .pages = &page,
  1754. .pgbase = 0,
  1755. .count = count,
  1756. .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
  1757. };
  1758. struct nfs4_readdir_res res;
  1759. struct rpc_message msg = {
  1760. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
  1761. .rpc_argp = &args,
  1762. .rpc_resp = &res,
  1763. .rpc_cred = cred,
  1764. };
  1765. int status;
  1766. dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __FUNCTION__,
  1767. dentry->d_parent->d_name.name,
  1768. dentry->d_name.name,
  1769. (unsigned long long)cookie);
  1770. lock_kernel();
  1771. nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
  1772. res.pgbase = args.pgbase;
  1773. status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
  1774. if (status == 0)
  1775. memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
  1776. unlock_kernel();
  1777. dprintk("%s: returns %d\n", __FUNCTION__, status);
  1778. return status;
  1779. }
  1780. static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
  1781. u64 cookie, struct page *page, unsigned int count, int plus)
  1782. {
  1783. struct nfs4_exception exception = { };
  1784. int err;
  1785. do {
  1786. err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
  1787. _nfs4_proc_readdir(dentry, cred, cookie,
  1788. page, count, plus),
  1789. &exception);
  1790. } while (exception.retry);
  1791. return err;
  1792. }
  1793. static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
  1794. struct iattr *sattr, dev_t rdev)
  1795. {
  1796. struct nfs_server *server = NFS_SERVER(dir);
  1797. struct nfs_fh fh;
  1798. struct nfs_fattr fattr, dir_fattr;
  1799. struct nfs4_create_arg arg = {
  1800. .dir_fh = NFS_FH(dir),
  1801. .server = server,
  1802. .name = &dentry->d_name,
  1803. .attrs = sattr,
  1804. .bitmask = server->attr_bitmask,
  1805. };
  1806. struct nfs4_create_res res = {
  1807. .server = server,
  1808. .fh = &fh,
  1809. .fattr = &fattr,
  1810. .dir_fattr = &dir_fattr,
  1811. };
  1812. struct rpc_message msg = {
  1813. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
  1814. .rpc_argp = &arg,
  1815. .rpc_resp = &res,
  1816. };
  1817. int status;
  1818. int mode = sattr->ia_mode;
  1819. nfs_fattr_init(&fattr);
  1820. nfs_fattr_init(&dir_fattr);
  1821. BUG_ON(!(sattr->ia_valid & ATTR_MODE));
  1822. BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
  1823. if (S_ISFIFO(mode))
  1824. arg.ftype = NF4FIFO;
  1825. else if (S_ISBLK(mode)) {
  1826. arg.ftype = NF4BLK;
  1827. arg.u.device.specdata1 = MAJOR(rdev);
  1828. arg.u.device.specdata2 = MINOR(rdev);
  1829. }
  1830. else if (S_ISCHR(mode)) {
  1831. arg.ftype = NF4CHR;
  1832. arg.u.device.specdata1 = MAJOR(rdev);
  1833. arg.u.device.specdata2 = MINOR(rdev);
  1834. }
  1835. else
  1836. arg.ftype = NF4SOCK;
  1837. status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
  1838. if (status == 0) {
  1839. update_changeattr(dir, &res.dir_cinfo);
  1840. nfs_post_op_update_inode(dir, res.dir_fattr);
  1841. status = nfs_instantiate(dentry, &fh, &fattr);
  1842. }
  1843. return status;
  1844. }
  1845. static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
  1846. struct iattr *sattr, dev_t rdev)
  1847. {
  1848. struct nfs4_exception exception = { };
  1849. int err;
  1850. do {
  1851. err = nfs4_handle_exception(NFS_SERVER(dir),
  1852. _nfs4_proc_mknod(dir, dentry, sattr, rdev),
  1853. &exception);
  1854. } while (exception.retry);
  1855. return err;
  1856. }
  1857. static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
  1858. struct nfs_fsstat *fsstat)
  1859. {
  1860. struct nfs4_statfs_arg args = {
  1861. .fh = fhandle,
  1862. .bitmask = server->attr_bitmask,
  1863. };
  1864. struct rpc_message msg = {
  1865. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
  1866. .rpc_argp = &args,
  1867. .rpc_resp = fsstat,
  1868. };
  1869. nfs_fattr_init(fsstat->fattr);
  1870. return rpc_call_sync(server->client, &msg, 0);
  1871. }
  1872. static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
  1873. {
  1874. struct nfs4_exception exception = { };
  1875. int err;
  1876. do {
  1877. err = nfs4_handle_exception(server,
  1878. _nfs4_proc_statfs(server, fhandle, fsstat),
  1879. &exception);
  1880. } while (exception.retry);
  1881. return err;
  1882. }
  1883. static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
  1884. struct nfs_fsinfo *fsinfo)
  1885. {
  1886. struct nfs4_fsinfo_arg args = {
  1887. .fh = fhandle,
  1888. .bitmask = server->attr_bitmask,
  1889. };
  1890. struct rpc_message msg = {
  1891. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
  1892. .rpc_argp = &args,
  1893. .rpc_resp = fsinfo,
  1894. };
  1895. return rpc_call_sync(server->client, &msg, 0);
  1896. }
  1897. static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
  1898. {
  1899. struct nfs4_exception exception = { };
  1900. int err;
  1901. do {
  1902. err = nfs4_handle_exception(server,
  1903. _nfs4_do_fsinfo(server, fhandle, fsinfo),
  1904. &exception);
  1905. } while (exception.retry);
  1906. return err;
  1907. }
  1908. static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
  1909. {
  1910. nfs_fattr_init(fsinfo->fattr);
  1911. return nfs4_do_fsinfo(server, fhandle, fsinfo);
  1912. }
  1913. static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
  1914. struct nfs_pathconf *pathconf)
  1915. {
  1916. struct nfs4_pathconf_arg args = {
  1917. .fh = fhandle,
  1918. .bitmask = server->attr_bitmask,
  1919. };
  1920. struct rpc_message msg = {
  1921. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
  1922. .rpc_argp = &args,
  1923. .rpc_resp = pathconf,
  1924. };
  1925. /* None of the pathconf attributes are mandatory to implement */
  1926. if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
  1927. memset(pathconf, 0, sizeof(*pathconf));
  1928. return 0;
  1929. }
  1930. nfs_fattr_init(pathconf->fattr);
  1931. return rpc_call_sync(server->client, &msg, 0);
  1932. }
  1933. static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
  1934. struct nfs_pathconf *pathconf)
  1935. {
  1936. struct nfs4_exception exception = { };
  1937. int err;
  1938. do {
  1939. err = nfs4_handle_exception(server,
  1940. _nfs4_proc_pathconf(server, fhandle, pathconf),
  1941. &exception);
  1942. } while (exception.retry);
  1943. return err;
  1944. }
  1945. static void
  1946. nfs4_read_done(struct rpc_task *task)
  1947. {
  1948. struct nfs_read_data *data = (struct nfs_read_data *) task->tk_calldata;
  1949. struct inode *inode = data->inode;
  1950. if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
  1951. rpc_restart_call(task);
  1952. return;
  1953. }
  1954. if (task->tk_status > 0)
  1955. renew_lease(NFS_SERVER(inode), data->timestamp);
  1956. /* Call back common NFS readpage processing */
  1957. nfs_readpage_result(task);
  1958. }
  1959. static void
  1960. nfs4_proc_read_setup(struct nfs_read_data *data)
  1961. {
  1962. struct rpc_task *task = &data->task;
  1963. struct rpc_message msg = {
  1964. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
  1965. .rpc_argp = &data->args,
  1966. .rpc_resp = &data->res,
  1967. .rpc_cred = data->cred,
  1968. };
  1969. struct inode *inode = data->inode;
  1970. int flags;
  1971. data->timestamp = jiffies;
  1972. /* N.B. Do we need to test? Never called for swapfile inode */
  1973. flags = RPC_TASK_ASYNC | (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0);
  1974. /* Finalize the task. */
  1975. rpc_init_task(task, NFS_CLIENT(inode), nfs4_read_done, flags);
  1976. rpc_call_setup(task, &msg, 0);
  1977. }
  1978. static void
  1979. nfs4_write_done(struct rpc_task *task)
  1980. {
  1981. struct nfs_write_data *data = (struct nfs_write_data *) task->tk_calldata;
  1982. struct inode *inode = data->inode;
  1983. if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
  1984. rpc_restart_call(task);
  1985. return;
  1986. }
  1987. if (task->tk_status >= 0) {
  1988. renew_lease(NFS_SERVER(inode), data->timestamp);
  1989. nfs_post_op_update_inode(inode, data->res.fattr);
  1990. }
  1991. /* Call back common NFS writeback processing */
  1992. nfs_writeback_done(task);
  1993. }
  1994. static void
  1995. nfs4_proc_write_setup(struct nfs_write_data *data, int how)
  1996. {
  1997. struct rpc_task *task = &data->task;
  1998. struct rpc_message msg = {
  1999. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
  2000. .rpc_argp = &data->args,
  2001. .rpc_resp = &data->res,
  2002. .rpc_cred = data->cred,
  2003. };
  2004. struct inode *inode = data->inode;
  2005. struct nfs_server *server = NFS_SERVER(inode);
  2006. int stable;
  2007. int flags;
  2008. if (how & FLUSH_STABLE) {
  2009. if (!NFS_I(inode)->ncommit)
  2010. stable = NFS_FILE_SYNC;
  2011. else
  2012. stable = NFS_DATA_SYNC;
  2013. } else
  2014. stable = NFS_UNSTABLE;
  2015. data->args.stable = stable;
  2016. data->args.bitmask = server->attr_bitmask;
  2017. data->res.server = server;
  2018. data->timestamp = jiffies;
  2019. /* Set the initial flags for the task. */
  2020. flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
  2021. /* Finalize the task. */
  2022. rpc_init_task(task, NFS_CLIENT(inode), nfs4_write_done, flags);
  2023. rpc_call_setup(task, &msg, 0);
  2024. }
  2025. static void
  2026. nfs4_commit_done(struct rpc_task *task)
  2027. {
  2028. struct nfs_write_data *data = (struct nfs_write_data *) task->tk_calldata;
  2029. struct inode *inode = data->inode;
  2030. if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
  2031. rpc_restart_call(task);
  2032. return;
  2033. }
  2034. if (task->tk_status >= 0)
  2035. nfs_post_op_update_inode(inode, data->res.fattr);
  2036. /* Call back common NFS writeback processing */
  2037. nfs_commit_done(task);
  2038. }
  2039. static void
  2040. nfs4_proc_commit_setup(struct nfs_write_data *data, int how)
  2041. {
  2042. struct rpc_task *task = &data->task;
  2043. struct rpc_message msg = {
  2044. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
  2045. .rpc_argp = &data->args,
  2046. .rpc_resp = &data->res,
  2047. .rpc_cred = data->cred,
  2048. };
  2049. struct inode *inode = data->inode;
  2050. struct nfs_server *server = NFS_SERVER(inode);
  2051. int flags;
  2052. data->args.bitmask = server->attr_bitmask;
  2053. data->res.server = server;
  2054. /* Set the initial flags for the task. */
  2055. flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
  2056. /* Finalize the task. */
  2057. rpc_init_task(task, NFS_CLIENT(inode), nfs4_commit_done, flags);
  2058. rpc_call_setup(task, &msg, 0);
  2059. }
  2060. /*
  2061. * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
  2062. * standalone procedure for queueing an asynchronous RENEW.
  2063. */
  2064. static void
  2065. renew_done(struct rpc_task *task)
  2066. {
  2067. struct nfs4_client *clp = (struct nfs4_client *)task->tk_msg.rpc_argp;
  2068. unsigned long timestamp = (unsigned long)task->tk_calldata;
  2069. if (task->tk_status < 0) {
  2070. switch (task->tk_status) {
  2071. case -NFS4ERR_STALE_CLIENTID:
  2072. case -NFS4ERR_EXPIRED:
  2073. case -NFS4ERR_CB_PATH_DOWN:
  2074. nfs4_schedule_state_recovery(clp);
  2075. }
  2076. return;
  2077. }
  2078. spin_lock(&clp->cl_lock);
  2079. if (time_before(clp->cl_last_renewal,timestamp))
  2080. clp->cl_last_renewal = timestamp;
  2081. spin_unlock(&clp->cl_lock);
  2082. }
  2083. int
  2084. nfs4_proc_async_renew(struct nfs4_client *clp)
  2085. {
  2086. struct rpc_message msg = {
  2087. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
  2088. .rpc_argp = clp,
  2089. .rpc_cred = clp->cl_cred,
  2090. };
  2091. return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
  2092. renew_done, (void *)jiffies);
  2093. }
  2094. int
  2095. nfs4_proc_renew(struct nfs4_client *clp)
  2096. {
  2097. struct rpc_message msg = {
  2098. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
  2099. .rpc_argp = clp,
  2100. .rpc_cred = clp->cl_cred,
  2101. };
  2102. unsigned long now = jiffies;
  2103. int status;
  2104. status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
  2105. if (status < 0)
  2106. return status;
  2107. spin_lock(&clp->cl_lock);
  2108. if (time_before(clp->cl_last_renewal,now))
  2109. clp->cl_last_renewal = now;
  2110. spin_unlock(&clp->cl_lock);
  2111. return 0;
  2112. }
  2113. static inline int nfs4_server_supports_acls(struct nfs_server *server)
  2114. {
  2115. return (server->caps & NFS_CAP_ACLS)
  2116. && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
  2117. && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
  2118. }
  2119. /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
  2120. * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
  2121. * the stack.
  2122. */
  2123. #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
  2124. static void buf_to_pages(const void *buf, size_t buflen,
  2125. struct page **pages, unsigned int *pgbase)
  2126. {
  2127. const void *p = buf;
  2128. *pgbase = offset_in_page(buf);
  2129. p -= *pgbase;
  2130. while (p < buf + buflen) {
  2131. *(pages++) = virt_to_page(p);
  2132. p += PAGE_CACHE_SIZE;
  2133. }
  2134. }
  2135. struct nfs4_cached_acl {
  2136. int cached;
  2137. size_t len;
  2138. char data[0];
  2139. };
  2140. static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
  2141. {
  2142. struct nfs_inode *nfsi = NFS_I(inode);
  2143. spin_lock(&inode->i_lock);
  2144. kfree(nfsi->nfs4_acl);
  2145. nfsi->nfs4_acl = acl;
  2146. spin_unlock(&inode->i_lock);
  2147. }
  2148. static void nfs4_zap_acl_attr(struct inode *inode)
  2149. {
  2150. nfs4_set_cached_acl(inode, NULL);
  2151. }
  2152. static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
  2153. {
  2154. struct nfs_inode *nfsi = NFS_I(inode);
  2155. struct nfs4_cached_acl *acl;
  2156. int ret = -ENOENT;
  2157. spin_lock(&inode->i_lock);
  2158. acl = nfsi->nfs4_acl;
  2159. if (acl == NULL)
  2160. goto out;
  2161. if (buf == NULL) /* user is just asking for length */
  2162. goto out_len;
  2163. if (acl->cached == 0)
  2164. goto out;
  2165. ret = -ERANGE; /* see getxattr(2) man page */
  2166. if (acl->len > buflen)
  2167. goto out;
  2168. memcpy(buf, acl->data, acl->len);
  2169. out_len:
  2170. ret = acl->len;
  2171. out:
  2172. spin_unlock(&inode->i_lock);
  2173. return ret;
  2174. }
  2175. static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
  2176. {
  2177. struct nfs4_cached_acl *acl;
  2178. if (buf && acl_len <= PAGE_SIZE) {
  2179. acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
  2180. if (acl == NULL)
  2181. goto out;
  2182. acl->cached = 1;
  2183. memcpy(acl->data, buf, acl_len);
  2184. } else {
  2185. acl = kmalloc(sizeof(*acl), GFP_KERNEL);
  2186. if (acl == NULL)
  2187. goto out;
  2188. acl->cached = 0;
  2189. }
  2190. acl->len = acl_len;
  2191. out:
  2192. nfs4_set_cached_acl(inode, acl);
  2193. }
  2194. static inline ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
  2195. {
  2196. struct page *pages[NFS4ACL_MAXPAGES];
  2197. struct nfs_getaclargs args = {
  2198. .fh = NFS_FH(inode),
  2199. .acl_pages = pages,
  2200. .acl_len = buflen,
  2201. };
  2202. size_t resp_len = buflen;
  2203. void *resp_buf;
  2204. struct rpc_message msg = {
  2205. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
  2206. .rpc_argp = &args,
  2207. .rpc_resp = &resp_len,
  2208. };
  2209. struct page *localpage = NULL;
  2210. int ret;
  2211. if (buflen < PAGE_SIZE) {
  2212. /* As long as we're doing a round trip to the server anyway,
  2213. * let's be prepared for a page of acl data. */
  2214. localpage = alloc_page(GFP_KERNEL);
  2215. resp_buf = page_address(localpage);
  2216. if (localpage == NULL)
  2217. return -ENOMEM;
  2218. args.acl_pages[0] = localpage;
  2219. args.acl_pgbase = 0;
  2220. resp_len = args.acl_len = PAGE_SIZE;
  2221. } else {
  2222. resp_buf = buf;
  2223. buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
  2224. }
  2225. ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
  2226. if (ret)
  2227. goto out_free;
  2228. if (resp_len > args.acl_len)
  2229. nfs4_write_cached_acl(inode, NULL, resp_len);
  2230. else
  2231. nfs4_write_cached_acl(inode, resp_buf, resp_len);
  2232. if (buf) {
  2233. ret = -ERANGE;
  2234. if (resp_len > buflen)
  2235. goto out_free;
  2236. if (localpage)
  2237. memcpy(buf, resp_buf, resp_len);
  2238. }
  2239. ret = resp_len;
  2240. out_free:
  2241. if (localpage)
  2242. __free_page(localpage);
  2243. return ret;
  2244. }
  2245. static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
  2246. {
  2247. struct nfs_server *server = NFS_SERVER(inode);
  2248. int ret;
  2249. if (!nfs4_server_supports_acls(server))
  2250. return -EOPNOTSUPP;
  2251. ret = nfs_revalidate_inode(server, inode);
  2252. if (ret < 0)
  2253. return ret;
  2254. ret = nfs4_read_cached_acl(inode, buf, buflen);
  2255. if (ret != -ENOENT)
  2256. return ret;
  2257. return nfs4_get_acl_uncached(inode, buf, buflen);
  2258. }
  2259. static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
  2260. {
  2261. struct nfs_server *server = NFS_SERVER(inode);
  2262. struct page *pages[NFS4ACL_MAXPAGES];
  2263. struct nfs_setaclargs arg = {
  2264. .fh = NFS_FH(inode),
  2265. .acl_pages = pages,
  2266. .acl_len = buflen,
  2267. };
  2268. struct rpc_message msg = {
  2269. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
  2270. .rpc_argp = &arg,
  2271. .rpc_resp = NULL,
  2272. };
  2273. int ret;
  2274. if (!nfs4_server_supports_acls(server))
  2275. return -EOPNOTSUPP;
  2276. nfs_inode_return_delegation(inode);
  2277. buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
  2278. ret = rpc_call_sync(NFS_SERVER(inode)->client, &msg, 0);
  2279. if (ret == 0)
  2280. nfs4_write_cached_acl(inode, buf, buflen);
  2281. return ret;
  2282. }
  2283. static int
  2284. nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server)
  2285. {
  2286. struct nfs4_client *clp = server->nfs4_state;
  2287. if (!clp || task->tk_status >= 0)
  2288. return 0;
  2289. switch(task->tk_status) {
  2290. case -NFS4ERR_STALE_CLIENTID:
  2291. case -NFS4ERR_STALE_STATEID:
  2292. case -NFS4ERR_EXPIRED:
  2293. rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL, NULL);
  2294. nfs4_schedule_state_recovery(clp);
  2295. if (test_bit(NFS4CLNT_OK, &clp->cl_state))
  2296. rpc_wake_up_task(task);
  2297. task->tk_status = 0;
  2298. return -EAGAIN;
  2299. case -NFS4ERR_GRACE:
  2300. case -NFS4ERR_DELAY:
  2301. rpc_delay(task, NFS4_POLL_RETRY_MAX);
  2302. task->tk_status = 0;
  2303. return -EAGAIN;
  2304. case -NFS4ERR_OLD_STATEID:
  2305. task->tk_status = 0;
  2306. return -EAGAIN;
  2307. }
  2308. task->tk_status = nfs4_map_errors(task->tk_status);
  2309. return 0;
  2310. }
  2311. static int nfs4_wait_clnt_recover(struct rpc_clnt *clnt, struct nfs4_client *clp)
  2312. {
  2313. DEFINE_WAIT(wait);
  2314. sigset_t oldset;
  2315. int interruptible, res = 0;
  2316. might_sleep();
  2317. rpc_clnt_sigmask(clnt, &oldset);
  2318. interruptible = TASK_UNINTERRUPTIBLE;
  2319. if (clnt->cl_intr)
  2320. interruptible = TASK_INTERRUPTIBLE;
  2321. prepare_to_wait(&clp->cl_waitq, &wait, interruptible);
  2322. nfs4_schedule_state_recovery(clp);
  2323. if (clnt->cl_intr && signalled())
  2324. res = -ERESTARTSYS;
  2325. else if (!test_bit(NFS4CLNT_OK, &clp->cl_state))
  2326. schedule();
  2327. finish_wait(&clp->cl_waitq, &wait);
  2328. rpc_clnt_sigunmask(clnt, &oldset);
  2329. return res;
  2330. }
  2331. static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
  2332. {
  2333. sigset_t oldset;
  2334. int res = 0;
  2335. might_sleep();
  2336. if (*timeout <= 0)
  2337. *timeout = NFS4_POLL_RETRY_MIN;
  2338. if (*timeout > NFS4_POLL_RETRY_MAX)
  2339. *timeout = NFS4_POLL_RETRY_MAX;
  2340. rpc_clnt_sigmask(clnt, &oldset);
  2341. if (clnt->cl_intr) {
  2342. schedule_timeout_interruptible(*timeout);
  2343. if (signalled())
  2344. res = -ERESTARTSYS;
  2345. } else
  2346. schedule_timeout_uninterruptible(*timeout);
  2347. rpc_clnt_sigunmask(clnt, &oldset);
  2348. *timeout <<= 1;
  2349. return res;
  2350. }
  2351. /* This is the error handling routine for processes that are allowed
  2352. * to sleep.
  2353. */
  2354. int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
  2355. {
  2356. struct nfs4_client *clp = server->nfs4_state;
  2357. int ret = errorcode;
  2358. exception->retry = 0;
  2359. switch(errorcode) {
  2360. case 0:
  2361. return 0;
  2362. case -NFS4ERR_STALE_CLIENTID:
  2363. case -NFS4ERR_STALE_STATEID:
  2364. case -NFS4ERR_EXPIRED:
  2365. ret = nfs4_wait_clnt_recover(server->client, clp);
  2366. if (ret == 0)
  2367. exception->retry = 1;
  2368. break;
  2369. case -NFS4ERR_GRACE:
  2370. case -NFS4ERR_DELAY:
  2371. ret = nfs4_delay(server->client, &exception->timeout);
  2372. if (ret == 0)
  2373. exception->retry = 1;
  2374. break;
  2375. case -NFS4ERR_OLD_STATEID:
  2376. if (ret == 0)
  2377. exception->retry = 1;
  2378. }
  2379. /* We failed to handle the error */
  2380. return nfs4_map_errors(ret);
  2381. }
  2382. int nfs4_proc_setclientid(struct nfs4_client *clp, u32 program, unsigned short port)
  2383. {
  2384. nfs4_verifier sc_verifier;
  2385. struct nfs4_setclientid setclientid = {
  2386. .sc_verifier = &sc_verifier,
  2387. .sc_prog = program,
  2388. };
  2389. struct rpc_message msg = {
  2390. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
  2391. .rpc_argp = &setclientid,
  2392. .rpc_resp = clp,
  2393. .rpc_cred = clp->cl_cred,
  2394. };
  2395. u32 *p;
  2396. int loop = 0;
  2397. int status;
  2398. p = (u32*)sc_verifier.data;
  2399. *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
  2400. *p = htonl((u32)clp->cl_boot_time.tv_nsec);
  2401. for(;;) {
  2402. setclientid.sc_name_len = scnprintf(setclientid.sc_name,
  2403. sizeof(setclientid.sc_name), "%s/%u.%u.%u.%u %s %u",
  2404. clp->cl_ipaddr, NIPQUAD(clp->cl_addr.s_addr),
  2405. clp->cl_cred->cr_ops->cr_name,
  2406. clp->cl_id_uniquifier);
  2407. setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
  2408. sizeof(setclientid.sc_netid), "tcp");
  2409. setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
  2410. sizeof(setclientid.sc_uaddr), "%s.%d.%d",
  2411. clp->cl_ipaddr, port >> 8, port & 255);
  2412. status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
  2413. if (status != -NFS4ERR_CLID_INUSE)
  2414. break;
  2415. if (signalled())
  2416. break;
  2417. if (loop++ & 1)
  2418. ssleep(clp->cl_lease_time + 1);
  2419. else
  2420. if (++clp->cl_id_uniquifier == 0)
  2421. break;
  2422. }
  2423. return status;
  2424. }
  2425. int
  2426. nfs4_proc_setclientid_confirm(struct nfs4_client *clp)
  2427. {
  2428. struct nfs_fsinfo fsinfo;
  2429. struct rpc_message msg = {
  2430. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
  2431. .rpc_argp = clp,
  2432. .rpc_resp = &fsinfo,
  2433. .rpc_cred = clp->cl_cred,
  2434. };
  2435. unsigned long now;
  2436. int status;
  2437. now = jiffies;
  2438. status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
  2439. if (status == 0) {
  2440. spin_lock(&clp->cl_lock);
  2441. clp->cl_lease_time = fsinfo.lease_time * HZ;
  2442. clp->cl_last_renewal = now;
  2443. spin_unlock(&clp->cl_lock);
  2444. }
  2445. return status;
  2446. }
  2447. static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
  2448. {
  2449. struct nfs4_delegreturnargs args = {
  2450. .fhandle = NFS_FH(inode),
  2451. .stateid = stateid,
  2452. };
  2453. struct rpc_message msg = {
  2454. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
  2455. .rpc_argp = &args,
  2456. .rpc_cred = cred,
  2457. };
  2458. return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
  2459. }
  2460. int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
  2461. {
  2462. struct nfs_server *server = NFS_SERVER(inode);
  2463. struct nfs4_exception exception = { };
  2464. int err;
  2465. do {
  2466. err = _nfs4_proc_delegreturn(inode, cred, stateid);
  2467. switch (err) {
  2468. case -NFS4ERR_STALE_STATEID:
  2469. case -NFS4ERR_EXPIRED:
  2470. nfs4_schedule_state_recovery(server->nfs4_state);
  2471. case 0:
  2472. return 0;
  2473. }
  2474. err = nfs4_handle_exception(server, err, &exception);
  2475. } while (exception.retry);
  2476. return err;
  2477. }
  2478. #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
  2479. #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
  2480. /*
  2481. * sleep, with exponential backoff, and retry the LOCK operation.
  2482. */
  2483. static unsigned long
  2484. nfs4_set_lock_task_retry(unsigned long timeout)
  2485. {
  2486. schedule_timeout_interruptible(timeout);
  2487. timeout <<= 1;
  2488. if (timeout > NFS4_LOCK_MAXTIMEOUT)
  2489. return NFS4_LOCK_MAXTIMEOUT;
  2490. return timeout;
  2491. }
  2492. static inline int
  2493. nfs4_lck_type(int cmd, struct file_lock *request)
  2494. {
  2495. /* set lock type */
  2496. switch (request->fl_type) {
  2497. case F_RDLCK:
  2498. return IS_SETLKW(cmd) ? NFS4_READW_LT : NFS4_READ_LT;
  2499. case F_WRLCK:
  2500. return IS_SETLKW(cmd) ? NFS4_WRITEW_LT : NFS4_WRITE_LT;
  2501. case F_UNLCK:
  2502. return NFS4_WRITE_LT;
  2503. }
  2504. BUG();
  2505. return 0;
  2506. }
  2507. static inline uint64_t
  2508. nfs4_lck_length(struct file_lock *request)
  2509. {
  2510. if (request->fl_end == OFFSET_MAX)
  2511. return ~(uint64_t)0;
  2512. return request->fl_end - request->fl_start + 1;
  2513. }
  2514. static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  2515. {
  2516. struct inode *inode = state->inode;
  2517. struct nfs_server *server = NFS_SERVER(inode);
  2518. struct nfs4_client *clp = server->nfs4_state;
  2519. struct nfs_lockargs arg = {
  2520. .fh = NFS_FH(inode),
  2521. .type = nfs4_lck_type(cmd, request),
  2522. .offset = request->fl_start,
  2523. .length = nfs4_lck_length(request),
  2524. };
  2525. struct nfs_lockres res = {
  2526. .server = server,
  2527. };
  2528. struct rpc_message msg = {
  2529. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
  2530. .rpc_argp = &arg,
  2531. .rpc_resp = &res,
  2532. .rpc_cred = state->owner->so_cred,
  2533. };
  2534. struct nfs_lowner nlo;
  2535. struct nfs4_lock_state *lsp;
  2536. int status;
  2537. down_read(&clp->cl_sem);
  2538. nlo.clientid = clp->cl_clientid;
  2539. status = nfs4_set_lock_state(state, request);
  2540. if (status != 0)
  2541. goto out;
  2542. lsp = request->fl_u.nfs4_fl.owner;
  2543. nlo.id = lsp->ls_id;
  2544. arg.u.lockt = &nlo;
  2545. status = rpc_call_sync(server->client, &msg, 0);
  2546. if (!status) {
  2547. request->fl_type = F_UNLCK;
  2548. } else if (status == -NFS4ERR_DENIED) {
  2549. int64_t len, start, end;
  2550. start = res.u.denied.offset;
  2551. len = res.u.denied.length;
  2552. end = start + len - 1;
  2553. if (end < 0 || len == 0)
  2554. request->fl_end = OFFSET_MAX;
  2555. else
  2556. request->fl_end = (loff_t)end;
  2557. request->fl_start = (loff_t)start;
  2558. request->fl_type = F_WRLCK;
  2559. if (res.u.denied.type & 1)
  2560. request->fl_type = F_RDLCK;
  2561. request->fl_pid = 0;
  2562. status = 0;
  2563. }
  2564. out:
  2565. up_read(&clp->cl_sem);
  2566. return status;
  2567. }
  2568. static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  2569. {
  2570. struct nfs4_exception exception = { };
  2571. int err;
  2572. do {
  2573. err = nfs4_handle_exception(NFS_SERVER(state->inode),
  2574. _nfs4_proc_getlk(state, cmd, request),
  2575. &exception);
  2576. } while (exception.retry);
  2577. return err;
  2578. }
  2579. static int do_vfs_lock(struct file *file, struct file_lock *fl)
  2580. {
  2581. int res = 0;
  2582. switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
  2583. case FL_POSIX:
  2584. res = posix_lock_file_wait(file, fl);
  2585. break;
  2586. case FL_FLOCK:
  2587. res = flock_lock_file_wait(file, fl);
  2588. break;
  2589. default:
  2590. BUG();
  2591. }
  2592. if (res < 0)
  2593. printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n",
  2594. __FUNCTION__);
  2595. return res;
  2596. }
  2597. struct nfs4_unlockdata {
  2598. struct nfs_lockargs arg;
  2599. struct nfs_locku_opargs luargs;
  2600. struct nfs_lockres res;
  2601. struct nfs4_lock_state *lsp;
  2602. struct nfs_open_context *ctx;
  2603. atomic_t refcount;
  2604. struct completion completion;
  2605. };
  2606. static void nfs4_locku_release_calldata(struct nfs4_unlockdata *calldata)
  2607. {
  2608. if (atomic_dec_and_test(&calldata->refcount)) {
  2609. nfs_free_seqid(calldata->luargs.seqid);
  2610. nfs4_put_lock_state(calldata->lsp);
  2611. put_nfs_open_context(calldata->ctx);
  2612. kfree(calldata);
  2613. }
  2614. }
  2615. static void nfs4_locku_complete(struct nfs4_unlockdata *calldata)
  2616. {
  2617. complete(&calldata->completion);
  2618. nfs4_locku_release_calldata(calldata);
  2619. }
  2620. static void nfs4_locku_done(struct rpc_task *task)
  2621. {
  2622. struct nfs4_unlockdata *calldata = (struct nfs4_unlockdata *)task->tk_calldata;
  2623. nfs_increment_lock_seqid(task->tk_status, calldata->luargs.seqid);
  2624. switch (task->tk_status) {
  2625. case 0:
  2626. memcpy(calldata->lsp->ls_stateid.data,
  2627. calldata->res.u.stateid.data,
  2628. sizeof(calldata->lsp->ls_stateid.data));
  2629. break;
  2630. case -NFS4ERR_STALE_STATEID:
  2631. case -NFS4ERR_EXPIRED:
  2632. nfs4_schedule_state_recovery(calldata->res.server->nfs4_state);
  2633. break;
  2634. default:
  2635. if (nfs4_async_handle_error(task, calldata->res.server) == -EAGAIN) {
  2636. rpc_restart_call(task);
  2637. return;
  2638. }
  2639. }
  2640. nfs4_locku_complete(calldata);
  2641. }
  2642. static void nfs4_locku_begin(struct rpc_task *task)
  2643. {
  2644. struct nfs4_unlockdata *calldata = (struct nfs4_unlockdata *)task->tk_calldata;
  2645. struct rpc_message msg = {
  2646. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
  2647. .rpc_argp = &calldata->arg,
  2648. .rpc_resp = &calldata->res,
  2649. .rpc_cred = calldata->lsp->ls_state->owner->so_cred,
  2650. };
  2651. int status;
  2652. status = nfs_wait_on_sequence(calldata->luargs.seqid, task);
  2653. if (status != 0)
  2654. return;
  2655. if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
  2656. nfs4_locku_complete(calldata);
  2657. task->tk_exit = NULL;
  2658. rpc_exit(task, 0);
  2659. return;
  2660. }
  2661. rpc_call_setup(task, &msg, 0);
  2662. }
  2663. static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
  2664. {
  2665. struct nfs4_unlockdata *calldata;
  2666. struct inode *inode = state->inode;
  2667. struct nfs_server *server = NFS_SERVER(inode);
  2668. struct nfs4_lock_state *lsp;
  2669. int status;
  2670. status = nfs4_set_lock_state(state, request);
  2671. if (status != 0)
  2672. return status;
  2673. lsp = request->fl_u.nfs4_fl.owner;
  2674. /* We might have lost the locks! */
  2675. if ((lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0)
  2676. return 0;
  2677. calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
  2678. if (calldata == NULL)
  2679. return -ENOMEM;
  2680. calldata->luargs.seqid = nfs_alloc_seqid(&lsp->ls_seqid);
  2681. if (calldata->luargs.seqid == NULL) {
  2682. kfree(calldata);
  2683. return -ENOMEM;
  2684. }
  2685. calldata->luargs.stateid = &lsp->ls_stateid;
  2686. calldata->arg.fh = NFS_FH(inode);
  2687. calldata->arg.type = nfs4_lck_type(cmd, request);
  2688. calldata->arg.offset = request->fl_start;
  2689. calldata->arg.length = nfs4_lck_length(request);
  2690. calldata->arg.u.locku = &calldata->luargs;
  2691. calldata->res.server = server;
  2692. calldata->lsp = lsp;
  2693. atomic_inc(&lsp->ls_count);
  2694. /* Ensure we don't close file until we're done freeing locks! */
  2695. calldata->ctx = get_nfs_open_context((struct nfs_open_context*)request->fl_file->private_data);
  2696. atomic_set(&calldata->refcount, 2);
  2697. init_completion(&calldata->completion);
  2698. status = nfs4_call_async(NFS_SERVER(inode)->client, nfs4_locku_begin,
  2699. nfs4_locku_done, calldata);
  2700. if (status == 0)
  2701. wait_for_completion_interruptible(&calldata->completion);
  2702. do_vfs_lock(request->fl_file, request);
  2703. nfs4_locku_release_calldata(calldata);
  2704. return status;
  2705. }
  2706. static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *request, int reclaim)
  2707. {
  2708. struct inode *inode = state->inode;
  2709. struct nfs_server *server = NFS_SERVER(inode);
  2710. struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
  2711. struct nfs_lock_opargs largs = {
  2712. .lock_stateid = &lsp->ls_stateid,
  2713. .open_stateid = &state->stateid,
  2714. .lock_owner = {
  2715. .clientid = server->nfs4_state->cl_clientid,
  2716. .id = lsp->ls_id,
  2717. },
  2718. .reclaim = reclaim,
  2719. };
  2720. struct nfs_lockargs arg = {
  2721. .fh = NFS_FH(inode),
  2722. .type = nfs4_lck_type(cmd, request),
  2723. .offset = request->fl_start,
  2724. .length = nfs4_lck_length(request),
  2725. .u = {
  2726. .lock = &largs,
  2727. },
  2728. };
  2729. struct nfs_lockres res = {
  2730. .server = server,
  2731. };
  2732. struct rpc_message msg = {
  2733. .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
  2734. .rpc_argp = &arg,
  2735. .rpc_resp = &res,
  2736. .rpc_cred = state->owner->so_cred,
  2737. };
  2738. int status = -ENOMEM;
  2739. largs.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
  2740. if (largs.lock_seqid == NULL)
  2741. return -ENOMEM;
  2742. if (!(lsp->ls_seqid.flags & NFS_SEQID_CONFIRMED)) {
  2743. struct nfs4_state_owner *owner = state->owner;
  2744. largs.open_seqid = nfs_alloc_seqid(&owner->so_seqid);
  2745. if (largs.open_seqid == NULL)
  2746. goto out;
  2747. largs.new_lock_owner = 1;
  2748. status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
  2749. /* increment open seqid on success, and seqid mutating errors */
  2750. if (largs.new_lock_owner != 0) {
  2751. nfs_increment_open_seqid(status, largs.open_seqid);
  2752. if (status == 0)
  2753. nfs_confirm_seqid(&lsp->ls_seqid, 0);
  2754. }
  2755. nfs_free_seqid(largs.open_seqid);
  2756. } else
  2757. status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
  2758. /* increment lock seqid on success, and seqid mutating errors*/
  2759. nfs_increment_lock_seqid(status, largs.lock_seqid);
  2760. /* save the returned stateid. */
  2761. if (status == 0) {
  2762. memcpy(lsp->ls_stateid.data, res.u.stateid.data,
  2763. sizeof(lsp->ls_stateid.data));
  2764. lsp->ls_flags |= NFS_LOCK_INITIALIZED;
  2765. } else if (status == -NFS4ERR_DENIED)
  2766. status = -EAGAIN;
  2767. out:
  2768. nfs_free_seqid(largs.lock_seqid);
  2769. return status;
  2770. }
  2771. static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
  2772. {
  2773. struct nfs_server *server = NFS_SERVER(state->inode);
  2774. struct nfs4_exception exception = { };
  2775. int err;
  2776. do {
  2777. err = _nfs4_do_setlk(state, F_SETLK, request, 1);
  2778. if (err != -NFS4ERR_DELAY)
  2779. break;
  2780. nfs4_handle_exception(server, err, &exception);
  2781. } while (exception.retry);
  2782. return err;
  2783. }
  2784. static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
  2785. {
  2786. struct nfs_server *server = NFS_SERVER(state->inode);
  2787. struct nfs4_exception exception = { };
  2788. int err;
  2789. do {
  2790. err = _nfs4_do_setlk(state, F_SETLK, request, 0);
  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_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  2798. {
  2799. struct nfs4_client *clp = state->owner->so_client;
  2800. int status;
  2801. down_read(&clp->cl_sem);
  2802. status = nfs4_set_lock_state(state, request);
  2803. if (status == 0)
  2804. status = _nfs4_do_setlk(state, cmd, request, 0);
  2805. if (status == 0) {
  2806. /* Note: we always want to sleep here! */
  2807. request->fl_flags |= FL_SLEEP;
  2808. if (do_vfs_lock(request->fl_file, request) < 0)
  2809. printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __FUNCTION__);
  2810. }
  2811. up_read(&clp->cl_sem);
  2812. return status;
  2813. }
  2814. static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
  2815. {
  2816. struct nfs4_exception exception = { };
  2817. int err;
  2818. do {
  2819. err = nfs4_handle_exception(NFS_SERVER(state->inode),
  2820. _nfs4_proc_setlk(state, cmd, request),
  2821. &exception);
  2822. } while (exception.retry);
  2823. return err;
  2824. }
  2825. static int
  2826. nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
  2827. {
  2828. struct nfs_open_context *ctx;
  2829. struct nfs4_state *state;
  2830. unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
  2831. int status;
  2832. /* verify open state */
  2833. ctx = (struct nfs_open_context *)filp->private_data;
  2834. state = ctx->state;
  2835. if (request->fl_start < 0 || request->fl_end < 0)
  2836. return -EINVAL;
  2837. if (IS_GETLK(cmd))
  2838. return nfs4_proc_getlk(state, F_GETLK, request);
  2839. if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
  2840. return -EINVAL;
  2841. if (request->fl_type == F_UNLCK)
  2842. return nfs4_proc_unlck(state, cmd, request);
  2843. do {
  2844. status = nfs4_proc_setlk(state, cmd, request);
  2845. if ((status != -EAGAIN) || IS_SETLK(cmd))
  2846. break;
  2847. timeout = nfs4_set_lock_task_retry(timeout);
  2848. status = -ERESTARTSYS;
  2849. if (signalled())
  2850. break;
  2851. } while(status < 0);
  2852. return status;
  2853. }
  2854. #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
  2855. int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
  2856. size_t buflen, int flags)
  2857. {
  2858. struct inode *inode = dentry->d_inode;
  2859. if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
  2860. return -EOPNOTSUPP;
  2861. if (!S_ISREG(inode->i_mode) &&
  2862. (!S_ISDIR(inode->i_mode) || inode->i_mode & S_ISVTX))
  2863. return -EPERM;
  2864. return nfs4_proc_set_acl(inode, buf, buflen);
  2865. }
  2866. /* The getxattr man page suggests returning -ENODATA for unknown attributes,
  2867. * and that's what we'll do for e.g. user attributes that haven't been set.
  2868. * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
  2869. * attributes in kernel-managed attribute namespaces. */
  2870. ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
  2871. size_t buflen)
  2872. {
  2873. struct inode *inode = dentry->d_inode;
  2874. if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
  2875. return -EOPNOTSUPP;
  2876. return nfs4_proc_get_acl(inode, buf, buflen);
  2877. }
  2878. ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
  2879. {
  2880. size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
  2881. if (buf && buflen < len)
  2882. return -ERANGE;
  2883. if (buf)
  2884. memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
  2885. return len;
  2886. }
  2887. struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
  2888. .recover_open = nfs4_open_reclaim,
  2889. .recover_lock = nfs4_lock_reclaim,
  2890. };
  2891. struct nfs4_state_recovery_ops nfs4_network_partition_recovery_ops = {
  2892. .recover_open = nfs4_open_expired,
  2893. .recover_lock = nfs4_lock_expired,
  2894. };
  2895. static struct inode_operations nfs4_file_inode_operations = {
  2896. .permission = nfs_permission,
  2897. .getattr = nfs_getattr,
  2898. .setattr = nfs_setattr,
  2899. .getxattr = nfs4_getxattr,
  2900. .setxattr = nfs4_setxattr,
  2901. .listxattr = nfs4_listxattr,
  2902. };
  2903. struct nfs_rpc_ops nfs_v4_clientops = {
  2904. .version = 4, /* protocol version */
  2905. .dentry_ops = &nfs4_dentry_operations,
  2906. .dir_inode_ops = &nfs4_dir_inode_operations,
  2907. .file_inode_ops = &nfs4_file_inode_operations,
  2908. .getroot = nfs4_proc_get_root,
  2909. .getattr = nfs4_proc_getattr,
  2910. .setattr = nfs4_proc_setattr,
  2911. .lookup = nfs4_proc_lookup,
  2912. .access = nfs4_proc_access,
  2913. .readlink = nfs4_proc_readlink,
  2914. .read = nfs4_proc_read,
  2915. .write = nfs4_proc_write,
  2916. .commit = nfs4_proc_commit,
  2917. .create = nfs4_proc_create,
  2918. .remove = nfs4_proc_remove,
  2919. .unlink_setup = nfs4_proc_unlink_setup,
  2920. .unlink_done = nfs4_proc_unlink_done,
  2921. .rename = nfs4_proc_rename,
  2922. .link = nfs4_proc_link,
  2923. .symlink = nfs4_proc_symlink,
  2924. .mkdir = nfs4_proc_mkdir,
  2925. .rmdir = nfs4_proc_remove,
  2926. .readdir = nfs4_proc_readdir,
  2927. .mknod = nfs4_proc_mknod,
  2928. .statfs = nfs4_proc_statfs,
  2929. .fsinfo = nfs4_proc_fsinfo,
  2930. .pathconf = nfs4_proc_pathconf,
  2931. .decode_dirent = nfs4_decode_dirent,
  2932. .read_setup = nfs4_proc_read_setup,
  2933. .write_setup = nfs4_proc_write_setup,
  2934. .commit_setup = nfs4_proc_commit_setup,
  2935. .file_open = nfs_open,
  2936. .file_release = nfs_release,
  2937. .lock = nfs4_proc_lock,
  2938. .clear_acl_cache = nfs4_zap_acl_attr,
  2939. };
  2940. /*
  2941. * Local variables:
  2942. * c-basic-offset: 8
  2943. * End:
  2944. */