nfs4state.c 58 KB

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