nfs4state.c 64 KB

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