nfs4state.c 65 KB

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