nfs4state.c 58 KB

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