nfs4state.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235
  1. /*
  2. * fs/nfs/nfs4state.c
  3. *
  4. * Client-side XDR 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. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in the
  19. * documentation and/or other materials provided with the distribution.
  20. * 3. Neither the name of the University nor the names of its
  21. * contributors may be used to endorse or promote products derived
  22. * from this software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  25. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  26. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  27. * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  31. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  32. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  33. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  34. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. * Implementation of the NFSv4 state model. For the time being,
  37. * this is minimal, but will be made much more complex in a
  38. * subsequent patch.
  39. */
  40. #include <linux/kernel.h>
  41. #include <linux/slab.h>
  42. #include <linux/fs.h>
  43. #include <linux/nfs_fs.h>
  44. #include <linux/nfs_idmap.h>
  45. #include <linux/kthread.h>
  46. #include <linux/module.h>
  47. #include <linux/random.h>
  48. #include <linux/ratelimit.h>
  49. #include <linux/workqueue.h>
  50. #include <linux/bitops.h>
  51. #include <linux/jiffies.h>
  52. #include <linux/sunrpc/clnt.h>
  53. #include "nfs4_fs.h"
  54. #include "callback.h"
  55. #include "delegation.h"
  56. #include "internal.h"
  57. #include "nfs4session.h"
  58. #include "pnfs.h"
  59. #include "netns.h"
  60. #define NFSDBG_FACILITY NFSDBG_STATE
  61. #define OPENOWNER_POOL_SIZE 8
  62. const nfs4_stateid zero_stateid;
  63. static DEFINE_MUTEX(nfs_clid_init_mutex);
  64. static LIST_HEAD(nfs4_clientid_list);
  65. int nfs4_init_clientid(struct nfs_client *clp, struct rpc_cred *cred)
  66. {
  67. struct nfs4_setclientid_res clid = {
  68. .clientid = clp->cl_clientid,
  69. .confirm = clp->cl_confirm,
  70. };
  71. unsigned short port;
  72. int status;
  73. struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
  74. if (test_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state))
  75. goto do_confirm;
  76. port = nn->nfs_callback_tcpport;
  77. if (clp->cl_addr.ss_family == AF_INET6)
  78. port = nn->nfs_callback_tcpport6;
  79. status = nfs4_proc_setclientid(clp, NFS4_CALLBACK, port, cred, &clid);
  80. if (status != 0)
  81. goto out;
  82. clp->cl_clientid = clid.clientid;
  83. clp->cl_confirm = clid.confirm;
  84. set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
  85. do_confirm:
  86. status = nfs4_proc_setclientid_confirm(clp, &clid, cred);
  87. if (status != 0)
  88. goto out;
  89. clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
  90. nfs4_schedule_state_renewal(clp);
  91. out:
  92. return status;
  93. }
  94. /**
  95. * nfs40_discover_server_trunking - Detect server IP address trunking (mv0)
  96. *
  97. * @clp: nfs_client under test
  98. * @result: OUT: found nfs_client, or clp
  99. * @cred: credential to use for trunking test
  100. *
  101. * Returns zero, a negative errno, or a negative NFS4ERR status.
  102. * If zero is returned, an nfs_client pointer is planted in
  103. * "result".
  104. *
  105. * Note: The returned client may not yet be marked ready.
  106. */
  107. int nfs40_discover_server_trunking(struct nfs_client *clp,
  108. struct nfs_client **result,
  109. struct rpc_cred *cred)
  110. {
  111. struct nfs4_setclientid_res clid = {
  112. .clientid = clp->cl_clientid,
  113. .confirm = clp->cl_confirm,
  114. };
  115. struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
  116. unsigned short port;
  117. int status;
  118. port = nn->nfs_callback_tcpport;
  119. if (clp->cl_addr.ss_family == AF_INET6)
  120. port = nn->nfs_callback_tcpport6;
  121. status = nfs4_proc_setclientid(clp, NFS4_CALLBACK, port, cred, &clid);
  122. if (status != 0)
  123. goto out;
  124. clp->cl_clientid = clid.clientid;
  125. clp->cl_confirm = clid.confirm;
  126. status = nfs40_walk_client_list(clp, result, cred);
  127. switch (status) {
  128. case -NFS4ERR_STALE_CLIENTID:
  129. set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
  130. case 0:
  131. /* Sustain the lease, even if it's empty. If the clientid4
  132. * goes stale it's of no use for trunking discovery. */
  133. nfs4_schedule_state_renewal(*result);
  134. break;
  135. }
  136. out:
  137. return status;
  138. }
  139. struct rpc_cred *nfs4_get_machine_cred_locked(struct nfs_client *clp)
  140. {
  141. struct rpc_cred *cred = NULL;
  142. if (clp->cl_machine_cred != NULL)
  143. cred = get_rpccred(clp->cl_machine_cred);
  144. return cred;
  145. }
  146. static void nfs4_clear_machine_cred(struct nfs_client *clp)
  147. {
  148. struct rpc_cred *cred;
  149. spin_lock(&clp->cl_lock);
  150. cred = clp->cl_machine_cred;
  151. clp->cl_machine_cred = NULL;
  152. spin_unlock(&clp->cl_lock);
  153. if (cred != NULL)
  154. put_rpccred(cred);
  155. }
  156. static struct rpc_cred *
  157. nfs4_get_renew_cred_server_locked(struct nfs_server *server)
  158. {
  159. struct rpc_cred *cred = NULL;
  160. struct nfs4_state_owner *sp;
  161. struct rb_node *pos;
  162. for (pos = rb_first(&server->state_owners);
  163. pos != NULL;
  164. pos = rb_next(pos)) {
  165. sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
  166. if (list_empty(&sp->so_states))
  167. continue;
  168. cred = get_rpccred(sp->so_cred);
  169. break;
  170. }
  171. return cred;
  172. }
  173. /**
  174. * nfs4_get_renew_cred_locked - Acquire credential for a renew operation
  175. * @clp: client state handle
  176. *
  177. * Returns an rpc_cred with reference count bumped, or NULL.
  178. * Caller must hold clp->cl_lock.
  179. */
  180. struct rpc_cred *nfs4_get_renew_cred_locked(struct nfs_client *clp)
  181. {
  182. struct rpc_cred *cred = NULL;
  183. struct nfs_server *server;
  184. /* Use machine credentials if available */
  185. cred = nfs4_get_machine_cred_locked(clp);
  186. if (cred != NULL)
  187. goto out;
  188. rcu_read_lock();
  189. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  190. cred = nfs4_get_renew_cred_server_locked(server);
  191. if (cred != NULL)
  192. break;
  193. }
  194. rcu_read_unlock();
  195. out:
  196. return cred;
  197. }
  198. #if defined(CONFIG_NFS_V4_1)
  199. static int nfs41_setup_state_renewal(struct nfs_client *clp)
  200. {
  201. int status;
  202. struct nfs_fsinfo fsinfo;
  203. if (!test_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state)) {
  204. nfs4_schedule_state_renewal(clp);
  205. return 0;
  206. }
  207. status = nfs4_proc_get_lease_time(clp, &fsinfo);
  208. if (status == 0) {
  209. /* Update lease time and schedule renewal */
  210. spin_lock(&clp->cl_lock);
  211. clp->cl_lease_time = fsinfo.lease_time * HZ;
  212. clp->cl_last_renewal = jiffies;
  213. spin_unlock(&clp->cl_lock);
  214. nfs4_schedule_state_renewal(clp);
  215. }
  216. return status;
  217. }
  218. /*
  219. * Back channel returns NFS4ERR_DELAY for new requests when
  220. * NFS4_SESSION_DRAINING is set so there is no work to be done when draining
  221. * is ended.
  222. */
  223. static void nfs4_end_drain_session(struct nfs_client *clp)
  224. {
  225. struct nfs4_session *ses = clp->cl_session;
  226. struct nfs4_slot_table *tbl;
  227. unsigned int i;
  228. if (ses == NULL)
  229. return;
  230. tbl = &ses->fc_slot_table;
  231. if (test_and_clear_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
  232. spin_lock(&tbl->slot_tbl_lock);
  233. for (i = 0; i <= tbl->max_slotid; i++) {
  234. if (rpc_wake_up_first(&tbl->slot_tbl_waitq,
  235. nfs4_set_task_privileged,
  236. NULL) == NULL)
  237. break;
  238. }
  239. spin_unlock(&tbl->slot_tbl_lock);
  240. }
  241. }
  242. /*
  243. * Signal state manager thread if session fore channel is drained
  244. */
  245. void nfs4_session_drain_complete(struct nfs4_session *session,
  246. struct nfs4_slot_table *tbl)
  247. {
  248. if (nfs4_session_draining(session))
  249. complete(&tbl->complete);
  250. }
  251. static int nfs4_wait_on_slot_tbl(struct nfs4_slot_table *tbl)
  252. {
  253. spin_lock(&tbl->slot_tbl_lock);
  254. if (tbl->highest_used_slotid != NFS4_NO_SLOT) {
  255. INIT_COMPLETION(tbl->complete);
  256. spin_unlock(&tbl->slot_tbl_lock);
  257. return wait_for_completion_interruptible(&tbl->complete);
  258. }
  259. spin_unlock(&tbl->slot_tbl_lock);
  260. return 0;
  261. }
  262. static int nfs4_begin_drain_session(struct nfs_client *clp)
  263. {
  264. struct nfs4_session *ses = clp->cl_session;
  265. int ret = 0;
  266. set_bit(NFS4_SESSION_DRAINING, &ses->session_state);
  267. /* back channel */
  268. ret = nfs4_wait_on_slot_tbl(&ses->bc_slot_table);
  269. if (ret)
  270. return ret;
  271. /* fore channel */
  272. return nfs4_wait_on_slot_tbl(&ses->fc_slot_table);
  273. }
  274. static void nfs41_finish_session_reset(struct nfs_client *clp)
  275. {
  276. clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
  277. clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
  278. /* create_session negotiated new slot table */
  279. clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
  280. nfs41_setup_state_renewal(clp);
  281. }
  282. int nfs41_init_clientid(struct nfs_client *clp, struct rpc_cred *cred)
  283. {
  284. int status;
  285. if (test_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state))
  286. goto do_confirm;
  287. nfs4_begin_drain_session(clp);
  288. status = nfs4_proc_exchange_id(clp, cred);
  289. if (status != 0)
  290. goto out;
  291. set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
  292. do_confirm:
  293. status = nfs4_proc_create_session(clp, cred);
  294. if (status != 0)
  295. goto out;
  296. nfs41_finish_session_reset(clp);
  297. nfs_mark_client_ready(clp, NFS_CS_READY);
  298. out:
  299. return status;
  300. }
  301. /**
  302. * nfs41_discover_server_trunking - Detect server IP address trunking (mv1)
  303. *
  304. * @clp: nfs_client under test
  305. * @result: OUT: found nfs_client, or clp
  306. * @cred: credential to use for trunking test
  307. *
  308. * Returns NFS4_OK, a negative errno, or a negative NFS4ERR status.
  309. * If NFS4_OK is returned, an nfs_client pointer is planted in
  310. * "result".
  311. *
  312. * Note: The returned client may not yet be marked ready.
  313. */
  314. int nfs41_discover_server_trunking(struct nfs_client *clp,
  315. struct nfs_client **result,
  316. struct rpc_cred *cred)
  317. {
  318. int status;
  319. status = nfs4_proc_exchange_id(clp, cred);
  320. if (status != NFS4_OK)
  321. return status;
  322. set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
  323. return nfs41_walk_client_list(clp, result, cred);
  324. }
  325. struct rpc_cred *nfs4_get_exchange_id_cred(struct nfs_client *clp)
  326. {
  327. struct rpc_cred *cred;
  328. spin_lock(&clp->cl_lock);
  329. cred = nfs4_get_machine_cred_locked(clp);
  330. spin_unlock(&clp->cl_lock);
  331. return cred;
  332. }
  333. #endif /* CONFIG_NFS_V4_1 */
  334. static struct rpc_cred *
  335. nfs4_get_setclientid_cred_server(struct nfs_server *server)
  336. {
  337. struct nfs_client *clp = server->nfs_client;
  338. struct rpc_cred *cred = NULL;
  339. struct nfs4_state_owner *sp;
  340. struct rb_node *pos;
  341. spin_lock(&clp->cl_lock);
  342. pos = rb_first(&server->state_owners);
  343. if (pos != NULL) {
  344. sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
  345. cred = get_rpccred(sp->so_cred);
  346. }
  347. spin_unlock(&clp->cl_lock);
  348. return cred;
  349. }
  350. /**
  351. * nfs4_get_setclientid_cred - Acquire credential for a setclientid operation
  352. * @clp: client state handle
  353. *
  354. * Returns an rpc_cred with reference count bumped, or NULL.
  355. */
  356. struct rpc_cred *nfs4_get_setclientid_cred(struct nfs_client *clp)
  357. {
  358. struct nfs_server *server;
  359. struct rpc_cred *cred;
  360. spin_lock(&clp->cl_lock);
  361. cred = nfs4_get_machine_cred_locked(clp);
  362. spin_unlock(&clp->cl_lock);
  363. if (cred != NULL)
  364. goto out;
  365. rcu_read_lock();
  366. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  367. cred = nfs4_get_setclientid_cred_server(server);
  368. if (cred != NULL)
  369. break;
  370. }
  371. rcu_read_unlock();
  372. out:
  373. return cred;
  374. }
  375. static struct nfs4_state_owner *
  376. nfs4_find_state_owner_locked(struct nfs_server *server, struct rpc_cred *cred)
  377. {
  378. struct rb_node **p = &server->state_owners.rb_node,
  379. *parent = NULL;
  380. struct nfs4_state_owner *sp;
  381. while (*p != NULL) {
  382. parent = *p;
  383. sp = rb_entry(parent, struct nfs4_state_owner, so_server_node);
  384. if (cred < sp->so_cred)
  385. p = &parent->rb_left;
  386. else if (cred > sp->so_cred)
  387. p = &parent->rb_right;
  388. else {
  389. if (!list_empty(&sp->so_lru))
  390. list_del_init(&sp->so_lru);
  391. atomic_inc(&sp->so_count);
  392. return sp;
  393. }
  394. }
  395. return NULL;
  396. }
  397. static struct nfs4_state_owner *
  398. nfs4_insert_state_owner_locked(struct nfs4_state_owner *new)
  399. {
  400. struct nfs_server *server = new->so_server;
  401. struct rb_node **p = &server->state_owners.rb_node,
  402. *parent = NULL;
  403. struct nfs4_state_owner *sp;
  404. int err;
  405. while (*p != NULL) {
  406. parent = *p;
  407. sp = rb_entry(parent, struct nfs4_state_owner, so_server_node);
  408. if (new->so_cred < sp->so_cred)
  409. p = &parent->rb_left;
  410. else if (new->so_cred > sp->so_cred)
  411. p = &parent->rb_right;
  412. else {
  413. if (!list_empty(&sp->so_lru))
  414. list_del_init(&sp->so_lru);
  415. atomic_inc(&sp->so_count);
  416. return sp;
  417. }
  418. }
  419. err = ida_get_new(&server->openowner_id, &new->so_seqid.owner_id);
  420. if (err)
  421. return ERR_PTR(err);
  422. rb_link_node(&new->so_server_node, parent, p);
  423. rb_insert_color(&new->so_server_node, &server->state_owners);
  424. return new;
  425. }
  426. static void
  427. nfs4_remove_state_owner_locked(struct nfs4_state_owner *sp)
  428. {
  429. struct nfs_server *server = sp->so_server;
  430. if (!RB_EMPTY_NODE(&sp->so_server_node))
  431. rb_erase(&sp->so_server_node, &server->state_owners);
  432. ida_remove(&server->openowner_id, sp->so_seqid.owner_id);
  433. }
  434. static void
  435. nfs4_init_seqid_counter(struct nfs_seqid_counter *sc)
  436. {
  437. sc->create_time = ktime_get();
  438. sc->flags = 0;
  439. sc->counter = 0;
  440. spin_lock_init(&sc->lock);
  441. INIT_LIST_HEAD(&sc->list);
  442. rpc_init_wait_queue(&sc->wait, "Seqid_waitqueue");
  443. }
  444. static void
  445. nfs4_destroy_seqid_counter(struct nfs_seqid_counter *sc)
  446. {
  447. rpc_destroy_wait_queue(&sc->wait);
  448. }
  449. /*
  450. * nfs4_alloc_state_owner(): this is called on the OPEN or CREATE path to
  451. * create a new state_owner.
  452. *
  453. */
  454. static struct nfs4_state_owner *
  455. nfs4_alloc_state_owner(struct nfs_server *server,
  456. struct rpc_cred *cred,
  457. gfp_t gfp_flags)
  458. {
  459. struct nfs4_state_owner *sp;
  460. sp = kzalloc(sizeof(*sp), gfp_flags);
  461. if (!sp)
  462. return NULL;
  463. sp->so_server = server;
  464. sp->so_cred = get_rpccred(cred);
  465. spin_lock_init(&sp->so_lock);
  466. INIT_LIST_HEAD(&sp->so_states);
  467. nfs4_init_seqid_counter(&sp->so_seqid);
  468. atomic_set(&sp->so_count, 1);
  469. INIT_LIST_HEAD(&sp->so_lru);
  470. return sp;
  471. }
  472. static void
  473. nfs4_drop_state_owner(struct nfs4_state_owner *sp)
  474. {
  475. struct rb_node *rb_node = &sp->so_server_node;
  476. if (!RB_EMPTY_NODE(rb_node)) {
  477. struct nfs_server *server = sp->so_server;
  478. struct nfs_client *clp = server->nfs_client;
  479. spin_lock(&clp->cl_lock);
  480. if (!RB_EMPTY_NODE(rb_node)) {
  481. rb_erase(rb_node, &server->state_owners);
  482. RB_CLEAR_NODE(rb_node);
  483. }
  484. spin_unlock(&clp->cl_lock);
  485. }
  486. }
  487. static void nfs4_free_state_owner(struct nfs4_state_owner *sp)
  488. {
  489. nfs4_destroy_seqid_counter(&sp->so_seqid);
  490. put_rpccred(sp->so_cred);
  491. kfree(sp);
  492. }
  493. static void nfs4_gc_state_owners(struct nfs_server *server)
  494. {
  495. struct nfs_client *clp = server->nfs_client;
  496. struct nfs4_state_owner *sp, *tmp;
  497. unsigned long time_min, time_max;
  498. LIST_HEAD(doomed);
  499. spin_lock(&clp->cl_lock);
  500. time_max = jiffies;
  501. time_min = (long)time_max - (long)clp->cl_lease_time;
  502. list_for_each_entry_safe(sp, tmp, &server->state_owners_lru, so_lru) {
  503. /* NB: LRU is sorted so that oldest is at the head */
  504. if (time_in_range(sp->so_expires, time_min, time_max))
  505. break;
  506. list_move(&sp->so_lru, &doomed);
  507. nfs4_remove_state_owner_locked(sp);
  508. }
  509. spin_unlock(&clp->cl_lock);
  510. list_for_each_entry_safe(sp, tmp, &doomed, so_lru) {
  511. list_del(&sp->so_lru);
  512. nfs4_free_state_owner(sp);
  513. }
  514. }
  515. /**
  516. * nfs4_get_state_owner - Look up a state owner given a credential
  517. * @server: nfs_server to search
  518. * @cred: RPC credential to match
  519. *
  520. * Returns a pointer to an instantiated nfs4_state_owner struct, or NULL.
  521. */
  522. struct nfs4_state_owner *nfs4_get_state_owner(struct nfs_server *server,
  523. struct rpc_cred *cred,
  524. gfp_t gfp_flags)
  525. {
  526. struct nfs_client *clp = server->nfs_client;
  527. struct nfs4_state_owner *sp, *new;
  528. spin_lock(&clp->cl_lock);
  529. sp = nfs4_find_state_owner_locked(server, cred);
  530. spin_unlock(&clp->cl_lock);
  531. if (sp != NULL)
  532. goto out;
  533. new = nfs4_alloc_state_owner(server, cred, gfp_flags);
  534. if (new == NULL)
  535. goto out;
  536. do {
  537. if (ida_pre_get(&server->openowner_id, gfp_flags) == 0)
  538. break;
  539. spin_lock(&clp->cl_lock);
  540. sp = nfs4_insert_state_owner_locked(new);
  541. spin_unlock(&clp->cl_lock);
  542. } while (sp == ERR_PTR(-EAGAIN));
  543. if (sp != new)
  544. nfs4_free_state_owner(new);
  545. out:
  546. nfs4_gc_state_owners(server);
  547. return sp;
  548. }
  549. /**
  550. * nfs4_put_state_owner - Release a nfs4_state_owner
  551. * @sp: state owner data to release
  552. *
  553. * Note that we keep released state owners on an LRU
  554. * list.
  555. * This caches valid state owners so that they can be
  556. * reused, to avoid the OPEN_CONFIRM on minor version 0.
  557. * It also pins the uniquifier of dropped state owners for
  558. * a while, to ensure that those state owner names are
  559. * never reused.
  560. */
  561. void nfs4_put_state_owner(struct nfs4_state_owner *sp)
  562. {
  563. struct nfs_server *server = sp->so_server;
  564. struct nfs_client *clp = server->nfs_client;
  565. if (!atomic_dec_and_lock(&sp->so_count, &clp->cl_lock))
  566. return;
  567. sp->so_expires = jiffies;
  568. list_add_tail(&sp->so_lru, &server->state_owners_lru);
  569. spin_unlock(&clp->cl_lock);
  570. }
  571. /**
  572. * nfs4_purge_state_owners - Release all cached state owners
  573. * @server: nfs_server with cached state owners to release
  574. *
  575. * Called at umount time. Remaining state owners will be on
  576. * the LRU with ref count of zero.
  577. */
  578. void nfs4_purge_state_owners(struct nfs_server *server)
  579. {
  580. struct nfs_client *clp = server->nfs_client;
  581. struct nfs4_state_owner *sp, *tmp;
  582. LIST_HEAD(doomed);
  583. spin_lock(&clp->cl_lock);
  584. list_for_each_entry_safe(sp, tmp, &server->state_owners_lru, so_lru) {
  585. list_move(&sp->so_lru, &doomed);
  586. nfs4_remove_state_owner_locked(sp);
  587. }
  588. spin_unlock(&clp->cl_lock);
  589. list_for_each_entry_safe(sp, tmp, &doomed, so_lru) {
  590. list_del(&sp->so_lru);
  591. nfs4_free_state_owner(sp);
  592. }
  593. }
  594. static struct nfs4_state *
  595. nfs4_alloc_open_state(void)
  596. {
  597. struct nfs4_state *state;
  598. state = kzalloc(sizeof(*state), GFP_NOFS);
  599. if (!state)
  600. return NULL;
  601. atomic_set(&state->count, 1);
  602. INIT_LIST_HEAD(&state->lock_states);
  603. spin_lock_init(&state->state_lock);
  604. seqlock_init(&state->seqlock);
  605. return state;
  606. }
  607. void
  608. nfs4_state_set_mode_locked(struct nfs4_state *state, fmode_t fmode)
  609. {
  610. if (state->state == fmode)
  611. return;
  612. /* NB! List reordering - see the reclaim code for why. */
  613. if ((fmode & FMODE_WRITE) != (state->state & FMODE_WRITE)) {
  614. if (fmode & FMODE_WRITE)
  615. list_move(&state->open_states, &state->owner->so_states);
  616. else
  617. list_move_tail(&state->open_states, &state->owner->so_states);
  618. }
  619. state->state = fmode;
  620. }
  621. static struct nfs4_state *
  622. __nfs4_find_state_byowner(struct inode *inode, struct nfs4_state_owner *owner)
  623. {
  624. struct nfs_inode *nfsi = NFS_I(inode);
  625. struct nfs4_state *state;
  626. list_for_each_entry(state, &nfsi->open_states, inode_states) {
  627. if (state->owner != owner)
  628. continue;
  629. if (atomic_inc_not_zero(&state->count))
  630. return state;
  631. }
  632. return NULL;
  633. }
  634. static void
  635. nfs4_free_open_state(struct nfs4_state *state)
  636. {
  637. kfree(state);
  638. }
  639. struct nfs4_state *
  640. nfs4_get_open_state(struct inode *inode, struct nfs4_state_owner *owner)
  641. {
  642. struct nfs4_state *state, *new;
  643. struct nfs_inode *nfsi = NFS_I(inode);
  644. spin_lock(&inode->i_lock);
  645. state = __nfs4_find_state_byowner(inode, owner);
  646. spin_unlock(&inode->i_lock);
  647. if (state)
  648. goto out;
  649. new = nfs4_alloc_open_state();
  650. spin_lock(&owner->so_lock);
  651. spin_lock(&inode->i_lock);
  652. state = __nfs4_find_state_byowner(inode, owner);
  653. if (state == NULL && new != NULL) {
  654. state = new;
  655. state->owner = owner;
  656. atomic_inc(&owner->so_count);
  657. list_add(&state->inode_states, &nfsi->open_states);
  658. ihold(inode);
  659. state->inode = inode;
  660. spin_unlock(&inode->i_lock);
  661. /* Note: The reclaim code dictates that we add stateless
  662. * and read-only stateids to the end of the list */
  663. list_add_tail(&state->open_states, &owner->so_states);
  664. spin_unlock(&owner->so_lock);
  665. } else {
  666. spin_unlock(&inode->i_lock);
  667. spin_unlock(&owner->so_lock);
  668. if (new)
  669. nfs4_free_open_state(new);
  670. }
  671. out:
  672. return state;
  673. }
  674. void nfs4_put_open_state(struct nfs4_state *state)
  675. {
  676. struct inode *inode = state->inode;
  677. struct nfs4_state_owner *owner = state->owner;
  678. if (!atomic_dec_and_lock(&state->count, &owner->so_lock))
  679. return;
  680. spin_lock(&inode->i_lock);
  681. list_del(&state->inode_states);
  682. list_del(&state->open_states);
  683. spin_unlock(&inode->i_lock);
  684. spin_unlock(&owner->so_lock);
  685. iput(inode);
  686. nfs4_free_open_state(state);
  687. nfs4_put_state_owner(owner);
  688. }
  689. /*
  690. * Close the current file.
  691. */
  692. static void __nfs4_close(struct nfs4_state *state,
  693. fmode_t fmode, gfp_t gfp_mask, int wait)
  694. {
  695. struct nfs4_state_owner *owner = state->owner;
  696. int call_close = 0;
  697. fmode_t newstate;
  698. atomic_inc(&owner->so_count);
  699. /* Protect against nfs4_find_state() */
  700. spin_lock(&owner->so_lock);
  701. switch (fmode & (FMODE_READ | FMODE_WRITE)) {
  702. case FMODE_READ:
  703. state->n_rdonly--;
  704. break;
  705. case FMODE_WRITE:
  706. state->n_wronly--;
  707. break;
  708. case FMODE_READ|FMODE_WRITE:
  709. state->n_rdwr--;
  710. }
  711. newstate = FMODE_READ|FMODE_WRITE;
  712. if (state->n_rdwr == 0) {
  713. if (state->n_rdonly == 0) {
  714. newstate &= ~FMODE_READ;
  715. call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
  716. call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
  717. }
  718. if (state->n_wronly == 0) {
  719. newstate &= ~FMODE_WRITE;
  720. call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
  721. call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
  722. }
  723. if (newstate == 0)
  724. clear_bit(NFS_DELEGATED_STATE, &state->flags);
  725. }
  726. nfs4_state_set_mode_locked(state, newstate);
  727. spin_unlock(&owner->so_lock);
  728. if (!call_close) {
  729. nfs4_put_open_state(state);
  730. nfs4_put_state_owner(owner);
  731. } else
  732. nfs4_do_close(state, gfp_mask, wait);
  733. }
  734. void nfs4_close_state(struct nfs4_state *state, fmode_t fmode)
  735. {
  736. __nfs4_close(state, fmode, GFP_NOFS, 0);
  737. }
  738. void nfs4_close_sync(struct nfs4_state *state, fmode_t fmode)
  739. {
  740. __nfs4_close(state, fmode, GFP_KERNEL, 1);
  741. }
  742. /*
  743. * Search the state->lock_states for an existing lock_owner
  744. * that is compatible with current->files
  745. */
  746. static struct nfs4_lock_state *
  747. __nfs4_find_lock_state(struct nfs4_state *state, fl_owner_t fl_owner, pid_t fl_pid, unsigned int type)
  748. {
  749. struct nfs4_lock_state *pos;
  750. list_for_each_entry(pos, &state->lock_states, ls_locks) {
  751. if (type != NFS4_ANY_LOCK_TYPE && pos->ls_owner.lo_type != type)
  752. continue;
  753. switch (pos->ls_owner.lo_type) {
  754. case NFS4_POSIX_LOCK_TYPE:
  755. if (pos->ls_owner.lo_u.posix_owner != fl_owner)
  756. continue;
  757. break;
  758. case NFS4_FLOCK_LOCK_TYPE:
  759. if (pos->ls_owner.lo_u.flock_owner != fl_pid)
  760. continue;
  761. }
  762. atomic_inc(&pos->ls_count);
  763. return pos;
  764. }
  765. return NULL;
  766. }
  767. /*
  768. * Return a compatible lock_state. If no initialized lock_state structure
  769. * exists, return an uninitialized one.
  770. *
  771. */
  772. static struct nfs4_lock_state *nfs4_alloc_lock_state(struct nfs4_state *state, fl_owner_t fl_owner, pid_t fl_pid, unsigned int type)
  773. {
  774. struct nfs4_lock_state *lsp;
  775. struct nfs_server *server = state->owner->so_server;
  776. lsp = kzalloc(sizeof(*lsp), GFP_NOFS);
  777. if (lsp == NULL)
  778. return NULL;
  779. nfs4_init_seqid_counter(&lsp->ls_seqid);
  780. atomic_set(&lsp->ls_count, 1);
  781. lsp->ls_state = state;
  782. lsp->ls_owner.lo_type = type;
  783. switch (lsp->ls_owner.lo_type) {
  784. case NFS4_FLOCK_LOCK_TYPE:
  785. lsp->ls_owner.lo_u.flock_owner = fl_pid;
  786. break;
  787. case NFS4_POSIX_LOCK_TYPE:
  788. lsp->ls_owner.lo_u.posix_owner = fl_owner;
  789. break;
  790. default:
  791. goto out_free;
  792. }
  793. lsp->ls_seqid.owner_id = ida_simple_get(&server->lockowner_id, 0, 0, GFP_NOFS);
  794. if (lsp->ls_seqid.owner_id < 0)
  795. goto out_free;
  796. INIT_LIST_HEAD(&lsp->ls_locks);
  797. return lsp;
  798. out_free:
  799. kfree(lsp);
  800. return NULL;
  801. }
  802. void nfs4_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
  803. {
  804. ida_simple_remove(&server->lockowner_id, lsp->ls_seqid.owner_id);
  805. nfs4_destroy_seqid_counter(&lsp->ls_seqid);
  806. kfree(lsp);
  807. }
  808. /*
  809. * Return a compatible lock_state. If no initialized lock_state structure
  810. * exists, return an uninitialized one.
  811. *
  812. */
  813. static struct nfs4_lock_state *nfs4_get_lock_state(struct nfs4_state *state, fl_owner_t owner, pid_t pid, unsigned int type)
  814. {
  815. struct nfs4_lock_state *lsp, *new = NULL;
  816. for(;;) {
  817. spin_lock(&state->state_lock);
  818. lsp = __nfs4_find_lock_state(state, owner, pid, type);
  819. if (lsp != NULL)
  820. break;
  821. if (new != NULL) {
  822. list_add(&new->ls_locks, &state->lock_states);
  823. set_bit(LK_STATE_IN_USE, &state->flags);
  824. lsp = new;
  825. new = NULL;
  826. break;
  827. }
  828. spin_unlock(&state->state_lock);
  829. new = nfs4_alloc_lock_state(state, owner, pid, type);
  830. if (new == NULL)
  831. return NULL;
  832. }
  833. spin_unlock(&state->state_lock);
  834. if (new != NULL)
  835. nfs4_free_lock_state(state->owner->so_server, new);
  836. return lsp;
  837. }
  838. /*
  839. * Release reference to lock_state, and free it if we see that
  840. * it is no longer in use
  841. */
  842. void nfs4_put_lock_state(struct nfs4_lock_state *lsp)
  843. {
  844. struct nfs4_state *state;
  845. if (lsp == NULL)
  846. return;
  847. state = lsp->ls_state;
  848. if (!atomic_dec_and_lock(&lsp->ls_count, &state->state_lock))
  849. return;
  850. list_del(&lsp->ls_locks);
  851. if (list_empty(&state->lock_states))
  852. clear_bit(LK_STATE_IN_USE, &state->flags);
  853. spin_unlock(&state->state_lock);
  854. if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
  855. if (nfs4_release_lockowner(lsp) == 0)
  856. return;
  857. }
  858. nfs4_free_lock_state(lsp->ls_state->owner->so_server, lsp);
  859. }
  860. static void nfs4_fl_copy_lock(struct file_lock *dst, struct file_lock *src)
  861. {
  862. struct nfs4_lock_state *lsp = src->fl_u.nfs4_fl.owner;
  863. dst->fl_u.nfs4_fl.owner = lsp;
  864. atomic_inc(&lsp->ls_count);
  865. }
  866. static void nfs4_fl_release_lock(struct file_lock *fl)
  867. {
  868. nfs4_put_lock_state(fl->fl_u.nfs4_fl.owner);
  869. }
  870. static const struct file_lock_operations nfs4_fl_lock_ops = {
  871. .fl_copy_lock = nfs4_fl_copy_lock,
  872. .fl_release_private = nfs4_fl_release_lock,
  873. };
  874. int nfs4_set_lock_state(struct nfs4_state *state, struct file_lock *fl)
  875. {
  876. struct nfs4_lock_state *lsp;
  877. if (fl->fl_ops != NULL)
  878. return 0;
  879. if (fl->fl_flags & FL_POSIX)
  880. lsp = nfs4_get_lock_state(state, fl->fl_owner, 0, NFS4_POSIX_LOCK_TYPE);
  881. else if (fl->fl_flags & FL_FLOCK)
  882. lsp = nfs4_get_lock_state(state, NULL, fl->fl_pid,
  883. NFS4_FLOCK_LOCK_TYPE);
  884. else
  885. return -EINVAL;
  886. if (lsp == NULL)
  887. return -ENOMEM;
  888. fl->fl_u.nfs4_fl.owner = lsp;
  889. fl->fl_ops = &nfs4_fl_lock_ops;
  890. return 0;
  891. }
  892. static bool nfs4_copy_lock_stateid(nfs4_stateid *dst, struct nfs4_state *state,
  893. const struct nfs_lockowner *lockowner)
  894. {
  895. struct nfs4_lock_state *lsp;
  896. fl_owner_t fl_owner;
  897. pid_t fl_pid;
  898. bool ret = false;
  899. if (lockowner == NULL)
  900. goto out;
  901. if (test_bit(LK_STATE_IN_USE, &state->flags) == 0)
  902. goto out;
  903. fl_owner = lockowner->l_owner;
  904. fl_pid = lockowner->l_pid;
  905. spin_lock(&state->state_lock);
  906. lsp = __nfs4_find_lock_state(state, fl_owner, fl_pid, NFS4_ANY_LOCK_TYPE);
  907. if (lsp != NULL && test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0) {
  908. nfs4_stateid_copy(dst, &lsp->ls_stateid);
  909. ret = true;
  910. }
  911. spin_unlock(&state->state_lock);
  912. nfs4_put_lock_state(lsp);
  913. out:
  914. return ret;
  915. }
  916. static void nfs4_copy_open_stateid(nfs4_stateid *dst, struct nfs4_state *state)
  917. {
  918. int seq;
  919. do {
  920. seq = read_seqbegin(&state->seqlock);
  921. nfs4_stateid_copy(dst, &state->stateid);
  922. } while (read_seqretry(&state->seqlock, seq));
  923. }
  924. /*
  925. * Byte-range lock aware utility to initialize the stateid of read/write
  926. * requests.
  927. */
  928. void nfs4_select_rw_stateid(nfs4_stateid *dst, struct nfs4_state *state,
  929. fmode_t fmode, const struct nfs_lockowner *lockowner)
  930. {
  931. if (nfs4_copy_delegation_stateid(dst, state->inode, fmode))
  932. return;
  933. if (nfs4_copy_lock_stateid(dst, state, lockowner))
  934. return;
  935. nfs4_copy_open_stateid(dst, state);
  936. }
  937. struct nfs_seqid *nfs_alloc_seqid(struct nfs_seqid_counter *counter, gfp_t gfp_mask)
  938. {
  939. struct nfs_seqid *new;
  940. new = kmalloc(sizeof(*new), gfp_mask);
  941. if (new != NULL) {
  942. new->sequence = counter;
  943. INIT_LIST_HEAD(&new->list);
  944. new->task = NULL;
  945. }
  946. return new;
  947. }
  948. void nfs_release_seqid(struct nfs_seqid *seqid)
  949. {
  950. struct nfs_seqid_counter *sequence;
  951. if (list_empty(&seqid->list))
  952. return;
  953. sequence = seqid->sequence;
  954. spin_lock(&sequence->lock);
  955. list_del_init(&seqid->list);
  956. if (!list_empty(&sequence->list)) {
  957. struct nfs_seqid *next;
  958. next = list_first_entry(&sequence->list,
  959. struct nfs_seqid, list);
  960. rpc_wake_up_queued_task(&sequence->wait, next->task);
  961. }
  962. spin_unlock(&sequence->lock);
  963. }
  964. void nfs_free_seqid(struct nfs_seqid *seqid)
  965. {
  966. nfs_release_seqid(seqid);
  967. kfree(seqid);
  968. }
  969. /*
  970. * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or
  971. * failed with a seqid incrementing error -
  972. * see comments nfs_fs.h:seqid_mutating_error()
  973. */
  974. static void nfs_increment_seqid(int status, struct nfs_seqid *seqid)
  975. {
  976. switch (status) {
  977. case 0:
  978. break;
  979. case -NFS4ERR_BAD_SEQID:
  980. if (seqid->sequence->flags & NFS_SEQID_CONFIRMED)
  981. return;
  982. pr_warn_ratelimited("NFS: v4 server returned a bad"
  983. " sequence-id error on an"
  984. " unconfirmed sequence %p!\n",
  985. seqid->sequence);
  986. case -NFS4ERR_STALE_CLIENTID:
  987. case -NFS4ERR_STALE_STATEID:
  988. case -NFS4ERR_BAD_STATEID:
  989. case -NFS4ERR_BADXDR:
  990. case -NFS4ERR_RESOURCE:
  991. case -NFS4ERR_NOFILEHANDLE:
  992. /* Non-seqid mutating errors */
  993. return;
  994. };
  995. /*
  996. * Note: no locking needed as we are guaranteed to be first
  997. * on the sequence list
  998. */
  999. seqid->sequence->counter++;
  1000. }
  1001. void nfs_increment_open_seqid(int status, struct nfs_seqid *seqid)
  1002. {
  1003. struct nfs4_state_owner *sp = container_of(seqid->sequence,
  1004. struct nfs4_state_owner, so_seqid);
  1005. struct nfs_server *server = sp->so_server;
  1006. if (status == -NFS4ERR_BAD_SEQID)
  1007. nfs4_drop_state_owner(sp);
  1008. if (!nfs4_has_session(server->nfs_client))
  1009. nfs_increment_seqid(status, seqid);
  1010. }
  1011. /*
  1012. * Increment the seqid if the LOCK/LOCKU succeeded, or
  1013. * failed with a seqid incrementing error -
  1014. * see comments nfs_fs.h:seqid_mutating_error()
  1015. */
  1016. void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid)
  1017. {
  1018. nfs_increment_seqid(status, seqid);
  1019. }
  1020. int nfs_wait_on_sequence(struct nfs_seqid *seqid, struct rpc_task *task)
  1021. {
  1022. struct nfs_seqid_counter *sequence = seqid->sequence;
  1023. int status = 0;
  1024. spin_lock(&sequence->lock);
  1025. seqid->task = task;
  1026. if (list_empty(&seqid->list))
  1027. list_add_tail(&seqid->list, &sequence->list);
  1028. if (list_first_entry(&sequence->list, struct nfs_seqid, list) == seqid)
  1029. goto unlock;
  1030. rpc_sleep_on(&sequence->wait, task, NULL);
  1031. status = -EAGAIN;
  1032. unlock:
  1033. spin_unlock(&sequence->lock);
  1034. return status;
  1035. }
  1036. static int nfs4_run_state_manager(void *);
  1037. static void nfs4_clear_state_manager_bit(struct nfs_client *clp)
  1038. {
  1039. smp_mb__before_clear_bit();
  1040. clear_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state);
  1041. smp_mb__after_clear_bit();
  1042. wake_up_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING);
  1043. rpc_wake_up(&clp->cl_rpcwaitq);
  1044. }
  1045. /*
  1046. * Schedule the nfs_client asynchronous state management routine
  1047. */
  1048. void nfs4_schedule_state_manager(struct nfs_client *clp)
  1049. {
  1050. struct task_struct *task;
  1051. char buf[INET6_ADDRSTRLEN + sizeof("-manager") + 1];
  1052. if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
  1053. return;
  1054. __module_get(THIS_MODULE);
  1055. atomic_inc(&clp->cl_count);
  1056. /* The rcu_read_lock() is not strictly necessary, as the state
  1057. * manager is the only thread that ever changes the rpc_xprt
  1058. * after it's initialized. At this point, we're single threaded. */
  1059. rcu_read_lock();
  1060. snprintf(buf, sizeof(buf), "%s-manager",
  1061. rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR));
  1062. rcu_read_unlock();
  1063. task = kthread_run(nfs4_run_state_manager, clp, buf);
  1064. if (IS_ERR(task)) {
  1065. printk(KERN_ERR "%s: kthread_run: %ld\n",
  1066. __func__, PTR_ERR(task));
  1067. nfs4_clear_state_manager_bit(clp);
  1068. nfs_put_client(clp);
  1069. module_put(THIS_MODULE);
  1070. }
  1071. }
  1072. /*
  1073. * Schedule a lease recovery attempt
  1074. */
  1075. void nfs4_schedule_lease_recovery(struct nfs_client *clp)
  1076. {
  1077. if (!clp)
  1078. return;
  1079. if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
  1080. set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
  1081. dprintk("%s: scheduling lease recovery for server %s\n", __func__,
  1082. clp->cl_hostname);
  1083. nfs4_schedule_state_manager(clp);
  1084. }
  1085. EXPORT_SYMBOL_GPL(nfs4_schedule_lease_recovery);
  1086. int nfs4_wait_clnt_recover(struct nfs_client *clp)
  1087. {
  1088. int res;
  1089. might_sleep();
  1090. res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
  1091. nfs_wait_bit_killable, TASK_KILLABLE);
  1092. if (res)
  1093. return res;
  1094. if (clp->cl_cons_state < 0)
  1095. return clp->cl_cons_state;
  1096. return 0;
  1097. }
  1098. int nfs4_client_recover_expired_lease(struct nfs_client *clp)
  1099. {
  1100. unsigned int loop;
  1101. int ret;
  1102. for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
  1103. ret = nfs4_wait_clnt_recover(clp);
  1104. if (ret != 0)
  1105. break;
  1106. if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
  1107. !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
  1108. break;
  1109. nfs4_schedule_state_manager(clp);
  1110. ret = -EIO;
  1111. }
  1112. return ret;
  1113. }
  1114. /*
  1115. * nfs40_handle_cb_pathdown - return all delegations after NFS4ERR_CB_PATH_DOWN
  1116. * @clp: client to process
  1117. *
  1118. * Set the NFS4CLNT_LEASE_EXPIRED state in order to force a
  1119. * resend of the SETCLIENTID and hence re-establish the
  1120. * callback channel. Then return all existing delegations.
  1121. */
  1122. static void nfs40_handle_cb_pathdown(struct nfs_client *clp)
  1123. {
  1124. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  1125. nfs_expire_all_delegations(clp);
  1126. dprintk("%s: handling CB_PATHDOWN recovery for server %s\n", __func__,
  1127. clp->cl_hostname);
  1128. }
  1129. void nfs4_schedule_path_down_recovery(struct nfs_client *clp)
  1130. {
  1131. nfs40_handle_cb_pathdown(clp);
  1132. nfs4_schedule_state_manager(clp);
  1133. }
  1134. static int nfs4_state_mark_reclaim_reboot(struct nfs_client *clp, struct nfs4_state *state)
  1135. {
  1136. set_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
  1137. /* Don't recover state that expired before the reboot */
  1138. if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags)) {
  1139. clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
  1140. return 0;
  1141. }
  1142. set_bit(NFS_OWNER_RECLAIM_REBOOT, &state->owner->so_flags);
  1143. set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
  1144. return 1;
  1145. }
  1146. static int nfs4_state_mark_reclaim_nograce(struct nfs_client *clp, struct nfs4_state *state)
  1147. {
  1148. set_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags);
  1149. clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
  1150. set_bit(NFS_OWNER_RECLAIM_NOGRACE, &state->owner->so_flags);
  1151. set_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state);
  1152. return 1;
  1153. }
  1154. void nfs4_schedule_stateid_recovery(const struct nfs_server *server, struct nfs4_state *state)
  1155. {
  1156. struct nfs_client *clp = server->nfs_client;
  1157. nfs4_state_mark_reclaim_nograce(clp, state);
  1158. dprintk("%s: scheduling stateid recovery for server %s\n", __func__,
  1159. clp->cl_hostname);
  1160. nfs4_schedule_state_manager(clp);
  1161. }
  1162. EXPORT_SYMBOL_GPL(nfs4_schedule_stateid_recovery);
  1163. void nfs_inode_find_state_and_recover(struct inode *inode,
  1164. const nfs4_stateid *stateid)
  1165. {
  1166. struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
  1167. struct nfs_inode *nfsi = NFS_I(inode);
  1168. struct nfs_open_context *ctx;
  1169. struct nfs4_state *state;
  1170. bool found = false;
  1171. spin_lock(&inode->i_lock);
  1172. list_for_each_entry(ctx, &nfsi->open_files, list) {
  1173. state = ctx->state;
  1174. if (state == NULL)
  1175. continue;
  1176. if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
  1177. continue;
  1178. if (!nfs4_stateid_match(&state->stateid, stateid))
  1179. continue;
  1180. nfs4_state_mark_reclaim_nograce(clp, state);
  1181. found = true;
  1182. }
  1183. spin_unlock(&inode->i_lock);
  1184. if (found)
  1185. nfs4_schedule_state_manager(clp);
  1186. }
  1187. static int nfs4_reclaim_locks(struct nfs4_state *state, const struct nfs4_state_recovery_ops *ops)
  1188. {
  1189. struct inode *inode = state->inode;
  1190. struct nfs_inode *nfsi = NFS_I(inode);
  1191. struct file_lock *fl;
  1192. int status = 0;
  1193. if (inode->i_flock == NULL)
  1194. return 0;
  1195. /* Guard against delegation returns and new lock/unlock calls */
  1196. down_write(&nfsi->rwsem);
  1197. /* Protect inode->i_flock using the BKL */
  1198. lock_flocks();
  1199. for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
  1200. if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
  1201. continue;
  1202. if (nfs_file_open_context(fl->fl_file)->state != state)
  1203. continue;
  1204. unlock_flocks();
  1205. status = ops->recover_lock(state, fl);
  1206. switch (status) {
  1207. case 0:
  1208. break;
  1209. case -ESTALE:
  1210. case -NFS4ERR_ADMIN_REVOKED:
  1211. case -NFS4ERR_STALE_STATEID:
  1212. case -NFS4ERR_BAD_STATEID:
  1213. case -NFS4ERR_EXPIRED:
  1214. case -NFS4ERR_NO_GRACE:
  1215. case -NFS4ERR_STALE_CLIENTID:
  1216. case -NFS4ERR_BADSESSION:
  1217. case -NFS4ERR_BADSLOT:
  1218. case -NFS4ERR_BAD_HIGH_SLOT:
  1219. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  1220. goto out;
  1221. default:
  1222. printk(KERN_ERR "NFS: %s: unhandled error %d. "
  1223. "Zeroing state\n", __func__, status);
  1224. case -ENOMEM:
  1225. case -NFS4ERR_DENIED:
  1226. case -NFS4ERR_RECLAIM_BAD:
  1227. case -NFS4ERR_RECLAIM_CONFLICT:
  1228. /* kill_proc(fl->fl_pid, SIGLOST, 1); */
  1229. status = 0;
  1230. }
  1231. lock_flocks();
  1232. }
  1233. unlock_flocks();
  1234. out:
  1235. up_write(&nfsi->rwsem);
  1236. return status;
  1237. }
  1238. static int nfs4_reclaim_open_state(struct nfs4_state_owner *sp, const struct nfs4_state_recovery_ops *ops)
  1239. {
  1240. struct nfs4_state *state;
  1241. struct nfs4_lock_state *lock;
  1242. int status = 0;
  1243. /* Note: we rely on the sp->so_states list being ordered
  1244. * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
  1245. * states first.
  1246. * This is needed to ensure that the server won't give us any
  1247. * read delegations that we have to return if, say, we are
  1248. * recovering after a network partition or a reboot from a
  1249. * server that doesn't support a grace period.
  1250. */
  1251. restart:
  1252. spin_lock(&sp->so_lock);
  1253. list_for_each_entry(state, &sp->so_states, open_states) {
  1254. if (!test_and_clear_bit(ops->state_flag_bit, &state->flags))
  1255. continue;
  1256. if (state->state == 0)
  1257. continue;
  1258. atomic_inc(&state->count);
  1259. spin_unlock(&sp->so_lock);
  1260. status = ops->recover_open(sp, state);
  1261. if (status >= 0) {
  1262. status = nfs4_reclaim_locks(state, ops);
  1263. if (status >= 0) {
  1264. spin_lock(&state->state_lock);
  1265. list_for_each_entry(lock, &state->lock_states, ls_locks) {
  1266. if (!test_bit(NFS_LOCK_INITIALIZED, &lock->ls_flags))
  1267. pr_warn_ratelimited("NFS: "
  1268. "%s: Lock reclaim "
  1269. "failed!\n", __func__);
  1270. }
  1271. spin_unlock(&state->state_lock);
  1272. nfs4_put_open_state(state);
  1273. goto restart;
  1274. }
  1275. }
  1276. switch (status) {
  1277. default:
  1278. printk(KERN_ERR "NFS: %s: unhandled error %d. "
  1279. "Zeroing state\n", __func__, status);
  1280. case -ENOENT:
  1281. case -ENOMEM:
  1282. case -ESTALE:
  1283. /*
  1284. * Open state on this file cannot be recovered
  1285. * All we can do is revert to using the zero stateid.
  1286. */
  1287. memset(&state->stateid, 0,
  1288. sizeof(state->stateid));
  1289. /* Mark the file as being 'closed' */
  1290. state->state = 0;
  1291. break;
  1292. case -EKEYEXPIRED:
  1293. /*
  1294. * User RPCSEC_GSS context has expired.
  1295. * We cannot recover this stateid now, so
  1296. * skip it and allow recovery thread to
  1297. * proceed.
  1298. */
  1299. break;
  1300. case -NFS4ERR_ADMIN_REVOKED:
  1301. case -NFS4ERR_STALE_STATEID:
  1302. case -NFS4ERR_BAD_STATEID:
  1303. case -NFS4ERR_RECLAIM_BAD:
  1304. case -NFS4ERR_RECLAIM_CONFLICT:
  1305. nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
  1306. break;
  1307. case -NFS4ERR_EXPIRED:
  1308. case -NFS4ERR_NO_GRACE:
  1309. nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
  1310. case -NFS4ERR_STALE_CLIENTID:
  1311. case -NFS4ERR_BADSESSION:
  1312. case -NFS4ERR_BADSLOT:
  1313. case -NFS4ERR_BAD_HIGH_SLOT:
  1314. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  1315. goto out_err;
  1316. }
  1317. nfs4_put_open_state(state);
  1318. goto restart;
  1319. }
  1320. spin_unlock(&sp->so_lock);
  1321. return 0;
  1322. out_err:
  1323. nfs4_put_open_state(state);
  1324. return status;
  1325. }
  1326. static void nfs4_clear_open_state(struct nfs4_state *state)
  1327. {
  1328. struct nfs4_lock_state *lock;
  1329. clear_bit(NFS_DELEGATED_STATE, &state->flags);
  1330. clear_bit(NFS_O_RDONLY_STATE, &state->flags);
  1331. clear_bit(NFS_O_WRONLY_STATE, &state->flags);
  1332. clear_bit(NFS_O_RDWR_STATE, &state->flags);
  1333. spin_lock(&state->state_lock);
  1334. list_for_each_entry(lock, &state->lock_states, ls_locks) {
  1335. lock->ls_seqid.flags = 0;
  1336. clear_bit(NFS_LOCK_INITIALIZED, &lock->ls_flags);
  1337. }
  1338. spin_unlock(&state->state_lock);
  1339. }
  1340. static void nfs4_reset_seqids(struct nfs_server *server,
  1341. int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
  1342. {
  1343. struct nfs_client *clp = server->nfs_client;
  1344. struct nfs4_state_owner *sp;
  1345. struct rb_node *pos;
  1346. struct nfs4_state *state;
  1347. spin_lock(&clp->cl_lock);
  1348. for (pos = rb_first(&server->state_owners);
  1349. pos != NULL;
  1350. pos = rb_next(pos)) {
  1351. sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
  1352. sp->so_seqid.flags = 0;
  1353. spin_lock(&sp->so_lock);
  1354. list_for_each_entry(state, &sp->so_states, open_states) {
  1355. if (mark_reclaim(clp, state))
  1356. nfs4_clear_open_state(state);
  1357. }
  1358. spin_unlock(&sp->so_lock);
  1359. }
  1360. spin_unlock(&clp->cl_lock);
  1361. }
  1362. static void nfs4_state_mark_reclaim_helper(struct nfs_client *clp,
  1363. int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
  1364. {
  1365. struct nfs_server *server;
  1366. rcu_read_lock();
  1367. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  1368. nfs4_reset_seqids(server, mark_reclaim);
  1369. rcu_read_unlock();
  1370. }
  1371. static void nfs4_state_start_reclaim_reboot(struct nfs_client *clp)
  1372. {
  1373. /* Mark all delegations for reclaim */
  1374. nfs_delegation_mark_reclaim(clp);
  1375. nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_reboot);
  1376. }
  1377. static void nfs4_reclaim_complete(struct nfs_client *clp,
  1378. const struct nfs4_state_recovery_ops *ops)
  1379. {
  1380. /* Notify the server we're done reclaiming our state */
  1381. if (ops->reclaim_complete)
  1382. (void)ops->reclaim_complete(clp);
  1383. }
  1384. static void nfs4_clear_reclaim_server(struct nfs_server *server)
  1385. {
  1386. struct nfs_client *clp = server->nfs_client;
  1387. struct nfs4_state_owner *sp;
  1388. struct rb_node *pos;
  1389. struct nfs4_state *state;
  1390. spin_lock(&clp->cl_lock);
  1391. for (pos = rb_first(&server->state_owners);
  1392. pos != NULL;
  1393. pos = rb_next(pos)) {
  1394. sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
  1395. spin_lock(&sp->so_lock);
  1396. list_for_each_entry(state, &sp->so_states, open_states) {
  1397. if (!test_and_clear_bit(NFS_STATE_RECLAIM_REBOOT,
  1398. &state->flags))
  1399. continue;
  1400. nfs4_state_mark_reclaim_nograce(clp, state);
  1401. }
  1402. spin_unlock(&sp->so_lock);
  1403. }
  1404. spin_unlock(&clp->cl_lock);
  1405. }
  1406. static int nfs4_state_clear_reclaim_reboot(struct nfs_client *clp)
  1407. {
  1408. struct nfs_server *server;
  1409. if (!test_and_clear_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
  1410. return 0;
  1411. rcu_read_lock();
  1412. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  1413. nfs4_clear_reclaim_server(server);
  1414. rcu_read_unlock();
  1415. nfs_delegation_reap_unclaimed(clp);
  1416. return 1;
  1417. }
  1418. static void nfs4_state_end_reclaim_reboot(struct nfs_client *clp)
  1419. {
  1420. if (!nfs4_state_clear_reclaim_reboot(clp))
  1421. return;
  1422. nfs4_reclaim_complete(clp, clp->cl_mvops->reboot_recovery_ops);
  1423. }
  1424. static void nfs_delegation_clear_all(struct nfs_client *clp)
  1425. {
  1426. nfs_delegation_mark_reclaim(clp);
  1427. nfs_delegation_reap_unclaimed(clp);
  1428. }
  1429. static void nfs4_state_start_reclaim_nograce(struct nfs_client *clp)
  1430. {
  1431. nfs_delegation_clear_all(clp);
  1432. nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_nograce);
  1433. }
  1434. static void nfs4_warn_keyexpired(const char *s)
  1435. {
  1436. printk_ratelimited(KERN_WARNING "Error: state manager"
  1437. " encountered RPCSEC_GSS session"
  1438. " expired against NFSv4 server %s.\n",
  1439. s);
  1440. }
  1441. static int nfs4_recovery_handle_error(struct nfs_client *clp, int error)
  1442. {
  1443. switch (error) {
  1444. case 0:
  1445. break;
  1446. case -NFS4ERR_CB_PATH_DOWN:
  1447. nfs40_handle_cb_pathdown(clp);
  1448. break;
  1449. case -NFS4ERR_NO_GRACE:
  1450. nfs4_state_end_reclaim_reboot(clp);
  1451. break;
  1452. case -NFS4ERR_STALE_CLIENTID:
  1453. case -NFS4ERR_LEASE_MOVED:
  1454. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  1455. nfs4_state_clear_reclaim_reboot(clp);
  1456. nfs4_state_start_reclaim_reboot(clp);
  1457. break;
  1458. case -NFS4ERR_EXPIRED:
  1459. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  1460. nfs4_state_start_reclaim_nograce(clp);
  1461. break;
  1462. case -NFS4ERR_BADSESSION:
  1463. case -NFS4ERR_BADSLOT:
  1464. case -NFS4ERR_BAD_HIGH_SLOT:
  1465. case -NFS4ERR_DEADSESSION:
  1466. case -NFS4ERR_SEQ_FALSE_RETRY:
  1467. case -NFS4ERR_SEQ_MISORDERED:
  1468. set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
  1469. /* Zero session reset errors */
  1470. break;
  1471. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  1472. set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
  1473. break;
  1474. case -EKEYEXPIRED:
  1475. /* Nothing we can do */
  1476. nfs4_warn_keyexpired(clp->cl_hostname);
  1477. break;
  1478. default:
  1479. dprintk("%s: failed to handle error %d for server %s\n",
  1480. __func__, error, clp->cl_hostname);
  1481. return error;
  1482. }
  1483. dprintk("%s: handled error %d for server %s\n", __func__, error,
  1484. clp->cl_hostname);
  1485. return 0;
  1486. }
  1487. static int nfs4_do_reclaim(struct nfs_client *clp, const struct nfs4_state_recovery_ops *ops)
  1488. {
  1489. struct nfs4_state_owner *sp;
  1490. struct nfs_server *server;
  1491. struct rb_node *pos;
  1492. int status = 0;
  1493. restart:
  1494. rcu_read_lock();
  1495. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  1496. nfs4_purge_state_owners(server);
  1497. spin_lock(&clp->cl_lock);
  1498. for (pos = rb_first(&server->state_owners);
  1499. pos != NULL;
  1500. pos = rb_next(pos)) {
  1501. sp = rb_entry(pos,
  1502. struct nfs4_state_owner, so_server_node);
  1503. if (!test_and_clear_bit(ops->owner_flag_bit,
  1504. &sp->so_flags))
  1505. continue;
  1506. atomic_inc(&sp->so_count);
  1507. spin_unlock(&clp->cl_lock);
  1508. rcu_read_unlock();
  1509. status = nfs4_reclaim_open_state(sp, ops);
  1510. if (status < 0) {
  1511. set_bit(ops->owner_flag_bit, &sp->so_flags);
  1512. nfs4_put_state_owner(sp);
  1513. return nfs4_recovery_handle_error(clp, status);
  1514. }
  1515. nfs4_put_state_owner(sp);
  1516. goto restart;
  1517. }
  1518. spin_unlock(&clp->cl_lock);
  1519. }
  1520. rcu_read_unlock();
  1521. return status;
  1522. }
  1523. static int nfs4_check_lease(struct nfs_client *clp)
  1524. {
  1525. struct rpc_cred *cred;
  1526. const struct nfs4_state_maintenance_ops *ops =
  1527. clp->cl_mvops->state_renewal_ops;
  1528. int status;
  1529. /* Is the client already known to have an expired lease? */
  1530. if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
  1531. return 0;
  1532. spin_lock(&clp->cl_lock);
  1533. cred = ops->get_state_renewal_cred_locked(clp);
  1534. spin_unlock(&clp->cl_lock);
  1535. if (cred == NULL) {
  1536. cred = nfs4_get_setclientid_cred(clp);
  1537. status = -ENOKEY;
  1538. if (cred == NULL)
  1539. goto out;
  1540. }
  1541. status = ops->renew_lease(clp, cred);
  1542. put_rpccred(cred);
  1543. out:
  1544. return nfs4_recovery_handle_error(clp, status);
  1545. }
  1546. /* Set NFS4CLNT_LEASE_EXPIRED and reclaim reboot state for all v4.0 errors
  1547. * and for recoverable errors on EXCHANGE_ID for v4.1
  1548. */
  1549. static int nfs4_handle_reclaim_lease_error(struct nfs_client *clp, int status)
  1550. {
  1551. switch (status) {
  1552. case -NFS4ERR_SEQ_MISORDERED:
  1553. if (test_and_set_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state))
  1554. return -ESERVERFAULT;
  1555. /* Lease confirmation error: retry after purging the lease */
  1556. ssleep(1);
  1557. clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
  1558. break;
  1559. case -NFS4ERR_STALE_CLIENTID:
  1560. clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
  1561. nfs4_state_clear_reclaim_reboot(clp);
  1562. nfs4_state_start_reclaim_reboot(clp);
  1563. break;
  1564. case -NFS4ERR_CLID_INUSE:
  1565. pr_err("NFS: Server %s reports our clientid is in use\n",
  1566. clp->cl_hostname);
  1567. nfs_mark_client_ready(clp, -EPERM);
  1568. clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
  1569. return -EPERM;
  1570. case -EACCES:
  1571. if (clp->cl_machine_cred == NULL)
  1572. return -EACCES;
  1573. /* Handle case where the user hasn't set up machine creds */
  1574. nfs4_clear_machine_cred(clp);
  1575. case -NFS4ERR_DELAY:
  1576. case -ETIMEDOUT:
  1577. case -EAGAIN:
  1578. ssleep(1);
  1579. break;
  1580. case -NFS4ERR_MINOR_VERS_MISMATCH:
  1581. if (clp->cl_cons_state == NFS_CS_SESSION_INITING)
  1582. nfs_mark_client_ready(clp, -EPROTONOSUPPORT);
  1583. dprintk("%s: exit with error %d for server %s\n",
  1584. __func__, -EPROTONOSUPPORT, clp->cl_hostname);
  1585. return -EPROTONOSUPPORT;
  1586. case -EKEYEXPIRED:
  1587. nfs4_warn_keyexpired(clp->cl_hostname);
  1588. case -NFS4ERR_NOT_SAME: /* FixMe: implement recovery
  1589. * in nfs4_exchange_id */
  1590. default:
  1591. dprintk("%s: exit with error %d for server %s\n", __func__,
  1592. status, clp->cl_hostname);
  1593. return status;
  1594. }
  1595. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  1596. dprintk("%s: handled error %d for server %s\n", __func__, status,
  1597. clp->cl_hostname);
  1598. return 0;
  1599. }
  1600. static int nfs4_establish_lease(struct nfs_client *clp)
  1601. {
  1602. struct rpc_cred *cred;
  1603. const struct nfs4_state_recovery_ops *ops =
  1604. clp->cl_mvops->reboot_recovery_ops;
  1605. int status;
  1606. cred = ops->get_clid_cred(clp);
  1607. if (cred == NULL)
  1608. return -ENOENT;
  1609. status = ops->establish_clid(clp, cred);
  1610. put_rpccred(cred);
  1611. if (status != 0)
  1612. return status;
  1613. pnfs_destroy_all_layouts(clp);
  1614. return 0;
  1615. }
  1616. /*
  1617. * Returns zero or a negative errno. NFS4ERR values are converted
  1618. * to local errno values.
  1619. */
  1620. static int nfs4_reclaim_lease(struct nfs_client *clp)
  1621. {
  1622. int status;
  1623. status = nfs4_establish_lease(clp);
  1624. if (status < 0)
  1625. return nfs4_handle_reclaim_lease_error(clp, status);
  1626. if (test_and_clear_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state))
  1627. nfs4_state_start_reclaim_nograce(clp);
  1628. if (!test_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state))
  1629. set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
  1630. clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
  1631. clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  1632. return 0;
  1633. }
  1634. static int nfs4_purge_lease(struct nfs_client *clp)
  1635. {
  1636. int status;
  1637. status = nfs4_establish_lease(clp);
  1638. if (status < 0)
  1639. return nfs4_handle_reclaim_lease_error(clp, status);
  1640. clear_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state);
  1641. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  1642. nfs4_state_start_reclaim_nograce(clp);
  1643. return 0;
  1644. }
  1645. /**
  1646. * nfs4_discover_server_trunking - Detect server IP address trunking
  1647. *
  1648. * @clp: nfs_client under test
  1649. * @result: OUT: found nfs_client, or clp
  1650. *
  1651. * Returns zero or a negative errno. If zero is returned,
  1652. * an nfs_client pointer is planted in "result".
  1653. *
  1654. * Note: since we are invoked in process context, and
  1655. * not from inside the state manager, we cannot use
  1656. * nfs4_handle_reclaim_lease_error().
  1657. */
  1658. int nfs4_discover_server_trunking(struct nfs_client *clp,
  1659. struct nfs_client **result)
  1660. {
  1661. const struct nfs4_state_recovery_ops *ops =
  1662. clp->cl_mvops->reboot_recovery_ops;
  1663. rpc_authflavor_t *flavors, flav, save;
  1664. struct rpc_clnt *clnt;
  1665. struct rpc_cred *cred;
  1666. int i, len, status;
  1667. dprintk("NFS: %s: testing '%s'\n", __func__, clp->cl_hostname);
  1668. len = NFS_MAX_SECFLAVORS;
  1669. flavors = kcalloc(len, sizeof(*flavors), GFP_KERNEL);
  1670. if (flavors == NULL) {
  1671. status = -ENOMEM;
  1672. goto out;
  1673. }
  1674. len = rpcauth_list_flavors(flavors, len);
  1675. if (len < 0) {
  1676. status = len;
  1677. goto out_free;
  1678. }
  1679. clnt = clp->cl_rpcclient;
  1680. save = clnt->cl_auth->au_flavor;
  1681. i = 0;
  1682. mutex_lock(&nfs_clid_init_mutex);
  1683. status = -ENOENT;
  1684. again:
  1685. cred = ops->get_clid_cred(clp);
  1686. if (cred == NULL)
  1687. goto out_unlock;
  1688. status = ops->detect_trunking(clp, result, cred);
  1689. put_rpccred(cred);
  1690. switch (status) {
  1691. case 0:
  1692. break;
  1693. case -EACCES:
  1694. if (clp->cl_machine_cred == NULL)
  1695. break;
  1696. /* Handle case where the user hasn't set up machine creds */
  1697. nfs4_clear_machine_cred(clp);
  1698. case -NFS4ERR_DELAY:
  1699. case -ETIMEDOUT:
  1700. case -EAGAIN:
  1701. ssleep(1);
  1702. dprintk("NFS: %s after status %d, retrying\n",
  1703. __func__, status);
  1704. goto again;
  1705. case -NFS4ERR_CLID_INUSE:
  1706. case -NFS4ERR_WRONGSEC:
  1707. status = -EPERM;
  1708. if (i >= len)
  1709. break;
  1710. flav = flavors[i++];
  1711. if (flav == save)
  1712. flav = flavors[i++];
  1713. clnt = rpc_clone_client_set_auth(clnt, flav);
  1714. if (IS_ERR(clnt)) {
  1715. status = PTR_ERR(clnt);
  1716. break;
  1717. }
  1718. clp->cl_rpcclient = clnt;
  1719. goto again;
  1720. case -NFS4ERR_MINOR_VERS_MISMATCH:
  1721. status = -EPROTONOSUPPORT;
  1722. break;
  1723. case -EKEYEXPIRED:
  1724. nfs4_warn_keyexpired(clp->cl_hostname);
  1725. case -NFS4ERR_NOT_SAME: /* FixMe: implement recovery
  1726. * in nfs4_exchange_id */
  1727. status = -EKEYEXPIRED;
  1728. }
  1729. out_unlock:
  1730. mutex_unlock(&nfs_clid_init_mutex);
  1731. out_free:
  1732. kfree(flavors);
  1733. out:
  1734. dprintk("NFS: %s: status = %d\n", __func__, status);
  1735. return status;
  1736. }
  1737. #ifdef CONFIG_NFS_V4_1
  1738. void nfs4_schedule_session_recovery(struct nfs4_session *session, int err)
  1739. {
  1740. struct nfs_client *clp = session->clp;
  1741. switch (err) {
  1742. default:
  1743. set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
  1744. break;
  1745. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  1746. set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
  1747. }
  1748. nfs4_schedule_lease_recovery(clp);
  1749. }
  1750. EXPORT_SYMBOL_GPL(nfs4_schedule_session_recovery);
  1751. static void nfs41_ping_server(struct nfs_client *clp)
  1752. {
  1753. /* Use CHECK_LEASE to ping the server with a SEQUENCE */
  1754. set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
  1755. nfs4_schedule_state_manager(clp);
  1756. }
  1757. void nfs41_server_notify_target_slotid_update(struct nfs_client *clp)
  1758. {
  1759. nfs41_ping_server(clp);
  1760. }
  1761. void nfs41_server_notify_highest_slotid_update(struct nfs_client *clp)
  1762. {
  1763. nfs41_ping_server(clp);
  1764. }
  1765. static void nfs4_reset_all_state(struct nfs_client *clp)
  1766. {
  1767. if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
  1768. set_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state);
  1769. clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
  1770. nfs4_state_start_reclaim_nograce(clp);
  1771. dprintk("%s: scheduling reset of all state for server %s!\n",
  1772. __func__, clp->cl_hostname);
  1773. nfs4_schedule_state_manager(clp);
  1774. }
  1775. }
  1776. static void nfs41_handle_server_reboot(struct nfs_client *clp)
  1777. {
  1778. if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
  1779. nfs4_state_start_reclaim_reboot(clp);
  1780. dprintk("%s: server %s rebooted!\n", __func__,
  1781. clp->cl_hostname);
  1782. nfs4_schedule_state_manager(clp);
  1783. }
  1784. }
  1785. static void nfs41_handle_state_revoked(struct nfs_client *clp)
  1786. {
  1787. nfs4_reset_all_state(clp);
  1788. dprintk("%s: state revoked on server %s\n", __func__, clp->cl_hostname);
  1789. }
  1790. static void nfs41_handle_recallable_state_revoked(struct nfs_client *clp)
  1791. {
  1792. /* This will need to handle layouts too */
  1793. nfs_expire_all_delegations(clp);
  1794. dprintk("%s: Recallable state revoked on server %s!\n", __func__,
  1795. clp->cl_hostname);
  1796. }
  1797. static void nfs41_handle_backchannel_fault(struct nfs_client *clp)
  1798. {
  1799. nfs_expire_all_delegations(clp);
  1800. if (test_and_set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state) == 0)
  1801. nfs4_schedule_state_manager(clp);
  1802. dprintk("%s: server %s declared a backchannel fault\n", __func__,
  1803. clp->cl_hostname);
  1804. }
  1805. static void nfs41_handle_cb_path_down(struct nfs_client *clp)
  1806. {
  1807. if (test_and_set_bit(NFS4CLNT_BIND_CONN_TO_SESSION,
  1808. &clp->cl_state) == 0)
  1809. nfs4_schedule_state_manager(clp);
  1810. }
  1811. void nfs41_handle_sequence_flag_errors(struct nfs_client *clp, u32 flags)
  1812. {
  1813. if (!flags)
  1814. return;
  1815. dprintk("%s: \"%s\" (client ID %llx) flags=0x%08x\n",
  1816. __func__, clp->cl_hostname, clp->cl_clientid, flags);
  1817. if (flags & SEQ4_STATUS_RESTART_RECLAIM_NEEDED)
  1818. nfs41_handle_server_reboot(clp);
  1819. if (flags & (SEQ4_STATUS_EXPIRED_ALL_STATE_REVOKED |
  1820. SEQ4_STATUS_EXPIRED_SOME_STATE_REVOKED |
  1821. SEQ4_STATUS_ADMIN_STATE_REVOKED |
  1822. SEQ4_STATUS_LEASE_MOVED))
  1823. nfs41_handle_state_revoked(clp);
  1824. if (flags & SEQ4_STATUS_RECALLABLE_STATE_REVOKED)
  1825. nfs41_handle_recallable_state_revoked(clp);
  1826. if (flags & SEQ4_STATUS_BACKCHANNEL_FAULT)
  1827. nfs41_handle_backchannel_fault(clp);
  1828. else if (flags & (SEQ4_STATUS_CB_PATH_DOWN |
  1829. SEQ4_STATUS_CB_PATH_DOWN_SESSION))
  1830. nfs41_handle_cb_path_down(clp);
  1831. }
  1832. static int nfs4_reset_session(struct nfs_client *clp)
  1833. {
  1834. struct rpc_cred *cred;
  1835. int status;
  1836. if (!nfs4_has_session(clp))
  1837. return 0;
  1838. nfs4_begin_drain_session(clp);
  1839. cred = nfs4_get_exchange_id_cred(clp);
  1840. status = nfs4_proc_destroy_session(clp->cl_session, cred);
  1841. if (status && status != -NFS4ERR_BADSESSION &&
  1842. status != -NFS4ERR_DEADSESSION) {
  1843. status = nfs4_recovery_handle_error(clp, status);
  1844. goto out;
  1845. }
  1846. memset(clp->cl_session->sess_id.data, 0, NFS4_MAX_SESSIONID_LEN);
  1847. status = nfs4_proc_create_session(clp, cred);
  1848. if (status) {
  1849. dprintk("%s: session reset failed with status %d for server %s!\n",
  1850. __func__, status, clp->cl_hostname);
  1851. status = nfs4_handle_reclaim_lease_error(clp, status);
  1852. goto out;
  1853. }
  1854. nfs41_finish_session_reset(clp);
  1855. dprintk("%s: session reset was successful for server %s!\n",
  1856. __func__, clp->cl_hostname);
  1857. out:
  1858. if (cred)
  1859. put_rpccred(cred);
  1860. return status;
  1861. }
  1862. static int nfs4_bind_conn_to_session(struct nfs_client *clp)
  1863. {
  1864. struct rpc_cred *cred;
  1865. int ret;
  1866. if (!nfs4_has_session(clp))
  1867. return 0;
  1868. nfs4_begin_drain_session(clp);
  1869. cred = nfs4_get_exchange_id_cred(clp);
  1870. ret = nfs4_proc_bind_conn_to_session(clp, cred);
  1871. if (cred)
  1872. put_rpccred(cred);
  1873. clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
  1874. switch (ret) {
  1875. case 0:
  1876. dprintk("%s: bind_conn_to_session was successful for server %s!\n",
  1877. __func__, clp->cl_hostname);
  1878. break;
  1879. case -NFS4ERR_DELAY:
  1880. ssleep(1);
  1881. set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
  1882. break;
  1883. default:
  1884. return nfs4_recovery_handle_error(clp, ret);
  1885. }
  1886. return 0;
  1887. }
  1888. #else /* CONFIG_NFS_V4_1 */
  1889. static int nfs4_reset_session(struct nfs_client *clp) { return 0; }
  1890. static int nfs4_end_drain_session(struct nfs_client *clp) { return 0; }
  1891. static int nfs4_bind_conn_to_session(struct nfs_client *clp)
  1892. {
  1893. return 0;
  1894. }
  1895. #endif /* CONFIG_NFS_V4_1 */
  1896. static void nfs4_state_manager(struct nfs_client *clp)
  1897. {
  1898. int status = 0;
  1899. const char *section = "", *section_sep = "";
  1900. /* Ensure exclusive access to NFSv4 state */
  1901. do {
  1902. if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
  1903. section = "purge state";
  1904. status = nfs4_purge_lease(clp);
  1905. if (status < 0)
  1906. goto out_error;
  1907. continue;
  1908. }
  1909. if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state)) {
  1910. section = "lease expired";
  1911. /* We're going to have to re-establish a clientid */
  1912. status = nfs4_reclaim_lease(clp);
  1913. if (status < 0)
  1914. goto out_error;
  1915. continue;
  1916. }
  1917. /* Initialize or reset the session */
  1918. if (test_and_clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state)) {
  1919. section = "reset session";
  1920. status = nfs4_reset_session(clp);
  1921. if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
  1922. continue;
  1923. if (status < 0)
  1924. goto out_error;
  1925. }
  1926. /* Send BIND_CONN_TO_SESSION */
  1927. if (test_and_clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION,
  1928. &clp->cl_state)) {
  1929. section = "bind conn to session";
  1930. status = nfs4_bind_conn_to_session(clp);
  1931. if (status < 0)
  1932. goto out_error;
  1933. continue;
  1934. }
  1935. if (test_and_clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state)) {
  1936. section = "check lease";
  1937. status = nfs4_check_lease(clp);
  1938. if (status < 0)
  1939. goto out_error;
  1940. continue;
  1941. }
  1942. /* First recover reboot state... */
  1943. if (test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state)) {
  1944. section = "reclaim reboot";
  1945. status = nfs4_do_reclaim(clp,
  1946. clp->cl_mvops->reboot_recovery_ops);
  1947. if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) ||
  1948. test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state))
  1949. continue;
  1950. nfs4_state_end_reclaim_reboot(clp);
  1951. if (test_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state))
  1952. continue;
  1953. if (status < 0)
  1954. goto out_error;
  1955. }
  1956. /* Now recover expired state... */
  1957. if (test_and_clear_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state)) {
  1958. section = "reclaim nograce";
  1959. status = nfs4_do_reclaim(clp,
  1960. clp->cl_mvops->nograce_recovery_ops);
  1961. if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) ||
  1962. test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state) ||
  1963. test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
  1964. continue;
  1965. if (status < 0)
  1966. goto out_error;
  1967. }
  1968. nfs4_end_drain_session(clp);
  1969. if (test_and_clear_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state)) {
  1970. nfs_client_return_marked_delegations(clp);
  1971. continue;
  1972. }
  1973. nfs4_clear_state_manager_bit(clp);
  1974. /* Did we race with an attempt to give us more work? */
  1975. if (clp->cl_state == 0)
  1976. break;
  1977. if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
  1978. break;
  1979. } while (atomic_read(&clp->cl_count) > 1);
  1980. return;
  1981. out_error:
  1982. if (strlen(section))
  1983. section_sep = ": ";
  1984. pr_warn_ratelimited("NFS: state manager%s%s failed on NFSv4 server %s"
  1985. " with error %d\n", section_sep, section,
  1986. clp->cl_hostname, -status);
  1987. ssleep(1);
  1988. nfs4_end_drain_session(clp);
  1989. nfs4_clear_state_manager_bit(clp);
  1990. }
  1991. static int nfs4_run_state_manager(void *ptr)
  1992. {
  1993. struct nfs_client *clp = ptr;
  1994. allow_signal(SIGKILL);
  1995. nfs4_state_manager(clp);
  1996. nfs_put_client(clp);
  1997. module_put_and_exit(0);
  1998. return 0;
  1999. }
  2000. /*
  2001. * Local variables:
  2002. * c-basic-offset: 8
  2003. * End:
  2004. */