nfs4state.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030
  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. bool roc = pnfs_roc(state->inode);
  652. nfs4_do_close(state, gfp_mask, wait, roc);
  653. }
  654. }
  655. void nfs4_close_state(struct nfs4_state *state, fmode_t fmode)
  656. {
  657. __nfs4_close(state, fmode, GFP_NOFS, 0);
  658. }
  659. void nfs4_close_sync(struct nfs4_state *state, fmode_t fmode)
  660. {
  661. __nfs4_close(state, fmode, GFP_KERNEL, 1);
  662. }
  663. /*
  664. * Search the state->lock_states for an existing lock_owner
  665. * that is compatible with current->files
  666. */
  667. static struct nfs4_lock_state *
  668. __nfs4_find_lock_state(struct nfs4_state *state, fl_owner_t fl_owner, pid_t fl_pid, unsigned int type)
  669. {
  670. struct nfs4_lock_state *pos;
  671. list_for_each_entry(pos, &state->lock_states, ls_locks) {
  672. if (type != NFS4_ANY_LOCK_TYPE && pos->ls_owner.lo_type != type)
  673. continue;
  674. switch (pos->ls_owner.lo_type) {
  675. case NFS4_POSIX_LOCK_TYPE:
  676. if (pos->ls_owner.lo_u.posix_owner != fl_owner)
  677. continue;
  678. break;
  679. case NFS4_FLOCK_LOCK_TYPE:
  680. if (pos->ls_owner.lo_u.flock_owner != fl_pid)
  681. continue;
  682. }
  683. atomic_inc(&pos->ls_count);
  684. return pos;
  685. }
  686. return NULL;
  687. }
  688. /*
  689. * Return a compatible lock_state. If no initialized lock_state structure
  690. * exists, return an uninitialized one.
  691. *
  692. */
  693. 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)
  694. {
  695. struct nfs4_lock_state *lsp;
  696. struct nfs_server *server = state->owner->so_server;
  697. lsp = kzalloc(sizeof(*lsp), GFP_NOFS);
  698. if (lsp == NULL)
  699. return NULL;
  700. nfs4_init_seqid_counter(&lsp->ls_seqid);
  701. atomic_set(&lsp->ls_count, 1);
  702. lsp->ls_state = state;
  703. lsp->ls_owner.lo_type = type;
  704. switch (lsp->ls_owner.lo_type) {
  705. case NFS4_FLOCK_LOCK_TYPE:
  706. lsp->ls_owner.lo_u.flock_owner = fl_pid;
  707. break;
  708. case NFS4_POSIX_LOCK_TYPE:
  709. lsp->ls_owner.lo_u.posix_owner = fl_owner;
  710. break;
  711. default:
  712. goto out_free;
  713. }
  714. lsp->ls_seqid.owner_id = ida_simple_get(&server->lockowner_id, 0, 0, GFP_NOFS);
  715. if (lsp->ls_seqid.owner_id < 0)
  716. goto out_free;
  717. INIT_LIST_HEAD(&lsp->ls_locks);
  718. return lsp;
  719. out_free:
  720. kfree(lsp);
  721. return NULL;
  722. }
  723. void nfs4_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
  724. {
  725. ida_simple_remove(&server->lockowner_id, lsp->ls_seqid.owner_id);
  726. nfs4_destroy_seqid_counter(&lsp->ls_seqid);
  727. kfree(lsp);
  728. }
  729. /*
  730. * Return a compatible lock_state. If no initialized lock_state structure
  731. * exists, return an uninitialized one.
  732. *
  733. */
  734. static struct nfs4_lock_state *nfs4_get_lock_state(struct nfs4_state *state, fl_owner_t owner, pid_t pid, unsigned int type)
  735. {
  736. struct nfs4_lock_state *lsp, *new = NULL;
  737. for(;;) {
  738. spin_lock(&state->state_lock);
  739. lsp = __nfs4_find_lock_state(state, owner, pid, type);
  740. if (lsp != NULL)
  741. break;
  742. if (new != NULL) {
  743. list_add(&new->ls_locks, &state->lock_states);
  744. set_bit(LK_STATE_IN_USE, &state->flags);
  745. lsp = new;
  746. new = NULL;
  747. break;
  748. }
  749. spin_unlock(&state->state_lock);
  750. new = nfs4_alloc_lock_state(state, owner, pid, type);
  751. if (new == NULL)
  752. return NULL;
  753. }
  754. spin_unlock(&state->state_lock);
  755. if (new != NULL)
  756. nfs4_free_lock_state(state->owner->so_server, new);
  757. return lsp;
  758. }
  759. /*
  760. * Release reference to lock_state, and free it if we see that
  761. * it is no longer in use
  762. */
  763. void nfs4_put_lock_state(struct nfs4_lock_state *lsp)
  764. {
  765. struct nfs4_state *state;
  766. if (lsp == NULL)
  767. return;
  768. state = lsp->ls_state;
  769. if (!atomic_dec_and_lock(&lsp->ls_count, &state->state_lock))
  770. return;
  771. list_del(&lsp->ls_locks);
  772. if (list_empty(&state->lock_states))
  773. clear_bit(LK_STATE_IN_USE, &state->flags);
  774. spin_unlock(&state->state_lock);
  775. if (lsp->ls_flags & NFS_LOCK_INITIALIZED) {
  776. if (nfs4_release_lockowner(lsp) == 0)
  777. return;
  778. }
  779. nfs4_free_lock_state(lsp->ls_state->owner->so_server, lsp);
  780. }
  781. static void nfs4_fl_copy_lock(struct file_lock *dst, struct file_lock *src)
  782. {
  783. struct nfs4_lock_state *lsp = src->fl_u.nfs4_fl.owner;
  784. dst->fl_u.nfs4_fl.owner = lsp;
  785. atomic_inc(&lsp->ls_count);
  786. }
  787. static void nfs4_fl_release_lock(struct file_lock *fl)
  788. {
  789. nfs4_put_lock_state(fl->fl_u.nfs4_fl.owner);
  790. }
  791. static const struct file_lock_operations nfs4_fl_lock_ops = {
  792. .fl_copy_lock = nfs4_fl_copy_lock,
  793. .fl_release_private = nfs4_fl_release_lock,
  794. };
  795. int nfs4_set_lock_state(struct nfs4_state *state, struct file_lock *fl)
  796. {
  797. struct nfs4_lock_state *lsp;
  798. if (fl->fl_ops != NULL)
  799. return 0;
  800. if (fl->fl_flags & FL_POSIX)
  801. lsp = nfs4_get_lock_state(state, fl->fl_owner, 0, NFS4_POSIX_LOCK_TYPE);
  802. else if (fl->fl_flags & FL_FLOCK)
  803. lsp = nfs4_get_lock_state(state, NULL, fl->fl_pid,
  804. NFS4_FLOCK_LOCK_TYPE);
  805. else
  806. return -EINVAL;
  807. if (lsp == NULL)
  808. return -ENOMEM;
  809. fl->fl_u.nfs4_fl.owner = lsp;
  810. fl->fl_ops = &nfs4_fl_lock_ops;
  811. return 0;
  812. }
  813. static bool nfs4_copy_lock_stateid(nfs4_stateid *dst, struct nfs4_state *state,
  814. fl_owner_t fl_owner, pid_t fl_pid)
  815. {
  816. struct nfs4_lock_state *lsp;
  817. bool ret = false;
  818. if (test_bit(LK_STATE_IN_USE, &state->flags) == 0)
  819. goto out;
  820. spin_lock(&state->state_lock);
  821. lsp = __nfs4_find_lock_state(state, fl_owner, fl_pid, NFS4_ANY_LOCK_TYPE);
  822. if (lsp != NULL && (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0) {
  823. nfs4_stateid_copy(dst, &lsp->ls_stateid);
  824. ret = true;
  825. }
  826. spin_unlock(&state->state_lock);
  827. nfs4_put_lock_state(lsp);
  828. out:
  829. return ret;
  830. }
  831. static void nfs4_copy_open_stateid(nfs4_stateid *dst, struct nfs4_state *state)
  832. {
  833. int seq;
  834. do {
  835. seq = read_seqbegin(&state->seqlock);
  836. nfs4_stateid_copy(dst, &state->stateid);
  837. } while (read_seqretry(&state->seqlock, seq));
  838. }
  839. /*
  840. * Byte-range lock aware utility to initialize the stateid of read/write
  841. * requests.
  842. */
  843. void nfs4_select_rw_stateid(nfs4_stateid *dst, struct nfs4_state *state,
  844. fmode_t fmode, fl_owner_t fl_owner, pid_t fl_pid)
  845. {
  846. if (nfs4_copy_delegation_stateid(dst, state->inode, fmode))
  847. return;
  848. if (nfs4_copy_lock_stateid(dst, state, fl_owner, fl_pid))
  849. return;
  850. nfs4_copy_open_stateid(dst, state);
  851. }
  852. struct nfs_seqid *nfs_alloc_seqid(struct nfs_seqid_counter *counter, gfp_t gfp_mask)
  853. {
  854. struct nfs_seqid *new;
  855. new = kmalloc(sizeof(*new), gfp_mask);
  856. if (new != NULL) {
  857. new->sequence = counter;
  858. INIT_LIST_HEAD(&new->list);
  859. new->task = NULL;
  860. }
  861. return new;
  862. }
  863. void nfs_release_seqid(struct nfs_seqid *seqid)
  864. {
  865. struct nfs_seqid_counter *sequence;
  866. if (list_empty(&seqid->list))
  867. return;
  868. sequence = seqid->sequence;
  869. spin_lock(&sequence->lock);
  870. list_del_init(&seqid->list);
  871. if (!list_empty(&sequence->list)) {
  872. struct nfs_seqid *next;
  873. next = list_first_entry(&sequence->list,
  874. struct nfs_seqid, list);
  875. rpc_wake_up_queued_task(&sequence->wait, next->task);
  876. }
  877. spin_unlock(&sequence->lock);
  878. }
  879. void nfs_free_seqid(struct nfs_seqid *seqid)
  880. {
  881. nfs_release_seqid(seqid);
  882. kfree(seqid);
  883. }
  884. /*
  885. * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or
  886. * failed with a seqid incrementing error -
  887. * see comments nfs_fs.h:seqid_mutating_error()
  888. */
  889. static void nfs_increment_seqid(int status, struct nfs_seqid *seqid)
  890. {
  891. BUG_ON(list_first_entry(&seqid->sequence->list, struct nfs_seqid, list) != seqid);
  892. switch (status) {
  893. case 0:
  894. break;
  895. case -NFS4ERR_BAD_SEQID:
  896. if (seqid->sequence->flags & NFS_SEQID_CONFIRMED)
  897. return;
  898. pr_warn_ratelimited("NFS: v4 server returned a bad"
  899. " sequence-id error on an"
  900. " unconfirmed sequence %p!\n",
  901. seqid->sequence);
  902. case -NFS4ERR_STALE_CLIENTID:
  903. case -NFS4ERR_STALE_STATEID:
  904. case -NFS4ERR_BAD_STATEID:
  905. case -NFS4ERR_BADXDR:
  906. case -NFS4ERR_RESOURCE:
  907. case -NFS4ERR_NOFILEHANDLE:
  908. /* Non-seqid mutating errors */
  909. return;
  910. };
  911. /*
  912. * Note: no locking needed as we are guaranteed to be first
  913. * on the sequence list
  914. */
  915. seqid->sequence->counter++;
  916. }
  917. void nfs_increment_open_seqid(int status, struct nfs_seqid *seqid)
  918. {
  919. struct nfs4_state_owner *sp = container_of(seqid->sequence,
  920. struct nfs4_state_owner, so_seqid);
  921. struct nfs_server *server = sp->so_server;
  922. if (status == -NFS4ERR_BAD_SEQID)
  923. nfs4_drop_state_owner(sp);
  924. if (!nfs4_has_session(server->nfs_client))
  925. nfs_increment_seqid(status, seqid);
  926. }
  927. /*
  928. * Increment the seqid if the LOCK/LOCKU succeeded, or
  929. * failed with a seqid incrementing error -
  930. * see comments nfs_fs.h:seqid_mutating_error()
  931. */
  932. void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid)
  933. {
  934. nfs_increment_seqid(status, seqid);
  935. }
  936. int nfs_wait_on_sequence(struct nfs_seqid *seqid, struct rpc_task *task)
  937. {
  938. struct nfs_seqid_counter *sequence = seqid->sequence;
  939. int status = 0;
  940. spin_lock(&sequence->lock);
  941. seqid->task = task;
  942. if (list_empty(&seqid->list))
  943. list_add_tail(&seqid->list, &sequence->list);
  944. if (list_first_entry(&sequence->list, struct nfs_seqid, list) == seqid)
  945. goto unlock;
  946. rpc_sleep_on(&sequence->wait, task, NULL);
  947. status = -EAGAIN;
  948. unlock:
  949. spin_unlock(&sequence->lock);
  950. return status;
  951. }
  952. static int nfs4_run_state_manager(void *);
  953. static void nfs4_clear_state_manager_bit(struct nfs_client *clp)
  954. {
  955. smp_mb__before_clear_bit();
  956. clear_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state);
  957. smp_mb__after_clear_bit();
  958. wake_up_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING);
  959. rpc_wake_up(&clp->cl_rpcwaitq);
  960. }
  961. /*
  962. * Schedule the nfs_client asynchronous state management routine
  963. */
  964. void nfs4_schedule_state_manager(struct nfs_client *clp)
  965. {
  966. struct task_struct *task;
  967. char buf[INET6_ADDRSTRLEN + sizeof("-manager") + 1];
  968. if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
  969. return;
  970. __module_get(THIS_MODULE);
  971. atomic_inc(&clp->cl_count);
  972. /* The rcu_read_lock() is not strictly necessary, as the state
  973. * manager is the only thread that ever changes the rpc_xprt
  974. * after it's initialized. At this point, we're single threaded. */
  975. rcu_read_lock();
  976. snprintf(buf, sizeof(buf), "%s-manager",
  977. rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR));
  978. rcu_read_unlock();
  979. task = kthread_run(nfs4_run_state_manager, clp, buf);
  980. if (IS_ERR(task)) {
  981. printk(KERN_ERR "%s: kthread_run: %ld\n",
  982. __func__, PTR_ERR(task));
  983. nfs4_clear_state_manager_bit(clp);
  984. nfs_put_client(clp);
  985. module_put(THIS_MODULE);
  986. }
  987. }
  988. /*
  989. * Schedule a lease recovery attempt
  990. */
  991. void nfs4_schedule_lease_recovery(struct nfs_client *clp)
  992. {
  993. if (!clp)
  994. return;
  995. if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
  996. set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
  997. dprintk("%s: scheduling lease recovery for server %s\n", __func__,
  998. clp->cl_hostname);
  999. nfs4_schedule_state_manager(clp);
  1000. }
  1001. EXPORT_SYMBOL_GPL(nfs4_schedule_lease_recovery);
  1002. /*
  1003. * nfs40_handle_cb_pathdown - return all delegations after NFS4ERR_CB_PATH_DOWN
  1004. * @clp: client to process
  1005. *
  1006. * Set the NFS4CLNT_LEASE_EXPIRED state in order to force a
  1007. * resend of the SETCLIENTID and hence re-establish the
  1008. * callback channel. Then return all existing delegations.
  1009. */
  1010. static void nfs40_handle_cb_pathdown(struct nfs_client *clp)
  1011. {
  1012. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  1013. nfs_expire_all_delegations(clp);
  1014. dprintk("%s: handling CB_PATHDOWN recovery for server %s\n", __func__,
  1015. clp->cl_hostname);
  1016. }
  1017. void nfs4_schedule_path_down_recovery(struct nfs_client *clp)
  1018. {
  1019. nfs40_handle_cb_pathdown(clp);
  1020. nfs4_schedule_state_manager(clp);
  1021. }
  1022. static int nfs4_state_mark_reclaim_reboot(struct nfs_client *clp, struct nfs4_state *state)
  1023. {
  1024. set_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
  1025. /* Don't recover state that expired before the reboot */
  1026. if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags)) {
  1027. clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
  1028. return 0;
  1029. }
  1030. set_bit(NFS_OWNER_RECLAIM_REBOOT, &state->owner->so_flags);
  1031. set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
  1032. return 1;
  1033. }
  1034. static int nfs4_state_mark_reclaim_nograce(struct nfs_client *clp, struct nfs4_state *state)
  1035. {
  1036. set_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags);
  1037. clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
  1038. set_bit(NFS_OWNER_RECLAIM_NOGRACE, &state->owner->so_flags);
  1039. set_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state);
  1040. return 1;
  1041. }
  1042. void nfs4_schedule_stateid_recovery(const struct nfs_server *server, struct nfs4_state *state)
  1043. {
  1044. struct nfs_client *clp = server->nfs_client;
  1045. nfs4_state_mark_reclaim_nograce(clp, state);
  1046. dprintk("%s: scheduling stateid recovery for server %s\n", __func__,
  1047. clp->cl_hostname);
  1048. nfs4_schedule_state_manager(clp);
  1049. }
  1050. EXPORT_SYMBOL_GPL(nfs4_schedule_stateid_recovery);
  1051. void nfs_inode_find_state_and_recover(struct inode *inode,
  1052. const nfs4_stateid *stateid)
  1053. {
  1054. struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
  1055. struct nfs_inode *nfsi = NFS_I(inode);
  1056. struct nfs_open_context *ctx;
  1057. struct nfs4_state *state;
  1058. bool found = false;
  1059. spin_lock(&inode->i_lock);
  1060. list_for_each_entry(ctx, &nfsi->open_files, list) {
  1061. state = ctx->state;
  1062. if (state == NULL)
  1063. continue;
  1064. if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
  1065. continue;
  1066. if (!nfs4_stateid_match(&state->stateid, stateid))
  1067. continue;
  1068. nfs4_state_mark_reclaim_nograce(clp, state);
  1069. found = true;
  1070. }
  1071. spin_unlock(&inode->i_lock);
  1072. if (found)
  1073. nfs4_schedule_state_manager(clp);
  1074. }
  1075. static int nfs4_reclaim_locks(struct nfs4_state *state, const struct nfs4_state_recovery_ops *ops)
  1076. {
  1077. struct inode *inode = state->inode;
  1078. struct nfs_inode *nfsi = NFS_I(inode);
  1079. struct file_lock *fl;
  1080. int status = 0;
  1081. if (inode->i_flock == NULL)
  1082. return 0;
  1083. /* Guard against delegation returns and new lock/unlock calls */
  1084. down_write(&nfsi->rwsem);
  1085. /* Protect inode->i_flock using the BKL */
  1086. lock_flocks();
  1087. for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
  1088. if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
  1089. continue;
  1090. if (nfs_file_open_context(fl->fl_file)->state != state)
  1091. continue;
  1092. unlock_flocks();
  1093. status = ops->recover_lock(state, fl);
  1094. switch (status) {
  1095. case 0:
  1096. break;
  1097. case -ESTALE:
  1098. case -NFS4ERR_ADMIN_REVOKED:
  1099. case -NFS4ERR_STALE_STATEID:
  1100. case -NFS4ERR_BAD_STATEID:
  1101. case -NFS4ERR_EXPIRED:
  1102. case -NFS4ERR_NO_GRACE:
  1103. case -NFS4ERR_STALE_CLIENTID:
  1104. case -NFS4ERR_BADSESSION:
  1105. case -NFS4ERR_BADSLOT:
  1106. case -NFS4ERR_BAD_HIGH_SLOT:
  1107. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  1108. goto out;
  1109. default:
  1110. printk(KERN_ERR "NFS: %s: unhandled error %d. "
  1111. "Zeroing state\n", __func__, status);
  1112. case -ENOMEM:
  1113. case -NFS4ERR_DENIED:
  1114. case -NFS4ERR_RECLAIM_BAD:
  1115. case -NFS4ERR_RECLAIM_CONFLICT:
  1116. /* kill_proc(fl->fl_pid, SIGLOST, 1); */
  1117. status = 0;
  1118. }
  1119. lock_flocks();
  1120. }
  1121. unlock_flocks();
  1122. out:
  1123. up_write(&nfsi->rwsem);
  1124. return status;
  1125. }
  1126. static int nfs4_reclaim_open_state(struct nfs4_state_owner *sp, const struct nfs4_state_recovery_ops *ops)
  1127. {
  1128. struct nfs4_state *state;
  1129. struct nfs4_lock_state *lock;
  1130. int status = 0;
  1131. /* Note: we rely on the sp->so_states list being ordered
  1132. * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
  1133. * states first.
  1134. * This is needed to ensure that the server won't give us any
  1135. * read delegations that we have to return if, say, we are
  1136. * recovering after a network partition or a reboot from a
  1137. * server that doesn't support a grace period.
  1138. */
  1139. restart:
  1140. spin_lock(&sp->so_lock);
  1141. list_for_each_entry(state, &sp->so_states, open_states) {
  1142. if (!test_and_clear_bit(ops->state_flag_bit, &state->flags))
  1143. continue;
  1144. if (state->state == 0)
  1145. continue;
  1146. atomic_inc(&state->count);
  1147. spin_unlock(&sp->so_lock);
  1148. status = ops->recover_open(sp, state);
  1149. if (status >= 0) {
  1150. status = nfs4_reclaim_locks(state, ops);
  1151. if (status >= 0) {
  1152. spin_lock(&state->state_lock);
  1153. list_for_each_entry(lock, &state->lock_states, ls_locks) {
  1154. if (!(lock->ls_flags & NFS_LOCK_INITIALIZED))
  1155. pr_warn_ratelimited("NFS: "
  1156. "%s: Lock reclaim "
  1157. "failed!\n", __func__);
  1158. }
  1159. spin_unlock(&state->state_lock);
  1160. nfs4_put_open_state(state);
  1161. goto restart;
  1162. }
  1163. }
  1164. switch (status) {
  1165. default:
  1166. printk(KERN_ERR "NFS: %s: unhandled error %d. "
  1167. "Zeroing state\n", __func__, status);
  1168. case -ENOENT:
  1169. case -ENOMEM:
  1170. case -ESTALE:
  1171. /*
  1172. * Open state on this file cannot be recovered
  1173. * All we can do is revert to using the zero stateid.
  1174. */
  1175. memset(&state->stateid, 0,
  1176. sizeof(state->stateid));
  1177. /* Mark the file as being 'closed' */
  1178. state->state = 0;
  1179. break;
  1180. case -EKEYEXPIRED:
  1181. /*
  1182. * User RPCSEC_GSS context has expired.
  1183. * We cannot recover this stateid now, so
  1184. * skip it and allow recovery thread to
  1185. * proceed.
  1186. */
  1187. break;
  1188. case -NFS4ERR_ADMIN_REVOKED:
  1189. case -NFS4ERR_STALE_STATEID:
  1190. case -NFS4ERR_BAD_STATEID:
  1191. case -NFS4ERR_RECLAIM_BAD:
  1192. case -NFS4ERR_RECLAIM_CONFLICT:
  1193. nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
  1194. break;
  1195. case -NFS4ERR_EXPIRED:
  1196. case -NFS4ERR_NO_GRACE:
  1197. nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
  1198. case -NFS4ERR_STALE_CLIENTID:
  1199. case -NFS4ERR_BADSESSION:
  1200. case -NFS4ERR_BADSLOT:
  1201. case -NFS4ERR_BAD_HIGH_SLOT:
  1202. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  1203. goto out_err;
  1204. }
  1205. nfs4_put_open_state(state);
  1206. goto restart;
  1207. }
  1208. spin_unlock(&sp->so_lock);
  1209. return 0;
  1210. out_err:
  1211. nfs4_put_open_state(state);
  1212. return status;
  1213. }
  1214. static void nfs4_clear_open_state(struct nfs4_state *state)
  1215. {
  1216. struct nfs4_lock_state *lock;
  1217. clear_bit(NFS_DELEGATED_STATE, &state->flags);
  1218. clear_bit(NFS_O_RDONLY_STATE, &state->flags);
  1219. clear_bit(NFS_O_WRONLY_STATE, &state->flags);
  1220. clear_bit(NFS_O_RDWR_STATE, &state->flags);
  1221. spin_lock(&state->state_lock);
  1222. list_for_each_entry(lock, &state->lock_states, ls_locks) {
  1223. lock->ls_seqid.flags = 0;
  1224. lock->ls_flags &= ~NFS_LOCK_INITIALIZED;
  1225. }
  1226. spin_unlock(&state->state_lock);
  1227. }
  1228. static void nfs4_reset_seqids(struct nfs_server *server,
  1229. int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
  1230. {
  1231. struct nfs_client *clp = server->nfs_client;
  1232. struct nfs4_state_owner *sp;
  1233. struct rb_node *pos;
  1234. struct nfs4_state *state;
  1235. spin_lock(&clp->cl_lock);
  1236. for (pos = rb_first(&server->state_owners);
  1237. pos != NULL;
  1238. pos = rb_next(pos)) {
  1239. sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
  1240. sp->so_seqid.flags = 0;
  1241. spin_lock(&sp->so_lock);
  1242. list_for_each_entry(state, &sp->so_states, open_states) {
  1243. if (mark_reclaim(clp, state))
  1244. nfs4_clear_open_state(state);
  1245. }
  1246. spin_unlock(&sp->so_lock);
  1247. }
  1248. spin_unlock(&clp->cl_lock);
  1249. }
  1250. static void nfs4_state_mark_reclaim_helper(struct nfs_client *clp,
  1251. int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
  1252. {
  1253. struct nfs_server *server;
  1254. rcu_read_lock();
  1255. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  1256. nfs4_reset_seqids(server, mark_reclaim);
  1257. rcu_read_unlock();
  1258. }
  1259. static void nfs4_state_start_reclaim_reboot(struct nfs_client *clp)
  1260. {
  1261. /* Mark all delegations for reclaim */
  1262. nfs_delegation_mark_reclaim(clp);
  1263. nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_reboot);
  1264. }
  1265. static void nfs4_reclaim_complete(struct nfs_client *clp,
  1266. const struct nfs4_state_recovery_ops *ops)
  1267. {
  1268. /* Notify the server we're done reclaiming our state */
  1269. if (ops->reclaim_complete)
  1270. (void)ops->reclaim_complete(clp);
  1271. }
  1272. static void nfs4_clear_reclaim_server(struct nfs_server *server)
  1273. {
  1274. struct nfs_client *clp = server->nfs_client;
  1275. struct nfs4_state_owner *sp;
  1276. struct rb_node *pos;
  1277. struct nfs4_state *state;
  1278. spin_lock(&clp->cl_lock);
  1279. for (pos = rb_first(&server->state_owners);
  1280. pos != NULL;
  1281. pos = rb_next(pos)) {
  1282. sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
  1283. spin_lock(&sp->so_lock);
  1284. list_for_each_entry(state, &sp->so_states, open_states) {
  1285. if (!test_and_clear_bit(NFS_STATE_RECLAIM_REBOOT,
  1286. &state->flags))
  1287. continue;
  1288. nfs4_state_mark_reclaim_nograce(clp, state);
  1289. }
  1290. spin_unlock(&sp->so_lock);
  1291. }
  1292. spin_unlock(&clp->cl_lock);
  1293. }
  1294. static int nfs4_state_clear_reclaim_reboot(struct nfs_client *clp)
  1295. {
  1296. struct nfs_server *server;
  1297. if (!test_and_clear_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
  1298. return 0;
  1299. rcu_read_lock();
  1300. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  1301. nfs4_clear_reclaim_server(server);
  1302. rcu_read_unlock();
  1303. nfs_delegation_reap_unclaimed(clp);
  1304. return 1;
  1305. }
  1306. static void nfs4_state_end_reclaim_reboot(struct nfs_client *clp)
  1307. {
  1308. if (!nfs4_state_clear_reclaim_reboot(clp))
  1309. return;
  1310. nfs4_reclaim_complete(clp, clp->cl_mvops->reboot_recovery_ops);
  1311. }
  1312. static void nfs_delegation_clear_all(struct nfs_client *clp)
  1313. {
  1314. nfs_delegation_mark_reclaim(clp);
  1315. nfs_delegation_reap_unclaimed(clp);
  1316. }
  1317. static void nfs4_state_start_reclaim_nograce(struct nfs_client *clp)
  1318. {
  1319. nfs_delegation_clear_all(clp);
  1320. nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_nograce);
  1321. }
  1322. static void nfs4_warn_keyexpired(const char *s)
  1323. {
  1324. printk_ratelimited(KERN_WARNING "Error: state manager"
  1325. " encountered RPCSEC_GSS session"
  1326. " expired against NFSv4 server %s.\n",
  1327. s);
  1328. }
  1329. static int nfs4_recovery_handle_error(struct nfs_client *clp, int error)
  1330. {
  1331. switch (error) {
  1332. case 0:
  1333. break;
  1334. case -NFS4ERR_CB_PATH_DOWN:
  1335. nfs40_handle_cb_pathdown(clp);
  1336. break;
  1337. case -NFS4ERR_NO_GRACE:
  1338. nfs4_state_end_reclaim_reboot(clp);
  1339. break;
  1340. case -NFS4ERR_STALE_CLIENTID:
  1341. case -NFS4ERR_LEASE_MOVED:
  1342. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  1343. nfs4_state_clear_reclaim_reboot(clp);
  1344. nfs4_state_start_reclaim_reboot(clp);
  1345. break;
  1346. case -NFS4ERR_EXPIRED:
  1347. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  1348. nfs4_state_start_reclaim_nograce(clp);
  1349. break;
  1350. case -NFS4ERR_BADSESSION:
  1351. case -NFS4ERR_BADSLOT:
  1352. case -NFS4ERR_BAD_HIGH_SLOT:
  1353. case -NFS4ERR_DEADSESSION:
  1354. case -NFS4ERR_SEQ_FALSE_RETRY:
  1355. case -NFS4ERR_SEQ_MISORDERED:
  1356. set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
  1357. /* Zero session reset errors */
  1358. break;
  1359. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  1360. set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
  1361. break;
  1362. case -EKEYEXPIRED:
  1363. /* Nothing we can do */
  1364. nfs4_warn_keyexpired(clp->cl_hostname);
  1365. break;
  1366. default:
  1367. dprintk("%s: failed to handle error %d for server %s\n",
  1368. __func__, error, clp->cl_hostname);
  1369. return error;
  1370. }
  1371. dprintk("%s: handled error %d for server %s\n", __func__, error,
  1372. clp->cl_hostname);
  1373. return 0;
  1374. }
  1375. static int nfs4_do_reclaim(struct nfs_client *clp, const struct nfs4_state_recovery_ops *ops)
  1376. {
  1377. struct nfs4_state_owner *sp;
  1378. struct nfs_server *server;
  1379. struct rb_node *pos;
  1380. int status = 0;
  1381. restart:
  1382. rcu_read_lock();
  1383. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  1384. nfs4_purge_state_owners(server);
  1385. spin_lock(&clp->cl_lock);
  1386. for (pos = rb_first(&server->state_owners);
  1387. pos != NULL;
  1388. pos = rb_next(pos)) {
  1389. sp = rb_entry(pos,
  1390. struct nfs4_state_owner, so_server_node);
  1391. if (!test_and_clear_bit(ops->owner_flag_bit,
  1392. &sp->so_flags))
  1393. continue;
  1394. atomic_inc(&sp->so_count);
  1395. spin_unlock(&clp->cl_lock);
  1396. rcu_read_unlock();
  1397. status = nfs4_reclaim_open_state(sp, ops);
  1398. if (status < 0) {
  1399. set_bit(ops->owner_flag_bit, &sp->so_flags);
  1400. nfs4_put_state_owner(sp);
  1401. return nfs4_recovery_handle_error(clp, status);
  1402. }
  1403. nfs4_put_state_owner(sp);
  1404. goto restart;
  1405. }
  1406. spin_unlock(&clp->cl_lock);
  1407. }
  1408. rcu_read_unlock();
  1409. return status;
  1410. }
  1411. static int nfs4_check_lease(struct nfs_client *clp)
  1412. {
  1413. struct rpc_cred *cred;
  1414. const struct nfs4_state_maintenance_ops *ops =
  1415. clp->cl_mvops->state_renewal_ops;
  1416. int status;
  1417. /* Is the client already known to have an expired lease? */
  1418. if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
  1419. return 0;
  1420. spin_lock(&clp->cl_lock);
  1421. cred = ops->get_state_renewal_cred_locked(clp);
  1422. spin_unlock(&clp->cl_lock);
  1423. if (cred == NULL) {
  1424. cred = nfs4_get_setclientid_cred(clp);
  1425. status = -ENOKEY;
  1426. if (cred == NULL)
  1427. goto out;
  1428. }
  1429. status = ops->renew_lease(clp, cred);
  1430. put_rpccred(cred);
  1431. out:
  1432. return nfs4_recovery_handle_error(clp, status);
  1433. }
  1434. /* Set NFS4CLNT_LEASE_EXPIRED for all v4.0 errors and for recoverable errors
  1435. * on EXCHANGE_ID for v4.1
  1436. */
  1437. static int nfs4_handle_reclaim_lease_error(struct nfs_client *clp, int status)
  1438. {
  1439. switch (status) {
  1440. case -NFS4ERR_SEQ_MISORDERED:
  1441. if (test_and_set_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state))
  1442. return -ESERVERFAULT;
  1443. /* Lease confirmation error: retry after purging the lease */
  1444. ssleep(1);
  1445. case -NFS4ERR_STALE_CLIENTID:
  1446. clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
  1447. break;
  1448. case -NFS4ERR_CLID_INUSE:
  1449. pr_err("NFS: Server %s reports our clientid is in use\n",
  1450. clp->cl_hostname);
  1451. nfs_mark_client_ready(clp, -EPERM);
  1452. clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
  1453. return -EPERM;
  1454. case -EACCES:
  1455. if (clp->cl_machine_cred == NULL)
  1456. return -EACCES;
  1457. /* Handle case where the user hasn't set up machine creds */
  1458. nfs4_clear_machine_cred(clp);
  1459. case -NFS4ERR_DELAY:
  1460. case -ETIMEDOUT:
  1461. case -EAGAIN:
  1462. ssleep(1);
  1463. break;
  1464. case -NFS4ERR_MINOR_VERS_MISMATCH:
  1465. if (clp->cl_cons_state == NFS_CS_SESSION_INITING)
  1466. nfs_mark_client_ready(clp, -EPROTONOSUPPORT);
  1467. dprintk("%s: exit with error %d for server %s\n",
  1468. __func__, -EPROTONOSUPPORT, clp->cl_hostname);
  1469. return -EPROTONOSUPPORT;
  1470. case -EKEYEXPIRED:
  1471. nfs4_warn_keyexpired(clp->cl_hostname);
  1472. case -NFS4ERR_NOT_SAME: /* FixMe: implement recovery
  1473. * in nfs4_exchange_id */
  1474. default:
  1475. dprintk("%s: exit with error %d for server %s\n", __func__,
  1476. status, clp->cl_hostname);
  1477. return status;
  1478. }
  1479. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  1480. dprintk("%s: handled error %d for server %s\n", __func__, status,
  1481. clp->cl_hostname);
  1482. return 0;
  1483. }
  1484. static int nfs4_establish_lease(struct nfs_client *clp)
  1485. {
  1486. struct rpc_cred *cred;
  1487. const struct nfs4_state_recovery_ops *ops =
  1488. clp->cl_mvops->reboot_recovery_ops;
  1489. int status;
  1490. cred = ops->get_clid_cred(clp);
  1491. if (cred == NULL)
  1492. return -ENOENT;
  1493. status = ops->establish_clid(clp, cred);
  1494. put_rpccred(cred);
  1495. if (status != 0)
  1496. return status;
  1497. pnfs_destroy_all_layouts(clp);
  1498. return 0;
  1499. }
  1500. /*
  1501. * Returns zero or a negative errno. NFS4ERR values are converted
  1502. * to local errno values.
  1503. */
  1504. static int nfs4_reclaim_lease(struct nfs_client *clp)
  1505. {
  1506. int status;
  1507. status = nfs4_establish_lease(clp);
  1508. if (status < 0)
  1509. return nfs4_handle_reclaim_lease_error(clp, status);
  1510. if (test_and_clear_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state))
  1511. nfs4_state_start_reclaim_nograce(clp);
  1512. if (!test_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state))
  1513. set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
  1514. clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
  1515. clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  1516. return 0;
  1517. }
  1518. static int nfs4_purge_lease(struct nfs_client *clp)
  1519. {
  1520. int status;
  1521. status = nfs4_establish_lease(clp);
  1522. if (status < 0)
  1523. return nfs4_handle_reclaim_lease_error(clp, status);
  1524. clear_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state);
  1525. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  1526. nfs4_state_start_reclaim_nograce(clp);
  1527. return 0;
  1528. }
  1529. #ifdef CONFIG_NFS_V4_1
  1530. void nfs4_schedule_session_recovery(struct nfs4_session *session, int err)
  1531. {
  1532. struct nfs_client *clp = session->clp;
  1533. switch (err) {
  1534. default:
  1535. set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
  1536. break;
  1537. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  1538. set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
  1539. }
  1540. nfs4_schedule_lease_recovery(clp);
  1541. }
  1542. EXPORT_SYMBOL_GPL(nfs4_schedule_session_recovery);
  1543. void nfs41_handle_recall_slot(struct nfs_client *clp)
  1544. {
  1545. set_bit(NFS4CLNT_RECALL_SLOT, &clp->cl_state);
  1546. dprintk("%s: scheduling slot recall for server %s\n", __func__,
  1547. clp->cl_hostname);
  1548. nfs4_schedule_state_manager(clp);
  1549. }
  1550. static void nfs4_reset_all_state(struct nfs_client *clp)
  1551. {
  1552. if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
  1553. set_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state);
  1554. clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
  1555. nfs4_state_start_reclaim_nograce(clp);
  1556. dprintk("%s: scheduling reset of all state for server %s!\n",
  1557. __func__, clp->cl_hostname);
  1558. nfs4_schedule_state_manager(clp);
  1559. }
  1560. }
  1561. static void nfs41_handle_server_reboot(struct nfs_client *clp)
  1562. {
  1563. if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
  1564. nfs4_state_start_reclaim_reboot(clp);
  1565. dprintk("%s: server %s rebooted!\n", __func__,
  1566. clp->cl_hostname);
  1567. nfs4_schedule_state_manager(clp);
  1568. }
  1569. }
  1570. static void nfs41_handle_state_revoked(struct nfs_client *clp)
  1571. {
  1572. nfs4_reset_all_state(clp);
  1573. dprintk("%s: state revoked on server %s\n", __func__, clp->cl_hostname);
  1574. }
  1575. static void nfs41_handle_recallable_state_revoked(struct nfs_client *clp)
  1576. {
  1577. /* This will need to handle layouts too */
  1578. nfs_expire_all_delegations(clp);
  1579. dprintk("%s: Recallable state revoked on server %s!\n", __func__,
  1580. clp->cl_hostname);
  1581. }
  1582. static void nfs41_handle_backchannel_fault(struct nfs_client *clp)
  1583. {
  1584. nfs_expire_all_delegations(clp);
  1585. if (test_and_set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state) == 0)
  1586. nfs4_schedule_state_manager(clp);
  1587. dprintk("%s: server %s declared a backchannel fault\n", __func__,
  1588. clp->cl_hostname);
  1589. }
  1590. static void nfs41_handle_cb_path_down(struct nfs_client *clp)
  1591. {
  1592. if (test_and_set_bit(NFS4CLNT_BIND_CONN_TO_SESSION,
  1593. &clp->cl_state) == 0)
  1594. nfs4_schedule_state_manager(clp);
  1595. }
  1596. void nfs41_handle_sequence_flag_errors(struct nfs_client *clp, u32 flags)
  1597. {
  1598. if (!flags)
  1599. return;
  1600. dprintk("%s: \"%s\" (client ID %llx) flags=0x%08x\n",
  1601. __func__, clp->cl_hostname, clp->cl_clientid, flags);
  1602. if (flags & SEQ4_STATUS_RESTART_RECLAIM_NEEDED)
  1603. nfs41_handle_server_reboot(clp);
  1604. if (flags & (SEQ4_STATUS_EXPIRED_ALL_STATE_REVOKED |
  1605. SEQ4_STATUS_EXPIRED_SOME_STATE_REVOKED |
  1606. SEQ4_STATUS_ADMIN_STATE_REVOKED |
  1607. SEQ4_STATUS_LEASE_MOVED))
  1608. nfs41_handle_state_revoked(clp);
  1609. if (flags & SEQ4_STATUS_RECALLABLE_STATE_REVOKED)
  1610. nfs41_handle_recallable_state_revoked(clp);
  1611. if (flags & SEQ4_STATUS_BACKCHANNEL_FAULT)
  1612. nfs41_handle_backchannel_fault(clp);
  1613. else if (flags & (SEQ4_STATUS_CB_PATH_DOWN |
  1614. SEQ4_STATUS_CB_PATH_DOWN_SESSION))
  1615. nfs41_handle_cb_path_down(clp);
  1616. }
  1617. static int nfs4_reset_session(struct nfs_client *clp)
  1618. {
  1619. struct rpc_cred *cred;
  1620. int status;
  1621. if (!nfs4_has_session(clp))
  1622. return 0;
  1623. nfs4_begin_drain_session(clp);
  1624. cred = nfs4_get_exchange_id_cred(clp);
  1625. status = nfs4_proc_destroy_session(clp->cl_session, cred);
  1626. if (status && status != -NFS4ERR_BADSESSION &&
  1627. status != -NFS4ERR_DEADSESSION) {
  1628. status = nfs4_recovery_handle_error(clp, status);
  1629. goto out;
  1630. }
  1631. memset(clp->cl_session->sess_id.data, 0, NFS4_MAX_SESSIONID_LEN);
  1632. status = nfs4_proc_create_session(clp, cred);
  1633. if (status) {
  1634. dprintk("%s: session reset failed with status %d for server %s!\n",
  1635. __func__, status, clp->cl_hostname);
  1636. status = nfs4_handle_reclaim_lease_error(clp, status);
  1637. goto out;
  1638. }
  1639. nfs41_finish_session_reset(clp);
  1640. dprintk("%s: session reset was successful for server %s!\n",
  1641. __func__, clp->cl_hostname);
  1642. out:
  1643. if (cred)
  1644. put_rpccred(cred);
  1645. return status;
  1646. }
  1647. static int nfs4_recall_slot(struct nfs_client *clp)
  1648. {
  1649. struct nfs4_slot_table *fc_tbl;
  1650. struct nfs4_slot *new, *old;
  1651. int i;
  1652. if (!nfs4_has_session(clp))
  1653. return 0;
  1654. nfs4_begin_drain_session(clp);
  1655. fc_tbl = &clp->cl_session->fc_slot_table;
  1656. new = kmalloc(fc_tbl->target_max_slots * sizeof(struct nfs4_slot),
  1657. GFP_NOFS);
  1658. if (!new)
  1659. return -ENOMEM;
  1660. spin_lock(&fc_tbl->slot_tbl_lock);
  1661. for (i = 0; i < fc_tbl->target_max_slots; i++)
  1662. new[i].seq_nr = fc_tbl->slots[i].seq_nr;
  1663. old = fc_tbl->slots;
  1664. fc_tbl->slots = new;
  1665. fc_tbl->max_slots = fc_tbl->target_max_slots;
  1666. fc_tbl->target_max_slots = 0;
  1667. clp->cl_session->fc_attrs.max_reqs = fc_tbl->max_slots;
  1668. spin_unlock(&fc_tbl->slot_tbl_lock);
  1669. kfree(old);
  1670. return 0;
  1671. }
  1672. static int nfs4_bind_conn_to_session(struct nfs_client *clp)
  1673. {
  1674. struct rpc_cred *cred;
  1675. int ret;
  1676. if (!nfs4_has_session(clp))
  1677. return 0;
  1678. nfs4_begin_drain_session(clp);
  1679. cred = nfs4_get_exchange_id_cred(clp);
  1680. ret = nfs4_proc_bind_conn_to_session(clp, cred);
  1681. if (cred)
  1682. put_rpccred(cred);
  1683. clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
  1684. switch (ret) {
  1685. case 0:
  1686. dprintk("%s: bind_conn_to_session was successful for server %s!\n",
  1687. __func__, clp->cl_hostname);
  1688. break;
  1689. case -NFS4ERR_DELAY:
  1690. ssleep(1);
  1691. set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
  1692. break;
  1693. default:
  1694. return nfs4_recovery_handle_error(clp, ret);
  1695. }
  1696. return 0;
  1697. }
  1698. #else /* CONFIG_NFS_V4_1 */
  1699. static int nfs4_reset_session(struct nfs_client *clp) { return 0; }
  1700. static int nfs4_end_drain_session(struct nfs_client *clp) { return 0; }
  1701. static int nfs4_recall_slot(struct nfs_client *clp) { return 0; }
  1702. static int nfs4_bind_conn_to_session(struct nfs_client *clp)
  1703. {
  1704. return 0;
  1705. }
  1706. #endif /* CONFIG_NFS_V4_1 */
  1707. static void nfs4_state_manager(struct nfs_client *clp)
  1708. {
  1709. int status = 0;
  1710. const char *section = "", *section_sep = "";
  1711. /* Ensure exclusive access to NFSv4 state */
  1712. do {
  1713. if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
  1714. section = "purge state";
  1715. status = nfs4_purge_lease(clp);
  1716. if (status < 0)
  1717. goto out_error;
  1718. continue;
  1719. }
  1720. if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state)) {
  1721. section = "lease expired";
  1722. /* We're going to have to re-establish a clientid */
  1723. status = nfs4_reclaim_lease(clp);
  1724. if (status < 0)
  1725. goto out_error;
  1726. continue;
  1727. }
  1728. if (test_and_clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state)) {
  1729. section = "check lease";
  1730. status = nfs4_check_lease(clp);
  1731. if (status < 0)
  1732. goto out_error;
  1733. if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
  1734. continue;
  1735. }
  1736. /* Initialize or reset the session */
  1737. if (test_and_clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state)) {
  1738. section = "reset session";
  1739. status = nfs4_reset_session(clp);
  1740. if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
  1741. continue;
  1742. if (status < 0)
  1743. goto out_error;
  1744. }
  1745. /* Send BIND_CONN_TO_SESSION */
  1746. if (test_and_clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION,
  1747. &clp->cl_state)) {
  1748. section = "bind conn to session";
  1749. status = nfs4_bind_conn_to_session(clp);
  1750. if (status < 0)
  1751. goto out_error;
  1752. continue;
  1753. }
  1754. /* Recall session slots */
  1755. if (test_and_clear_bit(NFS4CLNT_RECALL_SLOT, &clp->cl_state)) {
  1756. section = "recall slot";
  1757. status = nfs4_recall_slot(clp);
  1758. if (status < 0)
  1759. goto out_error;
  1760. continue;
  1761. }
  1762. /* First recover reboot state... */
  1763. if (test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state)) {
  1764. section = "reclaim reboot";
  1765. status = nfs4_do_reclaim(clp,
  1766. clp->cl_mvops->reboot_recovery_ops);
  1767. if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) ||
  1768. test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state))
  1769. continue;
  1770. nfs4_state_end_reclaim_reboot(clp);
  1771. if (test_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state))
  1772. continue;
  1773. if (status < 0)
  1774. goto out_error;
  1775. }
  1776. /* Now recover expired state... */
  1777. if (test_and_clear_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state)) {
  1778. section = "reclaim nograce";
  1779. status = nfs4_do_reclaim(clp,
  1780. clp->cl_mvops->nograce_recovery_ops);
  1781. if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) ||
  1782. test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state) ||
  1783. test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
  1784. continue;
  1785. if (status < 0)
  1786. goto out_error;
  1787. }
  1788. nfs4_end_drain_session(clp);
  1789. if (test_and_clear_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state)) {
  1790. nfs_client_return_marked_delegations(clp);
  1791. continue;
  1792. }
  1793. nfs4_clear_state_manager_bit(clp);
  1794. /* Did we race with an attempt to give us more work? */
  1795. if (clp->cl_state == 0)
  1796. break;
  1797. if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
  1798. break;
  1799. } while (atomic_read(&clp->cl_count) > 1);
  1800. return;
  1801. out_error:
  1802. if (strlen(section))
  1803. section_sep = ": ";
  1804. pr_warn_ratelimited("NFS: state manager%s%s failed on NFSv4 server %s"
  1805. " with error %d\n", section_sep, section,
  1806. clp->cl_hostname, -status);
  1807. nfs4_end_drain_session(clp);
  1808. nfs4_clear_state_manager_bit(clp);
  1809. }
  1810. static int nfs4_run_state_manager(void *ptr)
  1811. {
  1812. struct nfs_client *clp = ptr;
  1813. allow_signal(SIGKILL);
  1814. nfs4_state_manager(clp);
  1815. nfs_put_client(clp);
  1816. module_put_and_exit(0);
  1817. return 0;
  1818. }
  1819. /*
  1820. * Local variables:
  1821. * c-basic-offset: 8
  1822. * End:
  1823. */