nfs4state.c 54 KB

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