nfs4state.c 59 KB

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