nfs4state.c 63 KB

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