nfs4state.c 57 KB

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