nfs4state.c 58 KB

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