nfs4state.c 57 KB

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