nfs4state.c 58 KB

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