nfs4proc.c 81 KB

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