nfs4state.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718
  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 "nfs4_fs.h"
  52. #include "callback.h"
  53. #include "delegation.h"
  54. #include "internal.h"
  55. #include "pnfs.h"
  56. #define OPENOWNER_POOL_SIZE 8
  57. const nfs4_stateid zero_stateid;
  58. static LIST_HEAD(nfs4_clientid_list);
  59. int nfs4_init_clientid(struct nfs_client *clp, struct rpc_cred *cred)
  60. {
  61. struct nfs4_setclientid_res clid;
  62. unsigned short port;
  63. int status;
  64. port = nfs_callback_tcpport;
  65. if (clp->cl_addr.ss_family == AF_INET6)
  66. port = nfs_callback_tcpport6;
  67. status = nfs4_proc_setclientid(clp, NFS4_CALLBACK, port, cred, &clid);
  68. if (status != 0)
  69. goto out;
  70. status = nfs4_proc_setclientid_confirm(clp, &clid, cred);
  71. if (status != 0)
  72. goto out;
  73. clp->cl_clientid = clid.clientid;
  74. nfs4_schedule_state_renewal(clp);
  75. out:
  76. return status;
  77. }
  78. struct rpc_cred *nfs4_get_machine_cred_locked(struct nfs_client *clp)
  79. {
  80. struct rpc_cred *cred = NULL;
  81. if (clp->cl_machine_cred != NULL)
  82. cred = get_rpccred(clp->cl_machine_cred);
  83. return cred;
  84. }
  85. static void nfs4_clear_machine_cred(struct nfs_client *clp)
  86. {
  87. struct rpc_cred *cred;
  88. spin_lock(&clp->cl_lock);
  89. cred = clp->cl_machine_cred;
  90. clp->cl_machine_cred = NULL;
  91. spin_unlock(&clp->cl_lock);
  92. if (cred != NULL)
  93. put_rpccred(cred);
  94. }
  95. static struct rpc_cred *
  96. nfs4_get_renew_cred_server_locked(struct nfs_server *server)
  97. {
  98. struct rpc_cred *cred = NULL;
  99. struct nfs4_state_owner *sp;
  100. struct rb_node *pos;
  101. for (pos = rb_first(&server->state_owners);
  102. pos != NULL;
  103. pos = rb_next(pos)) {
  104. sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
  105. if (list_empty(&sp->so_states))
  106. continue;
  107. cred = get_rpccred(sp->so_cred);
  108. break;
  109. }
  110. return cred;
  111. }
  112. /**
  113. * nfs4_get_renew_cred_locked - Acquire credential for a renew operation
  114. * @clp: client state handle
  115. *
  116. * Returns an rpc_cred with reference count bumped, or NULL.
  117. * Caller must hold clp->cl_lock.
  118. */
  119. struct rpc_cred *nfs4_get_renew_cred_locked(struct nfs_client *clp)
  120. {
  121. struct rpc_cred *cred = NULL;
  122. struct nfs_server *server;
  123. rcu_read_lock();
  124. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  125. cred = nfs4_get_renew_cred_server_locked(server);
  126. if (cred != NULL)
  127. break;
  128. }
  129. rcu_read_unlock();
  130. return cred;
  131. }
  132. #if defined(CONFIG_NFS_V4_1)
  133. static int nfs41_setup_state_renewal(struct nfs_client *clp)
  134. {
  135. int status;
  136. struct nfs_fsinfo fsinfo;
  137. if (!test_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state)) {
  138. nfs4_schedule_state_renewal(clp);
  139. return 0;
  140. }
  141. status = nfs4_proc_get_lease_time(clp, &fsinfo);
  142. if (status == 0) {
  143. /* Update lease time and schedule renewal */
  144. spin_lock(&clp->cl_lock);
  145. clp->cl_lease_time = fsinfo.lease_time * HZ;
  146. clp->cl_last_renewal = jiffies;
  147. spin_unlock(&clp->cl_lock);
  148. nfs4_schedule_state_renewal(clp);
  149. }
  150. return status;
  151. }
  152. /*
  153. * Back channel returns NFS4ERR_DELAY for new requests when
  154. * NFS4_SESSION_DRAINING is set so there is no work to be done when draining
  155. * is ended.
  156. */
  157. static void nfs4_end_drain_session(struct nfs_client *clp)
  158. {
  159. struct nfs4_session *ses = clp->cl_session;
  160. int max_slots;
  161. if (ses == NULL)
  162. return;
  163. if (test_and_clear_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
  164. spin_lock(&ses->fc_slot_table.slot_tbl_lock);
  165. max_slots = ses->fc_slot_table.max_slots;
  166. while (max_slots--) {
  167. struct rpc_task *task;
  168. task = rpc_wake_up_next(&ses->fc_slot_table.
  169. slot_tbl_waitq);
  170. if (!task)
  171. break;
  172. rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
  173. }
  174. spin_unlock(&ses->fc_slot_table.slot_tbl_lock);
  175. }
  176. }
  177. static int nfs4_wait_on_slot_tbl(struct nfs4_slot_table *tbl)
  178. {
  179. spin_lock(&tbl->slot_tbl_lock);
  180. if (tbl->highest_used_slotid != -1) {
  181. INIT_COMPLETION(tbl->complete);
  182. spin_unlock(&tbl->slot_tbl_lock);
  183. return wait_for_completion_interruptible(&tbl->complete);
  184. }
  185. spin_unlock(&tbl->slot_tbl_lock);
  186. return 0;
  187. }
  188. static int nfs4_begin_drain_session(struct nfs_client *clp)
  189. {
  190. struct nfs4_session *ses = clp->cl_session;
  191. int ret = 0;
  192. set_bit(NFS4_SESSION_DRAINING, &ses->session_state);
  193. /* back channel */
  194. ret = nfs4_wait_on_slot_tbl(&ses->bc_slot_table);
  195. if (ret)
  196. return ret;
  197. /* fore channel */
  198. return nfs4_wait_on_slot_tbl(&ses->fc_slot_table);
  199. }
  200. int nfs41_init_clientid(struct nfs_client *clp, struct rpc_cred *cred)
  201. {
  202. int status;
  203. nfs4_begin_drain_session(clp);
  204. status = nfs4_proc_exchange_id(clp, cred);
  205. if (status != 0)
  206. goto out;
  207. status = nfs4_proc_create_session(clp);
  208. if (status != 0)
  209. goto out;
  210. nfs41_setup_state_renewal(clp);
  211. nfs_mark_client_ready(clp, NFS_CS_READY);
  212. out:
  213. return status;
  214. }
  215. struct rpc_cred *nfs4_get_exchange_id_cred(struct nfs_client *clp)
  216. {
  217. struct rpc_cred *cred;
  218. spin_lock(&clp->cl_lock);
  219. cred = nfs4_get_machine_cred_locked(clp);
  220. spin_unlock(&clp->cl_lock);
  221. return cred;
  222. }
  223. #endif /* CONFIG_NFS_V4_1 */
  224. static struct rpc_cred *
  225. nfs4_get_setclientid_cred_server(struct nfs_server *server)
  226. {
  227. struct nfs_client *clp = server->nfs_client;
  228. struct rpc_cred *cred = NULL;
  229. struct nfs4_state_owner *sp;
  230. struct rb_node *pos;
  231. spin_lock(&clp->cl_lock);
  232. pos = rb_first(&server->state_owners);
  233. if (pos != NULL) {
  234. sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
  235. cred = get_rpccred(sp->so_cred);
  236. }
  237. spin_unlock(&clp->cl_lock);
  238. return cred;
  239. }
  240. /**
  241. * nfs4_get_setclientid_cred - Acquire credential for a setclientid operation
  242. * @clp: client state handle
  243. *
  244. * Returns an rpc_cred with reference count bumped, or NULL.
  245. */
  246. struct rpc_cred *nfs4_get_setclientid_cred(struct nfs_client *clp)
  247. {
  248. struct nfs_server *server;
  249. struct rpc_cred *cred;
  250. spin_lock(&clp->cl_lock);
  251. cred = nfs4_get_machine_cred_locked(clp);
  252. spin_unlock(&clp->cl_lock);
  253. if (cred != NULL)
  254. goto out;
  255. rcu_read_lock();
  256. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  257. cred = nfs4_get_setclientid_cred_server(server);
  258. if (cred != NULL)
  259. break;
  260. }
  261. rcu_read_unlock();
  262. out:
  263. return cred;
  264. }
  265. static void nfs_alloc_unique_id_locked(struct rb_root *root,
  266. struct nfs_unique_id *new,
  267. __u64 minval, int maxbits)
  268. {
  269. struct rb_node **p, *parent;
  270. struct nfs_unique_id *pos;
  271. __u64 mask = ~0ULL;
  272. if (maxbits < 64)
  273. mask = (1ULL << maxbits) - 1ULL;
  274. /* Ensure distribution is more or less flat */
  275. get_random_bytes(&new->id, sizeof(new->id));
  276. new->id &= mask;
  277. if (new->id < minval)
  278. new->id += minval;
  279. retry:
  280. p = &root->rb_node;
  281. parent = NULL;
  282. while (*p != NULL) {
  283. parent = *p;
  284. pos = rb_entry(parent, struct nfs_unique_id, rb_node);
  285. if (new->id < pos->id)
  286. p = &(*p)->rb_left;
  287. else if (new->id > pos->id)
  288. p = &(*p)->rb_right;
  289. else
  290. goto id_exists;
  291. }
  292. rb_link_node(&new->rb_node, parent, p);
  293. rb_insert_color(&new->rb_node, root);
  294. return;
  295. id_exists:
  296. for (;;) {
  297. new->id++;
  298. if (new->id < minval || (new->id & mask) != new->id) {
  299. new->id = minval;
  300. break;
  301. }
  302. parent = rb_next(parent);
  303. if (parent == NULL)
  304. break;
  305. pos = rb_entry(parent, struct nfs_unique_id, rb_node);
  306. if (new->id < pos->id)
  307. break;
  308. }
  309. goto retry;
  310. }
  311. static void nfs_free_unique_id(struct rb_root *root, struct nfs_unique_id *id)
  312. {
  313. rb_erase(&id->rb_node, root);
  314. }
  315. static struct nfs4_state_owner *
  316. nfs4_find_state_owner_locked(struct nfs_server *server, struct rpc_cred *cred)
  317. {
  318. struct rb_node **p = &server->state_owners.rb_node,
  319. *parent = NULL;
  320. struct nfs4_state_owner *sp, *res = NULL;
  321. while (*p != NULL) {
  322. parent = *p;
  323. sp = rb_entry(parent, struct nfs4_state_owner, so_server_node);
  324. if (server < sp->so_server) {
  325. p = &parent->rb_left;
  326. continue;
  327. }
  328. if (server > sp->so_server) {
  329. p = &parent->rb_right;
  330. continue;
  331. }
  332. if (cred < sp->so_cred)
  333. p = &parent->rb_left;
  334. else if (cred > sp->so_cred)
  335. p = &parent->rb_right;
  336. else {
  337. atomic_inc(&sp->so_count);
  338. res = sp;
  339. break;
  340. }
  341. }
  342. return res;
  343. }
  344. static struct nfs4_state_owner *
  345. nfs4_insert_state_owner_locked(struct nfs4_state_owner *new)
  346. {
  347. struct nfs_server *server = new->so_server;
  348. struct rb_node **p = &server->state_owners.rb_node,
  349. *parent = NULL;
  350. struct nfs4_state_owner *sp;
  351. while (*p != NULL) {
  352. parent = *p;
  353. sp = rb_entry(parent, struct nfs4_state_owner, so_server_node);
  354. if (new->so_cred < sp->so_cred)
  355. p = &parent->rb_left;
  356. else if (new->so_cred > sp->so_cred)
  357. p = &parent->rb_right;
  358. else {
  359. atomic_inc(&sp->so_count);
  360. return sp;
  361. }
  362. }
  363. nfs_alloc_unique_id_locked(&server->openowner_id,
  364. &new->so_owner_id, 1, 64);
  365. rb_link_node(&new->so_server_node, parent, p);
  366. rb_insert_color(&new->so_server_node, &server->state_owners);
  367. return new;
  368. }
  369. static void
  370. nfs4_remove_state_owner_locked(struct nfs4_state_owner *sp)
  371. {
  372. struct nfs_server *server = sp->so_server;
  373. if (!RB_EMPTY_NODE(&sp->so_server_node))
  374. rb_erase(&sp->so_server_node, &server->state_owners);
  375. nfs_free_unique_id(&server->openowner_id, &sp->so_owner_id);
  376. }
  377. /*
  378. * nfs4_alloc_state_owner(): this is called on the OPEN or CREATE path to
  379. * create a new state_owner.
  380. *
  381. */
  382. static struct nfs4_state_owner *
  383. nfs4_alloc_state_owner(void)
  384. {
  385. struct nfs4_state_owner *sp;
  386. sp = kzalloc(sizeof(*sp),GFP_NOFS);
  387. if (!sp)
  388. return NULL;
  389. spin_lock_init(&sp->so_lock);
  390. INIT_LIST_HEAD(&sp->so_states);
  391. rpc_init_wait_queue(&sp->so_sequence.wait, "Seqid_waitqueue");
  392. sp->so_seqid.sequence = &sp->so_sequence;
  393. spin_lock_init(&sp->so_sequence.lock);
  394. INIT_LIST_HEAD(&sp->so_sequence.list);
  395. atomic_set(&sp->so_count, 1);
  396. return sp;
  397. }
  398. static void
  399. nfs4_drop_state_owner(struct nfs4_state_owner *sp)
  400. {
  401. if (!RB_EMPTY_NODE(&sp->so_server_node)) {
  402. struct nfs_server *server = sp->so_server;
  403. struct nfs_client *clp = server->nfs_client;
  404. spin_lock(&clp->cl_lock);
  405. rb_erase(&sp->so_server_node, &server->state_owners);
  406. RB_CLEAR_NODE(&sp->so_server_node);
  407. spin_unlock(&clp->cl_lock);
  408. }
  409. }
  410. /**
  411. * nfs4_get_state_owner - Look up a state owner given a credential
  412. * @server: nfs_server to search
  413. * @cred: RPC credential to match
  414. *
  415. * Returns a pointer to an instantiated nfs4_state_owner struct, or NULL.
  416. */
  417. struct nfs4_state_owner *nfs4_get_state_owner(struct nfs_server *server,
  418. struct rpc_cred *cred)
  419. {
  420. struct nfs_client *clp = server->nfs_client;
  421. struct nfs4_state_owner *sp, *new;
  422. spin_lock(&clp->cl_lock);
  423. sp = nfs4_find_state_owner_locked(server, cred);
  424. spin_unlock(&clp->cl_lock);
  425. if (sp != NULL)
  426. return sp;
  427. new = nfs4_alloc_state_owner();
  428. if (new == NULL)
  429. return NULL;
  430. new->so_server = server;
  431. new->so_cred = cred;
  432. spin_lock(&clp->cl_lock);
  433. sp = nfs4_insert_state_owner_locked(new);
  434. spin_unlock(&clp->cl_lock);
  435. if (sp == new)
  436. get_rpccred(cred);
  437. else {
  438. rpc_destroy_wait_queue(&new->so_sequence.wait);
  439. kfree(new);
  440. }
  441. return sp;
  442. }
  443. /**
  444. * nfs4_put_state_owner - Release a nfs4_state_owner
  445. * @sp: state owner data to release
  446. *
  447. */
  448. void nfs4_put_state_owner(struct nfs4_state_owner *sp)
  449. {
  450. struct nfs_client *clp = sp->so_server->nfs_client;
  451. struct rpc_cred *cred = sp->so_cred;
  452. if (!atomic_dec_and_lock(&sp->so_count, &clp->cl_lock))
  453. return;
  454. nfs4_remove_state_owner_locked(sp);
  455. spin_unlock(&clp->cl_lock);
  456. rpc_destroy_wait_queue(&sp->so_sequence.wait);
  457. put_rpccred(cred);
  458. kfree(sp);
  459. }
  460. static struct nfs4_state *
  461. nfs4_alloc_open_state(void)
  462. {
  463. struct nfs4_state *state;
  464. state = kzalloc(sizeof(*state), GFP_NOFS);
  465. if (!state)
  466. return NULL;
  467. atomic_set(&state->count, 1);
  468. INIT_LIST_HEAD(&state->lock_states);
  469. spin_lock_init(&state->state_lock);
  470. seqlock_init(&state->seqlock);
  471. return state;
  472. }
  473. void
  474. nfs4_state_set_mode_locked(struct nfs4_state *state, fmode_t fmode)
  475. {
  476. if (state->state == fmode)
  477. return;
  478. /* NB! List reordering - see the reclaim code for why. */
  479. if ((fmode & FMODE_WRITE) != (state->state & FMODE_WRITE)) {
  480. if (fmode & FMODE_WRITE)
  481. list_move(&state->open_states, &state->owner->so_states);
  482. else
  483. list_move_tail(&state->open_states, &state->owner->so_states);
  484. }
  485. state->state = fmode;
  486. }
  487. static struct nfs4_state *
  488. __nfs4_find_state_byowner(struct inode *inode, struct nfs4_state_owner *owner)
  489. {
  490. struct nfs_inode *nfsi = NFS_I(inode);
  491. struct nfs4_state *state;
  492. list_for_each_entry(state, &nfsi->open_states, inode_states) {
  493. if (state->owner != owner)
  494. continue;
  495. if (atomic_inc_not_zero(&state->count))
  496. return state;
  497. }
  498. return NULL;
  499. }
  500. static void
  501. nfs4_free_open_state(struct nfs4_state *state)
  502. {
  503. kfree(state);
  504. }
  505. struct nfs4_state *
  506. nfs4_get_open_state(struct inode *inode, struct nfs4_state_owner *owner)
  507. {
  508. struct nfs4_state *state, *new;
  509. struct nfs_inode *nfsi = NFS_I(inode);
  510. spin_lock(&inode->i_lock);
  511. state = __nfs4_find_state_byowner(inode, owner);
  512. spin_unlock(&inode->i_lock);
  513. if (state)
  514. goto out;
  515. new = nfs4_alloc_open_state();
  516. spin_lock(&owner->so_lock);
  517. spin_lock(&inode->i_lock);
  518. state = __nfs4_find_state_byowner(inode, owner);
  519. if (state == NULL && new != NULL) {
  520. state = new;
  521. state->owner = owner;
  522. atomic_inc(&owner->so_count);
  523. list_add(&state->inode_states, &nfsi->open_states);
  524. ihold(inode);
  525. state->inode = inode;
  526. spin_unlock(&inode->i_lock);
  527. /* Note: The reclaim code dictates that we add stateless
  528. * and read-only stateids to the end of the list */
  529. list_add_tail(&state->open_states, &owner->so_states);
  530. spin_unlock(&owner->so_lock);
  531. } else {
  532. spin_unlock(&inode->i_lock);
  533. spin_unlock(&owner->so_lock);
  534. if (new)
  535. nfs4_free_open_state(new);
  536. }
  537. out:
  538. return state;
  539. }
  540. void nfs4_put_open_state(struct nfs4_state *state)
  541. {
  542. struct inode *inode = state->inode;
  543. struct nfs4_state_owner *owner = state->owner;
  544. if (!atomic_dec_and_lock(&state->count, &owner->so_lock))
  545. return;
  546. spin_lock(&inode->i_lock);
  547. list_del(&state->inode_states);
  548. list_del(&state->open_states);
  549. spin_unlock(&inode->i_lock);
  550. spin_unlock(&owner->so_lock);
  551. iput(inode);
  552. nfs4_free_open_state(state);
  553. nfs4_put_state_owner(owner);
  554. }
  555. /*
  556. * Close the current file.
  557. */
  558. static void __nfs4_close(struct path *path, struct nfs4_state *state,
  559. fmode_t fmode, gfp_t gfp_mask, int wait)
  560. {
  561. struct nfs4_state_owner *owner = state->owner;
  562. int call_close = 0;
  563. fmode_t newstate;
  564. atomic_inc(&owner->so_count);
  565. /* Protect against nfs4_find_state() */
  566. spin_lock(&owner->so_lock);
  567. switch (fmode & (FMODE_READ | FMODE_WRITE)) {
  568. case FMODE_READ:
  569. state->n_rdonly--;
  570. break;
  571. case FMODE_WRITE:
  572. state->n_wronly--;
  573. break;
  574. case FMODE_READ|FMODE_WRITE:
  575. state->n_rdwr--;
  576. }
  577. newstate = FMODE_READ|FMODE_WRITE;
  578. if (state->n_rdwr == 0) {
  579. if (state->n_rdonly == 0) {
  580. newstate &= ~FMODE_READ;
  581. call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
  582. call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
  583. }
  584. if (state->n_wronly == 0) {
  585. newstate &= ~FMODE_WRITE;
  586. call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
  587. call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
  588. }
  589. if (newstate == 0)
  590. clear_bit(NFS_DELEGATED_STATE, &state->flags);
  591. }
  592. nfs4_state_set_mode_locked(state, newstate);
  593. spin_unlock(&owner->so_lock);
  594. if (!call_close) {
  595. nfs4_put_open_state(state);
  596. nfs4_put_state_owner(owner);
  597. } else {
  598. bool roc = pnfs_roc(state->inode);
  599. nfs4_do_close(path, state, gfp_mask, wait, roc);
  600. }
  601. }
  602. void nfs4_close_state(struct path *path, struct nfs4_state *state, fmode_t fmode)
  603. {
  604. __nfs4_close(path, state, fmode, GFP_NOFS, 0);
  605. }
  606. void nfs4_close_sync(struct path *path, struct nfs4_state *state, fmode_t fmode)
  607. {
  608. __nfs4_close(path, state, fmode, GFP_KERNEL, 1);
  609. }
  610. /*
  611. * Search the state->lock_states for an existing lock_owner
  612. * that is compatible with current->files
  613. */
  614. static struct nfs4_lock_state *
  615. __nfs4_find_lock_state(struct nfs4_state *state, fl_owner_t fl_owner, pid_t fl_pid, unsigned int type)
  616. {
  617. struct nfs4_lock_state *pos;
  618. list_for_each_entry(pos, &state->lock_states, ls_locks) {
  619. if (type != NFS4_ANY_LOCK_TYPE && pos->ls_owner.lo_type != type)
  620. continue;
  621. switch (pos->ls_owner.lo_type) {
  622. case NFS4_POSIX_LOCK_TYPE:
  623. if (pos->ls_owner.lo_u.posix_owner != fl_owner)
  624. continue;
  625. break;
  626. case NFS4_FLOCK_LOCK_TYPE:
  627. if (pos->ls_owner.lo_u.flock_owner != fl_pid)
  628. continue;
  629. }
  630. atomic_inc(&pos->ls_count);
  631. return pos;
  632. }
  633. return NULL;
  634. }
  635. /*
  636. * Return a compatible lock_state. If no initialized lock_state structure
  637. * exists, return an uninitialized one.
  638. *
  639. */
  640. 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)
  641. {
  642. struct nfs4_lock_state *lsp;
  643. struct nfs_server *server = state->owner->so_server;
  644. struct nfs_client *clp = server->nfs_client;
  645. lsp = kzalloc(sizeof(*lsp), GFP_NOFS);
  646. if (lsp == NULL)
  647. return NULL;
  648. rpc_init_wait_queue(&lsp->ls_sequence.wait, "lock_seqid_waitqueue");
  649. spin_lock_init(&lsp->ls_sequence.lock);
  650. INIT_LIST_HEAD(&lsp->ls_sequence.list);
  651. lsp->ls_seqid.sequence = &lsp->ls_sequence;
  652. atomic_set(&lsp->ls_count, 1);
  653. lsp->ls_state = state;
  654. lsp->ls_owner.lo_type = type;
  655. switch (lsp->ls_owner.lo_type) {
  656. case NFS4_FLOCK_LOCK_TYPE:
  657. lsp->ls_owner.lo_u.flock_owner = fl_pid;
  658. break;
  659. case NFS4_POSIX_LOCK_TYPE:
  660. lsp->ls_owner.lo_u.posix_owner = fl_owner;
  661. break;
  662. default:
  663. kfree(lsp);
  664. return NULL;
  665. }
  666. spin_lock(&clp->cl_lock);
  667. nfs_alloc_unique_id_locked(&server->lockowner_id, &lsp->ls_id, 1, 64);
  668. spin_unlock(&clp->cl_lock);
  669. INIT_LIST_HEAD(&lsp->ls_locks);
  670. return lsp;
  671. }
  672. static void nfs4_free_lock_state(struct nfs4_lock_state *lsp)
  673. {
  674. struct nfs_server *server = lsp->ls_state->owner->so_server;
  675. struct nfs_client *clp = server->nfs_client;
  676. spin_lock(&clp->cl_lock);
  677. nfs_free_unique_id(&server->lockowner_id, &lsp->ls_id);
  678. spin_unlock(&clp->cl_lock);
  679. rpc_destroy_wait_queue(&lsp->ls_sequence.wait);
  680. kfree(lsp);
  681. }
  682. /*
  683. * Return a compatible lock_state. If no initialized lock_state structure
  684. * exists, return an uninitialized one.
  685. *
  686. */
  687. static struct nfs4_lock_state *nfs4_get_lock_state(struct nfs4_state *state, fl_owner_t owner, pid_t pid, unsigned int type)
  688. {
  689. struct nfs4_lock_state *lsp, *new = NULL;
  690. for(;;) {
  691. spin_lock(&state->state_lock);
  692. lsp = __nfs4_find_lock_state(state, owner, pid, type);
  693. if (lsp != NULL)
  694. break;
  695. if (new != NULL) {
  696. list_add(&new->ls_locks, &state->lock_states);
  697. set_bit(LK_STATE_IN_USE, &state->flags);
  698. lsp = new;
  699. new = NULL;
  700. break;
  701. }
  702. spin_unlock(&state->state_lock);
  703. new = nfs4_alloc_lock_state(state, owner, pid, type);
  704. if (new == NULL)
  705. return NULL;
  706. }
  707. spin_unlock(&state->state_lock);
  708. if (new != NULL)
  709. nfs4_free_lock_state(new);
  710. return lsp;
  711. }
  712. /*
  713. * Release reference to lock_state, and free it if we see that
  714. * it is no longer in use
  715. */
  716. void nfs4_put_lock_state(struct nfs4_lock_state *lsp)
  717. {
  718. struct nfs4_state *state;
  719. if (lsp == NULL)
  720. return;
  721. state = lsp->ls_state;
  722. if (!atomic_dec_and_lock(&lsp->ls_count, &state->state_lock))
  723. return;
  724. list_del(&lsp->ls_locks);
  725. if (list_empty(&state->lock_states))
  726. clear_bit(LK_STATE_IN_USE, &state->flags);
  727. spin_unlock(&state->state_lock);
  728. if (lsp->ls_flags & NFS_LOCK_INITIALIZED)
  729. nfs4_release_lockowner(lsp);
  730. nfs4_free_lock_state(lsp);
  731. }
  732. static void nfs4_fl_copy_lock(struct file_lock *dst, struct file_lock *src)
  733. {
  734. struct nfs4_lock_state *lsp = src->fl_u.nfs4_fl.owner;
  735. dst->fl_u.nfs4_fl.owner = lsp;
  736. atomic_inc(&lsp->ls_count);
  737. }
  738. static void nfs4_fl_release_lock(struct file_lock *fl)
  739. {
  740. nfs4_put_lock_state(fl->fl_u.nfs4_fl.owner);
  741. }
  742. static const struct file_lock_operations nfs4_fl_lock_ops = {
  743. .fl_copy_lock = nfs4_fl_copy_lock,
  744. .fl_release_private = nfs4_fl_release_lock,
  745. };
  746. int nfs4_set_lock_state(struct nfs4_state *state, struct file_lock *fl)
  747. {
  748. struct nfs4_lock_state *lsp;
  749. if (fl->fl_ops != NULL)
  750. return 0;
  751. if (fl->fl_flags & FL_POSIX)
  752. lsp = nfs4_get_lock_state(state, fl->fl_owner, 0, NFS4_POSIX_LOCK_TYPE);
  753. else if (fl->fl_flags & FL_FLOCK)
  754. lsp = nfs4_get_lock_state(state, 0, fl->fl_pid, NFS4_FLOCK_LOCK_TYPE);
  755. else
  756. return -EINVAL;
  757. if (lsp == NULL)
  758. return -ENOMEM;
  759. fl->fl_u.nfs4_fl.owner = lsp;
  760. fl->fl_ops = &nfs4_fl_lock_ops;
  761. return 0;
  762. }
  763. /*
  764. * Byte-range lock aware utility to initialize the stateid of read/write
  765. * requests.
  766. */
  767. void nfs4_copy_stateid(nfs4_stateid *dst, struct nfs4_state *state, fl_owner_t fl_owner, pid_t fl_pid)
  768. {
  769. struct nfs4_lock_state *lsp;
  770. int seq;
  771. do {
  772. seq = read_seqbegin(&state->seqlock);
  773. memcpy(dst, &state->stateid, sizeof(*dst));
  774. } while (read_seqretry(&state->seqlock, seq));
  775. if (test_bit(LK_STATE_IN_USE, &state->flags) == 0)
  776. return;
  777. spin_lock(&state->state_lock);
  778. lsp = __nfs4_find_lock_state(state, fl_owner, fl_pid, NFS4_ANY_LOCK_TYPE);
  779. if (lsp != NULL && (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
  780. memcpy(dst, &lsp->ls_stateid, sizeof(*dst));
  781. spin_unlock(&state->state_lock);
  782. nfs4_put_lock_state(lsp);
  783. }
  784. struct nfs_seqid *nfs_alloc_seqid(struct nfs_seqid_counter *counter, gfp_t gfp_mask)
  785. {
  786. struct nfs_seqid *new;
  787. new = kmalloc(sizeof(*new), gfp_mask);
  788. if (new != NULL) {
  789. new->sequence = counter;
  790. INIT_LIST_HEAD(&new->list);
  791. }
  792. return new;
  793. }
  794. void nfs_release_seqid(struct nfs_seqid *seqid)
  795. {
  796. if (!list_empty(&seqid->list)) {
  797. struct rpc_sequence *sequence = seqid->sequence->sequence;
  798. spin_lock(&sequence->lock);
  799. list_del_init(&seqid->list);
  800. spin_unlock(&sequence->lock);
  801. rpc_wake_up(&sequence->wait);
  802. }
  803. }
  804. void nfs_free_seqid(struct nfs_seqid *seqid)
  805. {
  806. nfs_release_seqid(seqid);
  807. kfree(seqid);
  808. }
  809. /*
  810. * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or
  811. * failed with a seqid incrementing error -
  812. * see comments nfs_fs.h:seqid_mutating_error()
  813. */
  814. static void nfs_increment_seqid(int status, struct nfs_seqid *seqid)
  815. {
  816. BUG_ON(list_first_entry(&seqid->sequence->sequence->list, struct nfs_seqid, list) != seqid);
  817. switch (status) {
  818. case 0:
  819. break;
  820. case -NFS4ERR_BAD_SEQID:
  821. if (seqid->sequence->flags & NFS_SEQID_CONFIRMED)
  822. return;
  823. printk(KERN_WARNING "NFS: v4 server returned a bad"
  824. " sequence-id error on an"
  825. " unconfirmed sequence %p!\n",
  826. seqid->sequence);
  827. case -NFS4ERR_STALE_CLIENTID:
  828. case -NFS4ERR_STALE_STATEID:
  829. case -NFS4ERR_BAD_STATEID:
  830. case -NFS4ERR_BADXDR:
  831. case -NFS4ERR_RESOURCE:
  832. case -NFS4ERR_NOFILEHANDLE:
  833. /* Non-seqid mutating errors */
  834. return;
  835. };
  836. /*
  837. * Note: no locking needed as we are guaranteed to be first
  838. * on the sequence list
  839. */
  840. seqid->sequence->counter++;
  841. }
  842. void nfs_increment_open_seqid(int status, struct nfs_seqid *seqid)
  843. {
  844. struct nfs4_state_owner *sp = container_of(seqid->sequence,
  845. struct nfs4_state_owner, so_seqid);
  846. struct nfs_server *server = sp->so_server;
  847. if (status == -NFS4ERR_BAD_SEQID)
  848. nfs4_drop_state_owner(sp);
  849. if (!nfs4_has_session(server->nfs_client))
  850. nfs_increment_seqid(status, seqid);
  851. }
  852. /*
  853. * Increment the seqid if the LOCK/LOCKU succeeded, or
  854. * failed with a seqid incrementing error -
  855. * see comments nfs_fs.h:seqid_mutating_error()
  856. */
  857. void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid)
  858. {
  859. nfs_increment_seqid(status, seqid);
  860. }
  861. int nfs_wait_on_sequence(struct nfs_seqid *seqid, struct rpc_task *task)
  862. {
  863. struct rpc_sequence *sequence = seqid->sequence->sequence;
  864. int status = 0;
  865. spin_lock(&sequence->lock);
  866. if (list_empty(&seqid->list))
  867. list_add_tail(&seqid->list, &sequence->list);
  868. if (list_first_entry(&sequence->list, struct nfs_seqid, list) == seqid)
  869. goto unlock;
  870. rpc_sleep_on(&sequence->wait, task, NULL);
  871. status = -EAGAIN;
  872. unlock:
  873. spin_unlock(&sequence->lock);
  874. return status;
  875. }
  876. static int nfs4_run_state_manager(void *);
  877. static void nfs4_clear_state_manager_bit(struct nfs_client *clp)
  878. {
  879. smp_mb__before_clear_bit();
  880. clear_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state);
  881. smp_mb__after_clear_bit();
  882. wake_up_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING);
  883. rpc_wake_up(&clp->cl_rpcwaitq);
  884. }
  885. /*
  886. * Schedule the nfs_client asynchronous state management routine
  887. */
  888. void nfs4_schedule_state_manager(struct nfs_client *clp)
  889. {
  890. struct task_struct *task;
  891. if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
  892. return;
  893. __module_get(THIS_MODULE);
  894. atomic_inc(&clp->cl_count);
  895. task = kthread_run(nfs4_run_state_manager, clp, "%s-manager",
  896. rpc_peeraddr2str(clp->cl_rpcclient,
  897. RPC_DISPLAY_ADDR));
  898. if (!IS_ERR(task))
  899. return;
  900. nfs4_clear_state_manager_bit(clp);
  901. nfs_put_client(clp);
  902. module_put(THIS_MODULE);
  903. }
  904. /*
  905. * Schedule a lease recovery attempt
  906. */
  907. void nfs4_schedule_lease_recovery(struct nfs_client *clp)
  908. {
  909. if (!clp)
  910. return;
  911. if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
  912. set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
  913. nfs4_schedule_state_manager(clp);
  914. }
  915. static int nfs4_state_mark_reclaim_reboot(struct nfs_client *clp, struct nfs4_state *state)
  916. {
  917. set_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
  918. /* Don't recover state that expired before the reboot */
  919. if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags)) {
  920. clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
  921. return 0;
  922. }
  923. set_bit(NFS_OWNER_RECLAIM_REBOOT, &state->owner->so_flags);
  924. set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
  925. return 1;
  926. }
  927. static int nfs4_state_mark_reclaim_nograce(struct nfs_client *clp, struct nfs4_state *state)
  928. {
  929. set_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags);
  930. clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
  931. set_bit(NFS_OWNER_RECLAIM_NOGRACE, &state->owner->so_flags);
  932. set_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state);
  933. return 1;
  934. }
  935. void nfs4_schedule_stateid_recovery(const struct nfs_server *server, struct nfs4_state *state)
  936. {
  937. struct nfs_client *clp = server->nfs_client;
  938. nfs4_state_mark_reclaim_nograce(clp, state);
  939. nfs4_schedule_state_manager(clp);
  940. }
  941. static int nfs4_reclaim_locks(struct nfs4_state *state, const struct nfs4_state_recovery_ops *ops)
  942. {
  943. struct inode *inode = state->inode;
  944. struct nfs_inode *nfsi = NFS_I(inode);
  945. struct file_lock *fl;
  946. int status = 0;
  947. if (inode->i_flock == NULL)
  948. return 0;
  949. /* Guard against delegation returns and new lock/unlock calls */
  950. down_write(&nfsi->rwsem);
  951. /* Protect inode->i_flock using the BKL */
  952. lock_flocks();
  953. for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
  954. if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
  955. continue;
  956. if (nfs_file_open_context(fl->fl_file)->state != state)
  957. continue;
  958. unlock_flocks();
  959. status = ops->recover_lock(state, fl);
  960. switch (status) {
  961. case 0:
  962. break;
  963. case -ESTALE:
  964. case -NFS4ERR_ADMIN_REVOKED:
  965. case -NFS4ERR_STALE_STATEID:
  966. case -NFS4ERR_BAD_STATEID:
  967. case -NFS4ERR_EXPIRED:
  968. case -NFS4ERR_NO_GRACE:
  969. case -NFS4ERR_STALE_CLIENTID:
  970. case -NFS4ERR_BADSESSION:
  971. case -NFS4ERR_BADSLOT:
  972. case -NFS4ERR_BAD_HIGH_SLOT:
  973. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  974. goto out;
  975. default:
  976. printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
  977. __func__, status);
  978. case -ENOMEM:
  979. case -NFS4ERR_DENIED:
  980. case -NFS4ERR_RECLAIM_BAD:
  981. case -NFS4ERR_RECLAIM_CONFLICT:
  982. /* kill_proc(fl->fl_pid, SIGLOST, 1); */
  983. status = 0;
  984. }
  985. lock_flocks();
  986. }
  987. unlock_flocks();
  988. out:
  989. up_write(&nfsi->rwsem);
  990. return status;
  991. }
  992. static int nfs4_reclaim_open_state(struct nfs4_state_owner *sp, const struct nfs4_state_recovery_ops *ops)
  993. {
  994. struct nfs4_state *state;
  995. struct nfs4_lock_state *lock;
  996. int status = 0;
  997. /* Note: we rely on the sp->so_states list being ordered
  998. * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
  999. * states first.
  1000. * This is needed to ensure that the server won't give us any
  1001. * read delegations that we have to return if, say, we are
  1002. * recovering after a network partition or a reboot from a
  1003. * server that doesn't support a grace period.
  1004. */
  1005. restart:
  1006. spin_lock(&sp->so_lock);
  1007. list_for_each_entry(state, &sp->so_states, open_states) {
  1008. if (!test_and_clear_bit(ops->state_flag_bit, &state->flags))
  1009. continue;
  1010. if (state->state == 0)
  1011. continue;
  1012. atomic_inc(&state->count);
  1013. spin_unlock(&sp->so_lock);
  1014. status = ops->recover_open(sp, state);
  1015. if (status >= 0) {
  1016. status = nfs4_reclaim_locks(state, ops);
  1017. if (status >= 0) {
  1018. list_for_each_entry(lock, &state->lock_states, ls_locks) {
  1019. if (!(lock->ls_flags & NFS_LOCK_INITIALIZED))
  1020. printk("%s: Lock reclaim failed!\n",
  1021. __func__);
  1022. }
  1023. nfs4_put_open_state(state);
  1024. goto restart;
  1025. }
  1026. }
  1027. switch (status) {
  1028. default:
  1029. printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
  1030. __func__, status);
  1031. case -ENOENT:
  1032. case -ENOMEM:
  1033. case -ESTALE:
  1034. /*
  1035. * Open state on this file cannot be recovered
  1036. * All we can do is revert to using the zero stateid.
  1037. */
  1038. memset(state->stateid.data, 0,
  1039. sizeof(state->stateid.data));
  1040. /* Mark the file as being 'closed' */
  1041. state->state = 0;
  1042. break;
  1043. case -EKEYEXPIRED:
  1044. /*
  1045. * User RPCSEC_GSS context has expired.
  1046. * We cannot recover this stateid now, so
  1047. * skip it and allow recovery thread to
  1048. * proceed.
  1049. */
  1050. break;
  1051. case -NFS4ERR_ADMIN_REVOKED:
  1052. case -NFS4ERR_STALE_STATEID:
  1053. case -NFS4ERR_BAD_STATEID:
  1054. case -NFS4ERR_RECLAIM_BAD:
  1055. case -NFS4ERR_RECLAIM_CONFLICT:
  1056. nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
  1057. break;
  1058. case -NFS4ERR_EXPIRED:
  1059. case -NFS4ERR_NO_GRACE:
  1060. nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
  1061. case -NFS4ERR_STALE_CLIENTID:
  1062. case -NFS4ERR_BADSESSION:
  1063. case -NFS4ERR_BADSLOT:
  1064. case -NFS4ERR_BAD_HIGH_SLOT:
  1065. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  1066. goto out_err;
  1067. }
  1068. nfs4_put_open_state(state);
  1069. goto restart;
  1070. }
  1071. spin_unlock(&sp->so_lock);
  1072. return 0;
  1073. out_err:
  1074. nfs4_put_open_state(state);
  1075. return status;
  1076. }
  1077. static void nfs4_clear_open_state(struct nfs4_state *state)
  1078. {
  1079. struct nfs4_lock_state *lock;
  1080. clear_bit(NFS_DELEGATED_STATE, &state->flags);
  1081. clear_bit(NFS_O_RDONLY_STATE, &state->flags);
  1082. clear_bit(NFS_O_WRONLY_STATE, &state->flags);
  1083. clear_bit(NFS_O_RDWR_STATE, &state->flags);
  1084. list_for_each_entry(lock, &state->lock_states, ls_locks) {
  1085. lock->ls_seqid.flags = 0;
  1086. lock->ls_flags &= ~NFS_LOCK_INITIALIZED;
  1087. }
  1088. }
  1089. static void nfs4_reset_seqids(struct nfs_server *server,
  1090. int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
  1091. {
  1092. struct nfs_client *clp = server->nfs_client;
  1093. struct nfs4_state_owner *sp;
  1094. struct rb_node *pos;
  1095. struct nfs4_state *state;
  1096. spin_lock(&clp->cl_lock);
  1097. for (pos = rb_first(&server->state_owners);
  1098. pos != NULL;
  1099. pos = rb_next(pos)) {
  1100. sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
  1101. sp->so_seqid.flags = 0;
  1102. spin_lock(&sp->so_lock);
  1103. list_for_each_entry(state, &sp->so_states, open_states) {
  1104. if (mark_reclaim(clp, state))
  1105. nfs4_clear_open_state(state);
  1106. }
  1107. spin_unlock(&sp->so_lock);
  1108. }
  1109. spin_unlock(&clp->cl_lock);
  1110. }
  1111. static void nfs4_state_mark_reclaim_helper(struct nfs_client *clp,
  1112. int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
  1113. {
  1114. struct nfs_server *server;
  1115. rcu_read_lock();
  1116. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  1117. nfs4_reset_seqids(server, mark_reclaim);
  1118. rcu_read_unlock();
  1119. }
  1120. static void nfs4_state_start_reclaim_reboot(struct nfs_client *clp)
  1121. {
  1122. /* Mark all delegations for reclaim */
  1123. nfs_delegation_mark_reclaim(clp);
  1124. nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_reboot);
  1125. }
  1126. static void nfs4_reclaim_complete(struct nfs_client *clp,
  1127. const struct nfs4_state_recovery_ops *ops)
  1128. {
  1129. /* Notify the server we're done reclaiming our state */
  1130. if (ops->reclaim_complete)
  1131. (void)ops->reclaim_complete(clp);
  1132. }
  1133. static void nfs4_clear_reclaim_server(struct nfs_server *server)
  1134. {
  1135. struct nfs_client *clp = server->nfs_client;
  1136. struct nfs4_state_owner *sp;
  1137. struct rb_node *pos;
  1138. struct nfs4_state *state;
  1139. spin_lock(&clp->cl_lock);
  1140. for (pos = rb_first(&server->state_owners);
  1141. pos != NULL;
  1142. pos = rb_next(pos)) {
  1143. sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
  1144. spin_lock(&sp->so_lock);
  1145. list_for_each_entry(state, &sp->so_states, open_states) {
  1146. if (!test_and_clear_bit(NFS_STATE_RECLAIM_REBOOT,
  1147. &state->flags))
  1148. continue;
  1149. nfs4_state_mark_reclaim_nograce(clp, state);
  1150. }
  1151. spin_unlock(&sp->so_lock);
  1152. }
  1153. spin_unlock(&clp->cl_lock);
  1154. }
  1155. static int nfs4_state_clear_reclaim_reboot(struct nfs_client *clp)
  1156. {
  1157. struct nfs_server *server;
  1158. if (!test_and_clear_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
  1159. return 0;
  1160. rcu_read_lock();
  1161. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  1162. nfs4_clear_reclaim_server(server);
  1163. rcu_read_unlock();
  1164. nfs_delegation_reap_unclaimed(clp);
  1165. return 1;
  1166. }
  1167. static void nfs4_state_end_reclaim_reboot(struct nfs_client *clp)
  1168. {
  1169. if (!nfs4_state_clear_reclaim_reboot(clp))
  1170. return;
  1171. nfs4_reclaim_complete(clp, clp->cl_mvops->reboot_recovery_ops);
  1172. }
  1173. static void nfs_delegation_clear_all(struct nfs_client *clp)
  1174. {
  1175. nfs_delegation_mark_reclaim(clp);
  1176. nfs_delegation_reap_unclaimed(clp);
  1177. }
  1178. static void nfs4_state_start_reclaim_nograce(struct nfs_client *clp)
  1179. {
  1180. nfs_delegation_clear_all(clp);
  1181. nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_nograce);
  1182. }
  1183. static void nfs4_warn_keyexpired(const char *s)
  1184. {
  1185. printk_ratelimited(KERN_WARNING "Error: state manager"
  1186. " encountered RPCSEC_GSS session"
  1187. " expired against NFSv4 server %s.\n",
  1188. s);
  1189. }
  1190. static int nfs4_recovery_handle_error(struct nfs_client *clp, int error)
  1191. {
  1192. switch (error) {
  1193. case -NFS4ERR_CB_PATH_DOWN:
  1194. nfs_handle_cb_pathdown(clp);
  1195. return 0;
  1196. case -NFS4ERR_NO_GRACE:
  1197. nfs4_state_end_reclaim_reboot(clp);
  1198. return 0;
  1199. case -NFS4ERR_STALE_CLIENTID:
  1200. case -NFS4ERR_LEASE_MOVED:
  1201. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  1202. nfs4_state_clear_reclaim_reboot(clp);
  1203. nfs4_state_start_reclaim_reboot(clp);
  1204. break;
  1205. case -NFS4ERR_EXPIRED:
  1206. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  1207. nfs4_state_start_reclaim_nograce(clp);
  1208. break;
  1209. case -NFS4ERR_BADSESSION:
  1210. case -NFS4ERR_BADSLOT:
  1211. case -NFS4ERR_BAD_HIGH_SLOT:
  1212. case -NFS4ERR_DEADSESSION:
  1213. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  1214. case -NFS4ERR_SEQ_FALSE_RETRY:
  1215. case -NFS4ERR_SEQ_MISORDERED:
  1216. set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
  1217. /* Zero session reset errors */
  1218. return 0;
  1219. case -EKEYEXPIRED:
  1220. /* Nothing we can do */
  1221. nfs4_warn_keyexpired(clp->cl_hostname);
  1222. return 0;
  1223. }
  1224. return error;
  1225. }
  1226. static int nfs4_do_reclaim(struct nfs_client *clp, const struct nfs4_state_recovery_ops *ops)
  1227. {
  1228. struct nfs4_state_owner *sp;
  1229. struct nfs_server *server;
  1230. struct rb_node *pos;
  1231. int status = 0;
  1232. restart:
  1233. rcu_read_lock();
  1234. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  1235. spin_lock(&clp->cl_lock);
  1236. for (pos = rb_first(&server->state_owners);
  1237. pos != NULL;
  1238. pos = rb_next(pos)) {
  1239. sp = rb_entry(pos,
  1240. struct nfs4_state_owner, so_server_node);
  1241. if (!test_and_clear_bit(ops->owner_flag_bit,
  1242. &sp->so_flags))
  1243. continue;
  1244. atomic_inc(&sp->so_count);
  1245. spin_unlock(&clp->cl_lock);
  1246. rcu_read_unlock();
  1247. status = nfs4_reclaim_open_state(sp, ops);
  1248. if (status < 0) {
  1249. set_bit(ops->owner_flag_bit, &sp->so_flags);
  1250. nfs4_put_state_owner(sp);
  1251. return nfs4_recovery_handle_error(clp, status);
  1252. }
  1253. nfs4_put_state_owner(sp);
  1254. goto restart;
  1255. }
  1256. spin_unlock(&clp->cl_lock);
  1257. }
  1258. rcu_read_unlock();
  1259. return status;
  1260. }
  1261. static int nfs4_check_lease(struct nfs_client *clp)
  1262. {
  1263. struct rpc_cred *cred;
  1264. const struct nfs4_state_maintenance_ops *ops =
  1265. clp->cl_mvops->state_renewal_ops;
  1266. int status = -NFS4ERR_EXPIRED;
  1267. /* Is the client already known to have an expired lease? */
  1268. if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
  1269. return 0;
  1270. spin_lock(&clp->cl_lock);
  1271. cred = ops->get_state_renewal_cred_locked(clp);
  1272. spin_unlock(&clp->cl_lock);
  1273. if (cred == NULL) {
  1274. cred = nfs4_get_setclientid_cred(clp);
  1275. if (cred == NULL)
  1276. goto out;
  1277. }
  1278. status = ops->renew_lease(clp, cred);
  1279. put_rpccred(cred);
  1280. out:
  1281. return nfs4_recovery_handle_error(clp, status);
  1282. }
  1283. static int nfs4_reclaim_lease(struct nfs_client *clp)
  1284. {
  1285. struct rpc_cred *cred;
  1286. const struct nfs4_state_recovery_ops *ops =
  1287. clp->cl_mvops->reboot_recovery_ops;
  1288. int status = -ENOENT;
  1289. cred = ops->get_clid_cred(clp);
  1290. if (cred != NULL) {
  1291. status = ops->establish_clid(clp, cred);
  1292. put_rpccred(cred);
  1293. /* Handle case where the user hasn't set up machine creds */
  1294. if (status == -EACCES && cred == clp->cl_machine_cred) {
  1295. nfs4_clear_machine_cred(clp);
  1296. status = -EAGAIN;
  1297. }
  1298. if (status == -NFS4ERR_MINOR_VERS_MISMATCH)
  1299. status = -EPROTONOSUPPORT;
  1300. }
  1301. return status;
  1302. }
  1303. #ifdef CONFIG_NFS_V4_1
  1304. void nfs4_schedule_session_recovery(struct nfs4_session *session)
  1305. {
  1306. nfs4_schedule_lease_recovery(session->clp);
  1307. }
  1308. EXPORT_SYMBOL_GPL(nfs4_schedule_session_recovery);
  1309. void nfs41_handle_recall_slot(struct nfs_client *clp)
  1310. {
  1311. set_bit(NFS4CLNT_RECALL_SLOT, &clp->cl_state);
  1312. nfs4_schedule_state_manager(clp);
  1313. }
  1314. static void nfs4_reset_all_state(struct nfs_client *clp)
  1315. {
  1316. if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
  1317. clp->cl_boot_time = CURRENT_TIME;
  1318. nfs4_state_start_reclaim_nograce(clp);
  1319. nfs4_schedule_state_manager(clp);
  1320. }
  1321. }
  1322. static void nfs41_handle_server_reboot(struct nfs_client *clp)
  1323. {
  1324. if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
  1325. nfs4_state_start_reclaim_reboot(clp);
  1326. nfs4_schedule_state_manager(clp);
  1327. }
  1328. }
  1329. static void nfs41_handle_state_revoked(struct nfs_client *clp)
  1330. {
  1331. /* Temporary */
  1332. nfs4_reset_all_state(clp);
  1333. }
  1334. static void nfs41_handle_recallable_state_revoked(struct nfs_client *clp)
  1335. {
  1336. /* This will need to handle layouts too */
  1337. nfs_expire_all_delegations(clp);
  1338. }
  1339. static void nfs41_handle_cb_path_down(struct nfs_client *clp)
  1340. {
  1341. nfs_expire_all_delegations(clp);
  1342. if (test_and_set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state) == 0)
  1343. nfs4_schedule_state_manager(clp);
  1344. }
  1345. void nfs41_handle_sequence_flag_errors(struct nfs_client *clp, u32 flags)
  1346. {
  1347. if (!flags)
  1348. return;
  1349. else if (flags & SEQ4_STATUS_RESTART_RECLAIM_NEEDED)
  1350. nfs41_handle_server_reboot(clp);
  1351. else if (flags & (SEQ4_STATUS_EXPIRED_ALL_STATE_REVOKED |
  1352. SEQ4_STATUS_EXPIRED_SOME_STATE_REVOKED |
  1353. SEQ4_STATUS_ADMIN_STATE_REVOKED |
  1354. SEQ4_STATUS_LEASE_MOVED))
  1355. nfs41_handle_state_revoked(clp);
  1356. else if (flags & SEQ4_STATUS_RECALLABLE_STATE_REVOKED)
  1357. nfs41_handle_recallable_state_revoked(clp);
  1358. else if (flags & (SEQ4_STATUS_CB_PATH_DOWN |
  1359. SEQ4_STATUS_BACKCHANNEL_FAULT |
  1360. SEQ4_STATUS_CB_PATH_DOWN_SESSION))
  1361. nfs41_handle_cb_path_down(clp);
  1362. }
  1363. static int nfs4_reset_session(struct nfs_client *clp)
  1364. {
  1365. int status;
  1366. nfs4_begin_drain_session(clp);
  1367. status = nfs4_proc_destroy_session(clp->cl_session);
  1368. if (status && status != -NFS4ERR_BADSESSION &&
  1369. status != -NFS4ERR_DEADSESSION) {
  1370. status = nfs4_recovery_handle_error(clp, status);
  1371. goto out;
  1372. }
  1373. memset(clp->cl_session->sess_id.data, 0, NFS4_MAX_SESSIONID_LEN);
  1374. status = nfs4_proc_create_session(clp);
  1375. if (status) {
  1376. status = nfs4_recovery_handle_error(clp, status);
  1377. goto out;
  1378. }
  1379. /* create_session negotiated new slot table */
  1380. clear_bit(NFS4CLNT_RECALL_SLOT, &clp->cl_state);
  1381. /* Let the state manager reestablish state */
  1382. if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
  1383. nfs41_setup_state_renewal(clp);
  1384. out:
  1385. return status;
  1386. }
  1387. static int nfs4_recall_slot(struct nfs_client *clp)
  1388. {
  1389. struct nfs4_slot_table *fc_tbl = &clp->cl_session->fc_slot_table;
  1390. struct nfs4_channel_attrs *fc_attrs = &clp->cl_session->fc_attrs;
  1391. struct nfs4_slot *new, *old;
  1392. int i;
  1393. nfs4_begin_drain_session(clp);
  1394. new = kmalloc(fc_tbl->target_max_slots * sizeof(struct nfs4_slot),
  1395. GFP_NOFS);
  1396. if (!new)
  1397. return -ENOMEM;
  1398. spin_lock(&fc_tbl->slot_tbl_lock);
  1399. for (i = 0; i < fc_tbl->target_max_slots; i++)
  1400. new[i].seq_nr = fc_tbl->slots[i].seq_nr;
  1401. old = fc_tbl->slots;
  1402. fc_tbl->slots = new;
  1403. fc_tbl->max_slots = fc_tbl->target_max_slots;
  1404. fc_tbl->target_max_slots = 0;
  1405. fc_attrs->max_reqs = fc_tbl->max_slots;
  1406. spin_unlock(&fc_tbl->slot_tbl_lock);
  1407. kfree(old);
  1408. nfs4_end_drain_session(clp);
  1409. return 0;
  1410. }
  1411. #else /* CONFIG_NFS_V4_1 */
  1412. static int nfs4_reset_session(struct nfs_client *clp) { return 0; }
  1413. static int nfs4_end_drain_session(struct nfs_client *clp) { return 0; }
  1414. static int nfs4_recall_slot(struct nfs_client *clp) { return 0; }
  1415. #endif /* CONFIG_NFS_V4_1 */
  1416. /* Set NFS4CLNT_LEASE_EXPIRED for all v4.0 errors and for recoverable errors
  1417. * on EXCHANGE_ID for v4.1
  1418. */
  1419. static void nfs4_set_lease_expired(struct nfs_client *clp, int status)
  1420. {
  1421. if (nfs4_has_session(clp)) {
  1422. switch (status) {
  1423. case -NFS4ERR_DELAY:
  1424. case -NFS4ERR_CLID_INUSE:
  1425. case -EAGAIN:
  1426. break;
  1427. case -EKEYEXPIRED:
  1428. nfs4_warn_keyexpired(clp->cl_hostname);
  1429. case -NFS4ERR_NOT_SAME: /* FixMe: implement recovery
  1430. * in nfs4_exchange_id */
  1431. default:
  1432. return;
  1433. }
  1434. }
  1435. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  1436. }
  1437. static void nfs4_state_manager(struct nfs_client *clp)
  1438. {
  1439. int status = 0;
  1440. /* Ensure exclusive access to NFSv4 state */
  1441. for(;;) {
  1442. if (test_and_clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state)) {
  1443. /* We're going to have to re-establish a clientid */
  1444. status = nfs4_reclaim_lease(clp);
  1445. if (status) {
  1446. nfs4_set_lease_expired(clp, status);
  1447. if (test_bit(NFS4CLNT_LEASE_EXPIRED,
  1448. &clp->cl_state))
  1449. continue;
  1450. if (clp->cl_cons_state ==
  1451. NFS_CS_SESSION_INITING)
  1452. nfs_mark_client_ready(clp, status);
  1453. goto out_error;
  1454. }
  1455. clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
  1456. set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
  1457. pnfs_destroy_all_layouts(clp);
  1458. }
  1459. if (test_and_clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state)) {
  1460. status = nfs4_check_lease(clp);
  1461. if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
  1462. continue;
  1463. if (status < 0 && status != -NFS4ERR_CB_PATH_DOWN)
  1464. goto out_error;
  1465. }
  1466. /* Initialize or reset the session */
  1467. if (test_and_clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state)
  1468. && nfs4_has_session(clp)) {
  1469. status = nfs4_reset_session(clp);
  1470. if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
  1471. continue;
  1472. if (status < 0)
  1473. goto out_error;
  1474. }
  1475. /* First recover reboot state... */
  1476. if (test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state)) {
  1477. status = nfs4_do_reclaim(clp,
  1478. clp->cl_mvops->reboot_recovery_ops);
  1479. if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) ||
  1480. test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state))
  1481. continue;
  1482. nfs4_state_end_reclaim_reboot(clp);
  1483. if (test_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state))
  1484. continue;
  1485. if (status < 0)
  1486. goto out_error;
  1487. }
  1488. /* Now recover expired state... */
  1489. if (test_and_clear_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state)) {
  1490. status = nfs4_do_reclaim(clp,
  1491. clp->cl_mvops->nograce_recovery_ops);
  1492. if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) ||
  1493. test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state) ||
  1494. test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
  1495. continue;
  1496. if (status < 0)
  1497. goto out_error;
  1498. }
  1499. nfs4_end_drain_session(clp);
  1500. if (test_and_clear_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state)) {
  1501. nfs_client_return_marked_delegations(clp);
  1502. continue;
  1503. }
  1504. /* Recall session slots */
  1505. if (test_and_clear_bit(NFS4CLNT_RECALL_SLOT, &clp->cl_state)
  1506. && nfs4_has_session(clp)) {
  1507. status = nfs4_recall_slot(clp);
  1508. if (status < 0)
  1509. goto out_error;
  1510. continue;
  1511. }
  1512. nfs4_clear_state_manager_bit(clp);
  1513. /* Did we race with an attempt to give us more work? */
  1514. if (clp->cl_state == 0)
  1515. break;
  1516. if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
  1517. break;
  1518. }
  1519. return;
  1520. out_error:
  1521. printk(KERN_WARNING "Error: state manager failed on NFSv4 server %s"
  1522. " with error %d\n", clp->cl_hostname, -status);
  1523. nfs4_end_drain_session(clp);
  1524. nfs4_clear_state_manager_bit(clp);
  1525. }
  1526. static int nfs4_run_state_manager(void *ptr)
  1527. {
  1528. struct nfs_client *clp = ptr;
  1529. allow_signal(SIGKILL);
  1530. nfs4_state_manager(clp);
  1531. nfs_put_client(clp);
  1532. module_put_and_exit(0);
  1533. return 0;
  1534. }
  1535. /*
  1536. * Local variables:
  1537. * c-basic-offset: 8
  1538. * End:
  1539. */