nfs4state.c 59 KB

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