nfs4state.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274
  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/smp_lock.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/workqueue.h>
  49. #include <linux/bitops.h>
  50. #include "nfs4_fs.h"
  51. #include "callback.h"
  52. #include "delegation.h"
  53. #include "internal.h"
  54. #define OPENOWNER_POOL_SIZE 8
  55. const nfs4_stateid zero_stateid;
  56. static LIST_HEAD(nfs4_clientid_list);
  57. static int nfs4_init_client(struct nfs_client *clp, struct rpc_cred *cred)
  58. {
  59. unsigned short port;
  60. int status;
  61. port = nfs_callback_tcpport;
  62. if (clp->cl_addr.ss_family == AF_INET6)
  63. port = nfs_callback_tcpport6;
  64. status = nfs4_proc_setclientid(clp, NFS4_CALLBACK, port, cred);
  65. if (status == 0)
  66. status = nfs4_proc_setclientid_confirm(clp, cred);
  67. if (status == 0)
  68. nfs4_schedule_state_renewal(clp);
  69. return status;
  70. }
  71. struct rpc_cred *nfs4_get_machine_cred_locked(struct nfs_client *clp)
  72. {
  73. struct rpc_cred *cred = NULL;
  74. if (clp->cl_machine_cred != NULL)
  75. cred = get_rpccred(clp->cl_machine_cred);
  76. return cred;
  77. }
  78. static void nfs4_clear_machine_cred(struct nfs_client *clp)
  79. {
  80. struct rpc_cred *cred;
  81. spin_lock(&clp->cl_lock);
  82. cred = clp->cl_machine_cred;
  83. clp->cl_machine_cred = NULL;
  84. spin_unlock(&clp->cl_lock);
  85. if (cred != NULL)
  86. put_rpccred(cred);
  87. }
  88. struct rpc_cred *nfs4_get_renew_cred_locked(struct nfs_client *clp)
  89. {
  90. struct nfs4_state_owner *sp;
  91. struct rb_node *pos;
  92. struct rpc_cred *cred = NULL;
  93. for (pos = rb_first(&clp->cl_state_owners); pos != NULL; pos = rb_next(pos)) {
  94. sp = rb_entry(pos, struct nfs4_state_owner, so_client_node);
  95. if (list_empty(&sp->so_states))
  96. continue;
  97. cred = get_rpccred(sp->so_cred);
  98. break;
  99. }
  100. return cred;
  101. }
  102. struct rpc_cred *nfs4_get_setclientid_cred(struct nfs_client *clp)
  103. {
  104. struct nfs4_state_owner *sp;
  105. struct rb_node *pos;
  106. struct rpc_cred *cred;
  107. spin_lock(&clp->cl_lock);
  108. cred = nfs4_get_machine_cred_locked(clp);
  109. if (cred != NULL)
  110. goto out;
  111. pos = rb_first(&clp->cl_state_owners);
  112. if (pos != NULL) {
  113. sp = rb_entry(pos, struct nfs4_state_owner, so_client_node);
  114. cred = get_rpccred(sp->so_cred);
  115. }
  116. out:
  117. spin_unlock(&clp->cl_lock);
  118. return cred;
  119. }
  120. static void nfs_alloc_unique_id(struct rb_root *root, struct nfs_unique_id *new,
  121. __u64 minval, int maxbits)
  122. {
  123. struct rb_node **p, *parent;
  124. struct nfs_unique_id *pos;
  125. __u64 mask = ~0ULL;
  126. if (maxbits < 64)
  127. mask = (1ULL << maxbits) - 1ULL;
  128. /* Ensure distribution is more or less flat */
  129. get_random_bytes(&new->id, sizeof(new->id));
  130. new->id &= mask;
  131. if (new->id < minval)
  132. new->id += minval;
  133. retry:
  134. p = &root->rb_node;
  135. parent = NULL;
  136. while (*p != NULL) {
  137. parent = *p;
  138. pos = rb_entry(parent, struct nfs_unique_id, rb_node);
  139. if (new->id < pos->id)
  140. p = &(*p)->rb_left;
  141. else if (new->id > pos->id)
  142. p = &(*p)->rb_right;
  143. else
  144. goto id_exists;
  145. }
  146. rb_link_node(&new->rb_node, parent, p);
  147. rb_insert_color(&new->rb_node, root);
  148. return;
  149. id_exists:
  150. for (;;) {
  151. new->id++;
  152. if (new->id < minval || (new->id & mask) != new->id) {
  153. new->id = minval;
  154. break;
  155. }
  156. parent = rb_next(parent);
  157. if (parent == NULL)
  158. break;
  159. pos = rb_entry(parent, struct nfs_unique_id, rb_node);
  160. if (new->id < pos->id)
  161. break;
  162. }
  163. goto retry;
  164. }
  165. static void nfs_free_unique_id(struct rb_root *root, struct nfs_unique_id *id)
  166. {
  167. rb_erase(&id->rb_node, root);
  168. }
  169. static struct nfs4_state_owner *
  170. nfs4_find_state_owner(struct nfs_server *server, struct rpc_cred *cred)
  171. {
  172. struct nfs_client *clp = server->nfs_client;
  173. struct rb_node **p = &clp->cl_state_owners.rb_node,
  174. *parent = NULL;
  175. struct nfs4_state_owner *sp, *res = NULL;
  176. while (*p != NULL) {
  177. parent = *p;
  178. sp = rb_entry(parent, struct nfs4_state_owner, so_client_node);
  179. if (server < sp->so_server) {
  180. p = &parent->rb_left;
  181. continue;
  182. }
  183. if (server > sp->so_server) {
  184. p = &parent->rb_right;
  185. continue;
  186. }
  187. if (cred < sp->so_cred)
  188. p = &parent->rb_left;
  189. else if (cred > sp->so_cred)
  190. p = &parent->rb_right;
  191. else {
  192. atomic_inc(&sp->so_count);
  193. res = sp;
  194. break;
  195. }
  196. }
  197. return res;
  198. }
  199. static struct nfs4_state_owner *
  200. nfs4_insert_state_owner(struct nfs_client *clp, struct nfs4_state_owner *new)
  201. {
  202. struct rb_node **p = &clp->cl_state_owners.rb_node,
  203. *parent = NULL;
  204. struct nfs4_state_owner *sp;
  205. while (*p != NULL) {
  206. parent = *p;
  207. sp = rb_entry(parent, struct nfs4_state_owner, so_client_node);
  208. if (new->so_server < sp->so_server) {
  209. p = &parent->rb_left;
  210. continue;
  211. }
  212. if (new->so_server > sp->so_server) {
  213. p = &parent->rb_right;
  214. continue;
  215. }
  216. if (new->so_cred < sp->so_cred)
  217. p = &parent->rb_left;
  218. else if (new->so_cred > sp->so_cred)
  219. p = &parent->rb_right;
  220. else {
  221. atomic_inc(&sp->so_count);
  222. return sp;
  223. }
  224. }
  225. nfs_alloc_unique_id(&clp->cl_openowner_id, &new->so_owner_id, 1, 64);
  226. rb_link_node(&new->so_client_node, parent, p);
  227. rb_insert_color(&new->so_client_node, &clp->cl_state_owners);
  228. return new;
  229. }
  230. static void
  231. nfs4_remove_state_owner(struct nfs_client *clp, struct nfs4_state_owner *sp)
  232. {
  233. if (!RB_EMPTY_NODE(&sp->so_client_node))
  234. rb_erase(&sp->so_client_node, &clp->cl_state_owners);
  235. nfs_free_unique_id(&clp->cl_openowner_id, &sp->so_owner_id);
  236. }
  237. /*
  238. * nfs4_alloc_state_owner(): this is called on the OPEN or CREATE path to
  239. * create a new state_owner.
  240. *
  241. */
  242. static struct nfs4_state_owner *
  243. nfs4_alloc_state_owner(void)
  244. {
  245. struct nfs4_state_owner *sp;
  246. sp = kzalloc(sizeof(*sp),GFP_KERNEL);
  247. if (!sp)
  248. return NULL;
  249. spin_lock_init(&sp->so_lock);
  250. INIT_LIST_HEAD(&sp->so_states);
  251. INIT_LIST_HEAD(&sp->so_delegations);
  252. rpc_init_wait_queue(&sp->so_sequence.wait, "Seqid_waitqueue");
  253. sp->so_seqid.sequence = &sp->so_sequence;
  254. spin_lock_init(&sp->so_sequence.lock);
  255. INIT_LIST_HEAD(&sp->so_sequence.list);
  256. atomic_set(&sp->so_count, 1);
  257. return sp;
  258. }
  259. static void
  260. nfs4_drop_state_owner(struct nfs4_state_owner *sp)
  261. {
  262. if (!RB_EMPTY_NODE(&sp->so_client_node)) {
  263. struct nfs_client *clp = sp->so_client;
  264. spin_lock(&clp->cl_lock);
  265. rb_erase(&sp->so_client_node, &clp->cl_state_owners);
  266. RB_CLEAR_NODE(&sp->so_client_node);
  267. spin_unlock(&clp->cl_lock);
  268. }
  269. }
  270. struct nfs4_state_owner *nfs4_get_state_owner(struct nfs_server *server, struct rpc_cred *cred)
  271. {
  272. struct nfs_client *clp = server->nfs_client;
  273. struct nfs4_state_owner *sp, *new;
  274. spin_lock(&clp->cl_lock);
  275. sp = nfs4_find_state_owner(server, cred);
  276. spin_unlock(&clp->cl_lock);
  277. if (sp != NULL)
  278. return sp;
  279. new = nfs4_alloc_state_owner();
  280. if (new == NULL)
  281. return NULL;
  282. new->so_client = clp;
  283. new->so_server = server;
  284. new->so_cred = cred;
  285. spin_lock(&clp->cl_lock);
  286. sp = nfs4_insert_state_owner(clp, new);
  287. spin_unlock(&clp->cl_lock);
  288. if (sp == new)
  289. get_rpccred(cred);
  290. else {
  291. rpc_destroy_wait_queue(&new->so_sequence.wait);
  292. kfree(new);
  293. }
  294. return sp;
  295. }
  296. void nfs4_put_state_owner(struct nfs4_state_owner *sp)
  297. {
  298. struct nfs_client *clp = sp->so_client;
  299. struct rpc_cred *cred = sp->so_cred;
  300. if (!atomic_dec_and_lock(&sp->so_count, &clp->cl_lock))
  301. return;
  302. nfs4_remove_state_owner(clp, sp);
  303. spin_unlock(&clp->cl_lock);
  304. rpc_destroy_wait_queue(&sp->so_sequence.wait);
  305. put_rpccred(cred);
  306. kfree(sp);
  307. }
  308. static struct nfs4_state *
  309. nfs4_alloc_open_state(void)
  310. {
  311. struct nfs4_state *state;
  312. state = kzalloc(sizeof(*state), GFP_KERNEL);
  313. if (!state)
  314. return NULL;
  315. atomic_set(&state->count, 1);
  316. INIT_LIST_HEAD(&state->lock_states);
  317. spin_lock_init(&state->state_lock);
  318. seqlock_init(&state->seqlock);
  319. return state;
  320. }
  321. void
  322. nfs4_state_set_mode_locked(struct nfs4_state *state, fmode_t fmode)
  323. {
  324. if (state->state == fmode)
  325. return;
  326. /* NB! List reordering - see the reclaim code for why. */
  327. if ((fmode & FMODE_WRITE) != (state->state & FMODE_WRITE)) {
  328. if (fmode & FMODE_WRITE)
  329. list_move(&state->open_states, &state->owner->so_states);
  330. else
  331. list_move_tail(&state->open_states, &state->owner->so_states);
  332. }
  333. state->state = fmode;
  334. }
  335. static struct nfs4_state *
  336. __nfs4_find_state_byowner(struct inode *inode, struct nfs4_state_owner *owner)
  337. {
  338. struct nfs_inode *nfsi = NFS_I(inode);
  339. struct nfs4_state *state;
  340. list_for_each_entry(state, &nfsi->open_states, inode_states) {
  341. if (state->owner != owner)
  342. continue;
  343. if (atomic_inc_not_zero(&state->count))
  344. return state;
  345. }
  346. return NULL;
  347. }
  348. static void
  349. nfs4_free_open_state(struct nfs4_state *state)
  350. {
  351. kfree(state);
  352. }
  353. struct nfs4_state *
  354. nfs4_get_open_state(struct inode *inode, struct nfs4_state_owner *owner)
  355. {
  356. struct nfs4_state *state, *new;
  357. struct nfs_inode *nfsi = NFS_I(inode);
  358. spin_lock(&inode->i_lock);
  359. state = __nfs4_find_state_byowner(inode, owner);
  360. spin_unlock(&inode->i_lock);
  361. if (state)
  362. goto out;
  363. new = nfs4_alloc_open_state();
  364. spin_lock(&owner->so_lock);
  365. spin_lock(&inode->i_lock);
  366. state = __nfs4_find_state_byowner(inode, owner);
  367. if (state == NULL && new != NULL) {
  368. state = new;
  369. state->owner = owner;
  370. atomic_inc(&owner->so_count);
  371. list_add(&state->inode_states, &nfsi->open_states);
  372. state->inode = igrab(inode);
  373. spin_unlock(&inode->i_lock);
  374. /* Note: The reclaim code dictates that we add stateless
  375. * and read-only stateids to the end of the list */
  376. list_add_tail(&state->open_states, &owner->so_states);
  377. spin_unlock(&owner->so_lock);
  378. } else {
  379. spin_unlock(&inode->i_lock);
  380. spin_unlock(&owner->so_lock);
  381. if (new)
  382. nfs4_free_open_state(new);
  383. }
  384. out:
  385. return state;
  386. }
  387. void nfs4_put_open_state(struct nfs4_state *state)
  388. {
  389. struct inode *inode = state->inode;
  390. struct nfs4_state_owner *owner = state->owner;
  391. if (!atomic_dec_and_lock(&state->count, &owner->so_lock))
  392. return;
  393. spin_lock(&inode->i_lock);
  394. list_del(&state->inode_states);
  395. list_del(&state->open_states);
  396. spin_unlock(&inode->i_lock);
  397. spin_unlock(&owner->so_lock);
  398. iput(inode);
  399. nfs4_free_open_state(state);
  400. nfs4_put_state_owner(owner);
  401. }
  402. /*
  403. * Close the current file.
  404. */
  405. static void __nfs4_close(struct path *path, struct nfs4_state *state, fmode_t fmode, int wait)
  406. {
  407. struct nfs4_state_owner *owner = state->owner;
  408. int call_close = 0;
  409. fmode_t newstate;
  410. atomic_inc(&owner->so_count);
  411. /* Protect against nfs4_find_state() */
  412. spin_lock(&owner->so_lock);
  413. switch (fmode & (FMODE_READ | FMODE_WRITE)) {
  414. case FMODE_READ:
  415. state->n_rdonly--;
  416. break;
  417. case FMODE_WRITE:
  418. state->n_wronly--;
  419. break;
  420. case FMODE_READ|FMODE_WRITE:
  421. state->n_rdwr--;
  422. }
  423. newstate = FMODE_READ|FMODE_WRITE;
  424. if (state->n_rdwr == 0) {
  425. if (state->n_rdonly == 0) {
  426. newstate &= ~FMODE_READ;
  427. call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
  428. call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
  429. }
  430. if (state->n_wronly == 0) {
  431. newstate &= ~FMODE_WRITE;
  432. call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
  433. call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
  434. }
  435. if (newstate == 0)
  436. clear_bit(NFS_DELEGATED_STATE, &state->flags);
  437. }
  438. nfs4_state_set_mode_locked(state, newstate);
  439. spin_unlock(&owner->so_lock);
  440. if (!call_close) {
  441. nfs4_put_open_state(state);
  442. nfs4_put_state_owner(owner);
  443. } else
  444. nfs4_do_close(path, state, wait);
  445. }
  446. void nfs4_close_state(struct path *path, struct nfs4_state *state, fmode_t fmode)
  447. {
  448. __nfs4_close(path, state, fmode, 0);
  449. }
  450. void nfs4_close_sync(struct path *path, struct nfs4_state *state, fmode_t fmode)
  451. {
  452. __nfs4_close(path, state, fmode, 1);
  453. }
  454. /*
  455. * Search the state->lock_states for an existing lock_owner
  456. * that is compatible with current->files
  457. */
  458. static struct nfs4_lock_state *
  459. __nfs4_find_lock_state(struct nfs4_state *state, fl_owner_t fl_owner)
  460. {
  461. struct nfs4_lock_state *pos;
  462. list_for_each_entry(pos, &state->lock_states, ls_locks) {
  463. if (pos->ls_owner != fl_owner)
  464. continue;
  465. atomic_inc(&pos->ls_count);
  466. return pos;
  467. }
  468. return NULL;
  469. }
  470. /*
  471. * Return a compatible lock_state. If no initialized lock_state structure
  472. * exists, return an uninitialized one.
  473. *
  474. */
  475. static struct nfs4_lock_state *nfs4_alloc_lock_state(struct nfs4_state *state, fl_owner_t fl_owner)
  476. {
  477. struct nfs4_lock_state *lsp;
  478. struct nfs_client *clp = state->owner->so_client;
  479. lsp = kzalloc(sizeof(*lsp), GFP_KERNEL);
  480. if (lsp == NULL)
  481. return NULL;
  482. rpc_init_wait_queue(&lsp->ls_sequence.wait, "lock_seqid_waitqueue");
  483. spin_lock_init(&lsp->ls_sequence.lock);
  484. INIT_LIST_HEAD(&lsp->ls_sequence.list);
  485. lsp->ls_seqid.sequence = &lsp->ls_sequence;
  486. atomic_set(&lsp->ls_count, 1);
  487. lsp->ls_owner = fl_owner;
  488. spin_lock(&clp->cl_lock);
  489. nfs_alloc_unique_id(&clp->cl_lockowner_id, &lsp->ls_id, 1, 64);
  490. spin_unlock(&clp->cl_lock);
  491. INIT_LIST_HEAD(&lsp->ls_locks);
  492. return lsp;
  493. }
  494. static void nfs4_free_lock_state(struct nfs4_lock_state *lsp)
  495. {
  496. struct nfs_client *clp = lsp->ls_state->owner->so_client;
  497. spin_lock(&clp->cl_lock);
  498. nfs_free_unique_id(&clp->cl_lockowner_id, &lsp->ls_id);
  499. spin_unlock(&clp->cl_lock);
  500. rpc_destroy_wait_queue(&lsp->ls_sequence.wait);
  501. kfree(lsp);
  502. }
  503. /*
  504. * Return a compatible lock_state. If no initialized lock_state structure
  505. * exists, return an uninitialized one.
  506. *
  507. */
  508. static struct nfs4_lock_state *nfs4_get_lock_state(struct nfs4_state *state, fl_owner_t owner)
  509. {
  510. struct nfs4_lock_state *lsp, *new = NULL;
  511. for(;;) {
  512. spin_lock(&state->state_lock);
  513. lsp = __nfs4_find_lock_state(state, owner);
  514. if (lsp != NULL)
  515. break;
  516. if (new != NULL) {
  517. new->ls_state = state;
  518. list_add(&new->ls_locks, &state->lock_states);
  519. set_bit(LK_STATE_IN_USE, &state->flags);
  520. lsp = new;
  521. new = NULL;
  522. break;
  523. }
  524. spin_unlock(&state->state_lock);
  525. new = nfs4_alloc_lock_state(state, owner);
  526. if (new == NULL)
  527. return NULL;
  528. }
  529. spin_unlock(&state->state_lock);
  530. if (new != NULL)
  531. nfs4_free_lock_state(new);
  532. return lsp;
  533. }
  534. /*
  535. * Release reference to lock_state, and free it if we see that
  536. * it is no longer in use
  537. */
  538. void nfs4_put_lock_state(struct nfs4_lock_state *lsp)
  539. {
  540. struct nfs4_state *state;
  541. if (lsp == NULL)
  542. return;
  543. state = lsp->ls_state;
  544. if (!atomic_dec_and_lock(&lsp->ls_count, &state->state_lock))
  545. return;
  546. list_del(&lsp->ls_locks);
  547. if (list_empty(&state->lock_states))
  548. clear_bit(LK_STATE_IN_USE, &state->flags);
  549. spin_unlock(&state->state_lock);
  550. nfs4_free_lock_state(lsp);
  551. }
  552. static void nfs4_fl_copy_lock(struct file_lock *dst, struct file_lock *src)
  553. {
  554. struct nfs4_lock_state *lsp = src->fl_u.nfs4_fl.owner;
  555. dst->fl_u.nfs4_fl.owner = lsp;
  556. atomic_inc(&lsp->ls_count);
  557. }
  558. static void nfs4_fl_release_lock(struct file_lock *fl)
  559. {
  560. nfs4_put_lock_state(fl->fl_u.nfs4_fl.owner);
  561. }
  562. static struct file_lock_operations nfs4_fl_lock_ops = {
  563. .fl_copy_lock = nfs4_fl_copy_lock,
  564. .fl_release_private = nfs4_fl_release_lock,
  565. };
  566. int nfs4_set_lock_state(struct nfs4_state *state, struct file_lock *fl)
  567. {
  568. struct nfs4_lock_state *lsp;
  569. if (fl->fl_ops != NULL)
  570. return 0;
  571. lsp = nfs4_get_lock_state(state, fl->fl_owner);
  572. if (lsp == NULL)
  573. return -ENOMEM;
  574. fl->fl_u.nfs4_fl.owner = lsp;
  575. fl->fl_ops = &nfs4_fl_lock_ops;
  576. return 0;
  577. }
  578. /*
  579. * Byte-range lock aware utility to initialize the stateid of read/write
  580. * requests.
  581. */
  582. void nfs4_copy_stateid(nfs4_stateid *dst, struct nfs4_state *state, fl_owner_t fl_owner)
  583. {
  584. struct nfs4_lock_state *lsp;
  585. int seq;
  586. do {
  587. seq = read_seqbegin(&state->seqlock);
  588. memcpy(dst, &state->stateid, sizeof(*dst));
  589. } while (read_seqretry(&state->seqlock, seq));
  590. if (test_bit(LK_STATE_IN_USE, &state->flags) == 0)
  591. return;
  592. spin_lock(&state->state_lock);
  593. lsp = __nfs4_find_lock_state(state, fl_owner);
  594. if (lsp != NULL && (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
  595. memcpy(dst, &lsp->ls_stateid, sizeof(*dst));
  596. spin_unlock(&state->state_lock);
  597. nfs4_put_lock_state(lsp);
  598. }
  599. struct nfs_seqid *nfs_alloc_seqid(struct nfs_seqid_counter *counter)
  600. {
  601. struct nfs_seqid *new;
  602. new = kmalloc(sizeof(*new), GFP_KERNEL);
  603. if (new != NULL) {
  604. new->sequence = counter;
  605. INIT_LIST_HEAD(&new->list);
  606. }
  607. return new;
  608. }
  609. void nfs_free_seqid(struct nfs_seqid *seqid)
  610. {
  611. if (!list_empty(&seqid->list)) {
  612. struct rpc_sequence *sequence = seqid->sequence->sequence;
  613. spin_lock(&sequence->lock);
  614. list_del(&seqid->list);
  615. spin_unlock(&sequence->lock);
  616. rpc_wake_up(&sequence->wait);
  617. }
  618. kfree(seqid);
  619. }
  620. /*
  621. * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or
  622. * failed with a seqid incrementing error -
  623. * see comments nfs_fs.h:seqid_mutating_error()
  624. */
  625. static void nfs_increment_seqid(int status, struct nfs_seqid *seqid)
  626. {
  627. BUG_ON(list_first_entry(&seqid->sequence->sequence->list, struct nfs_seqid, list) != seqid);
  628. switch (status) {
  629. case 0:
  630. break;
  631. case -NFS4ERR_BAD_SEQID:
  632. if (seqid->sequence->flags & NFS_SEQID_CONFIRMED)
  633. return;
  634. printk(KERN_WARNING "NFS: v4 server returned a bad"
  635. " sequence-id error on an"
  636. " unconfirmed sequence %p!\n",
  637. seqid->sequence);
  638. case -NFS4ERR_STALE_CLIENTID:
  639. case -NFS4ERR_STALE_STATEID:
  640. case -NFS4ERR_BAD_STATEID:
  641. case -NFS4ERR_BADXDR:
  642. case -NFS4ERR_RESOURCE:
  643. case -NFS4ERR_NOFILEHANDLE:
  644. /* Non-seqid mutating errors */
  645. return;
  646. };
  647. /*
  648. * Note: no locking needed as we are guaranteed to be first
  649. * on the sequence list
  650. */
  651. seqid->sequence->counter++;
  652. }
  653. void nfs_increment_open_seqid(int status, struct nfs_seqid *seqid)
  654. {
  655. if (status == -NFS4ERR_BAD_SEQID) {
  656. struct nfs4_state_owner *sp = container_of(seqid->sequence,
  657. struct nfs4_state_owner, so_seqid);
  658. nfs4_drop_state_owner(sp);
  659. }
  660. nfs_increment_seqid(status, seqid);
  661. }
  662. /*
  663. * Increment the seqid if the LOCK/LOCKU succeeded, or
  664. * failed with a seqid incrementing error -
  665. * see comments nfs_fs.h:seqid_mutating_error()
  666. */
  667. void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid)
  668. {
  669. nfs_increment_seqid(status, seqid);
  670. }
  671. int nfs_wait_on_sequence(struct nfs_seqid *seqid, struct rpc_task *task)
  672. {
  673. struct rpc_sequence *sequence = seqid->sequence->sequence;
  674. int status = 0;
  675. spin_lock(&sequence->lock);
  676. if (list_empty(&seqid->list))
  677. list_add_tail(&seqid->list, &sequence->list);
  678. if (list_first_entry(&sequence->list, struct nfs_seqid, list) == seqid)
  679. goto unlock;
  680. rpc_sleep_on(&sequence->wait, task, NULL);
  681. status = -EAGAIN;
  682. unlock:
  683. spin_unlock(&sequence->lock);
  684. return status;
  685. }
  686. static int nfs4_run_state_manager(void *);
  687. static void nfs4_clear_state_manager_bit(struct nfs_client *clp)
  688. {
  689. smp_mb__before_clear_bit();
  690. clear_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state);
  691. smp_mb__after_clear_bit();
  692. wake_up_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING);
  693. rpc_wake_up(&clp->cl_rpcwaitq);
  694. }
  695. /*
  696. * Schedule the nfs_client asynchronous state management routine
  697. */
  698. void nfs4_schedule_state_manager(struct nfs_client *clp)
  699. {
  700. struct task_struct *task;
  701. if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
  702. return;
  703. __module_get(THIS_MODULE);
  704. atomic_inc(&clp->cl_count);
  705. task = kthread_run(nfs4_run_state_manager, clp, "%s-manager",
  706. rpc_peeraddr2str(clp->cl_rpcclient,
  707. RPC_DISPLAY_ADDR));
  708. if (!IS_ERR(task))
  709. return;
  710. nfs4_clear_state_manager_bit(clp);
  711. nfs_put_client(clp);
  712. module_put(THIS_MODULE);
  713. }
  714. /*
  715. * Schedule a state recovery attempt
  716. */
  717. void nfs4_schedule_state_recovery(struct nfs_client *clp)
  718. {
  719. if (!clp)
  720. return;
  721. if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
  722. set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
  723. nfs4_schedule_state_manager(clp);
  724. }
  725. static int nfs4_state_mark_reclaim_reboot(struct nfs_client *clp, struct nfs4_state *state)
  726. {
  727. set_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
  728. /* Don't recover state that expired before the reboot */
  729. if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags)) {
  730. clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
  731. return 0;
  732. }
  733. set_bit(NFS_OWNER_RECLAIM_REBOOT, &state->owner->so_flags);
  734. set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
  735. return 1;
  736. }
  737. int nfs4_state_mark_reclaim_nograce(struct nfs_client *clp, struct nfs4_state *state)
  738. {
  739. set_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags);
  740. clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
  741. set_bit(NFS_OWNER_RECLAIM_NOGRACE, &state->owner->so_flags);
  742. set_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state);
  743. return 1;
  744. }
  745. static int nfs4_reclaim_locks(struct nfs4_state *state, const struct nfs4_state_recovery_ops *ops)
  746. {
  747. struct inode *inode = state->inode;
  748. struct nfs_inode *nfsi = NFS_I(inode);
  749. struct file_lock *fl;
  750. int status = 0;
  751. down_write(&nfsi->rwsem);
  752. for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
  753. if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
  754. continue;
  755. if (nfs_file_open_context(fl->fl_file)->state != state)
  756. continue;
  757. status = ops->recover_lock(state, fl);
  758. if (status >= 0)
  759. continue;
  760. switch (status) {
  761. default:
  762. printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
  763. __func__, status);
  764. case -NFS4ERR_EXPIRED:
  765. case -NFS4ERR_NO_GRACE:
  766. case -NFS4ERR_RECLAIM_BAD:
  767. case -NFS4ERR_RECLAIM_CONFLICT:
  768. /* kill_proc(fl->fl_pid, SIGLOST, 1); */
  769. break;
  770. case -NFS4ERR_STALE_CLIENTID:
  771. goto out_err;
  772. }
  773. }
  774. up_write(&nfsi->rwsem);
  775. return 0;
  776. out_err:
  777. up_write(&nfsi->rwsem);
  778. return status;
  779. }
  780. static int nfs4_reclaim_open_state(struct nfs4_state_owner *sp, const struct nfs4_state_recovery_ops *ops)
  781. {
  782. struct nfs4_state *state;
  783. struct nfs4_lock_state *lock;
  784. int status = 0;
  785. /* Note: we rely on the sp->so_states list being ordered
  786. * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
  787. * states first.
  788. * This is needed to ensure that the server won't give us any
  789. * read delegations that we have to return if, say, we are
  790. * recovering after a network partition or a reboot from a
  791. * server that doesn't support a grace period.
  792. */
  793. restart:
  794. spin_lock(&sp->so_lock);
  795. list_for_each_entry(state, &sp->so_states, open_states) {
  796. if (!test_and_clear_bit(ops->state_flag_bit, &state->flags))
  797. continue;
  798. if (state->state == 0)
  799. continue;
  800. atomic_inc(&state->count);
  801. spin_unlock(&sp->so_lock);
  802. status = ops->recover_open(sp, state);
  803. if (status >= 0) {
  804. status = nfs4_reclaim_locks(state, ops);
  805. if (status >= 0) {
  806. list_for_each_entry(lock, &state->lock_states, ls_locks) {
  807. if (!(lock->ls_flags & NFS_LOCK_INITIALIZED))
  808. printk("%s: Lock reclaim failed!\n",
  809. __func__);
  810. }
  811. nfs4_put_open_state(state);
  812. goto restart;
  813. }
  814. }
  815. switch (status) {
  816. default:
  817. printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
  818. __func__, status);
  819. case -ENOENT:
  820. case -ESTALE:
  821. /*
  822. * Open state on this file cannot be recovered
  823. * All we can do is revert to using the zero stateid.
  824. */
  825. memset(state->stateid.data, 0,
  826. sizeof(state->stateid.data));
  827. /* Mark the file as being 'closed' */
  828. state->state = 0;
  829. break;
  830. case -NFS4ERR_RECLAIM_BAD:
  831. case -NFS4ERR_RECLAIM_CONFLICT:
  832. nfs4_state_mark_reclaim_nograce(sp->so_client, state);
  833. break;
  834. case -NFS4ERR_EXPIRED:
  835. case -NFS4ERR_NO_GRACE:
  836. nfs4_state_mark_reclaim_nograce(sp->so_client, state);
  837. case -NFS4ERR_STALE_CLIENTID:
  838. goto out_err;
  839. }
  840. nfs4_put_open_state(state);
  841. goto restart;
  842. }
  843. spin_unlock(&sp->so_lock);
  844. return 0;
  845. out_err:
  846. nfs4_put_open_state(state);
  847. return status;
  848. }
  849. static void nfs4_clear_open_state(struct nfs4_state *state)
  850. {
  851. struct nfs4_lock_state *lock;
  852. clear_bit(NFS_DELEGATED_STATE, &state->flags);
  853. clear_bit(NFS_O_RDONLY_STATE, &state->flags);
  854. clear_bit(NFS_O_WRONLY_STATE, &state->flags);
  855. clear_bit(NFS_O_RDWR_STATE, &state->flags);
  856. list_for_each_entry(lock, &state->lock_states, ls_locks) {
  857. lock->ls_seqid.flags = 0;
  858. lock->ls_flags &= ~NFS_LOCK_INITIALIZED;
  859. }
  860. }
  861. static void nfs4_state_mark_reclaim_helper(struct nfs_client *clp, int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
  862. {
  863. struct nfs4_state_owner *sp;
  864. struct rb_node *pos;
  865. struct nfs4_state *state;
  866. /* Reset all sequence ids to zero */
  867. for (pos = rb_first(&clp->cl_state_owners); pos != NULL; pos = rb_next(pos)) {
  868. sp = rb_entry(pos, struct nfs4_state_owner, so_client_node);
  869. sp->so_seqid.flags = 0;
  870. spin_lock(&sp->so_lock);
  871. list_for_each_entry(state, &sp->so_states, open_states) {
  872. if (mark_reclaim(clp, state))
  873. nfs4_clear_open_state(state);
  874. }
  875. spin_unlock(&sp->so_lock);
  876. }
  877. }
  878. static void nfs4_state_start_reclaim_reboot(struct nfs_client *clp)
  879. {
  880. /* Mark all delegations for reclaim */
  881. nfs_delegation_mark_reclaim(clp);
  882. nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_reboot);
  883. }
  884. static void nfs4_state_end_reclaim_reboot(struct nfs_client *clp)
  885. {
  886. struct nfs4_state_owner *sp;
  887. struct rb_node *pos;
  888. struct nfs4_state *state;
  889. if (!test_and_clear_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
  890. return;
  891. for (pos = rb_first(&clp->cl_state_owners); pos != NULL; pos = rb_next(pos)) {
  892. sp = rb_entry(pos, struct nfs4_state_owner, so_client_node);
  893. spin_lock(&sp->so_lock);
  894. list_for_each_entry(state, &sp->so_states, open_states) {
  895. if (!test_and_clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags))
  896. continue;
  897. nfs4_state_mark_reclaim_nograce(clp, state);
  898. }
  899. spin_unlock(&sp->so_lock);
  900. }
  901. nfs_delegation_reap_unclaimed(clp);
  902. }
  903. static void nfs_delegation_clear_all(struct nfs_client *clp)
  904. {
  905. nfs_delegation_mark_reclaim(clp);
  906. nfs_delegation_reap_unclaimed(clp);
  907. }
  908. static void nfs4_state_start_reclaim_nograce(struct nfs_client *clp)
  909. {
  910. nfs_delegation_clear_all(clp);
  911. nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_nograce);
  912. }
  913. static void nfs4_state_end_reclaim_nograce(struct nfs_client *clp)
  914. {
  915. clear_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state);
  916. }
  917. static void nfs4_recovery_handle_error(struct nfs_client *clp, int error)
  918. {
  919. switch (error) {
  920. case -NFS4ERR_CB_PATH_DOWN:
  921. nfs_handle_cb_pathdown(clp);
  922. break;
  923. case -NFS4ERR_STALE_CLIENTID:
  924. case -NFS4ERR_LEASE_MOVED:
  925. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  926. nfs4_state_start_reclaim_reboot(clp);
  927. break;
  928. case -NFS4ERR_EXPIRED:
  929. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  930. nfs4_state_start_reclaim_nograce(clp);
  931. case -NFS4ERR_BADSESSION:
  932. case -NFS4ERR_BADSLOT:
  933. case -NFS4ERR_BAD_HIGH_SLOT:
  934. case -NFS4ERR_DEADSESSION:
  935. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  936. case -NFS4ERR_SEQ_FALSE_RETRY:
  937. case -NFS4ERR_SEQ_MISORDERED:
  938. set_bit(NFS4CLNT_SESSION_SETUP, &clp->cl_state);
  939. }
  940. }
  941. static int nfs4_do_reclaim(struct nfs_client *clp, const struct nfs4_state_recovery_ops *ops)
  942. {
  943. struct rb_node *pos;
  944. int status = 0;
  945. restart:
  946. spin_lock(&clp->cl_lock);
  947. for (pos = rb_first(&clp->cl_state_owners); pos != NULL; pos = rb_next(pos)) {
  948. struct nfs4_state_owner *sp = rb_entry(pos, struct nfs4_state_owner, so_client_node);
  949. if (!test_and_clear_bit(ops->owner_flag_bit, &sp->so_flags))
  950. continue;
  951. atomic_inc(&sp->so_count);
  952. spin_unlock(&clp->cl_lock);
  953. status = nfs4_reclaim_open_state(sp, ops);
  954. if (status < 0) {
  955. set_bit(ops->owner_flag_bit, &sp->so_flags);
  956. nfs4_put_state_owner(sp);
  957. nfs4_recovery_handle_error(clp, status);
  958. return status;
  959. }
  960. nfs4_put_state_owner(sp);
  961. goto restart;
  962. }
  963. spin_unlock(&clp->cl_lock);
  964. return status;
  965. }
  966. static int nfs4_check_lease(struct nfs_client *clp)
  967. {
  968. struct rpc_cred *cred;
  969. struct nfs4_state_maintenance_ops *ops =
  970. nfs4_state_renewal_ops[clp->cl_minorversion];
  971. int status = -NFS4ERR_EXPIRED;
  972. /* Is the client already known to have an expired lease? */
  973. if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
  974. return 0;
  975. spin_lock(&clp->cl_lock);
  976. cred = ops->get_state_renewal_cred_locked(clp);
  977. spin_unlock(&clp->cl_lock);
  978. if (cred == NULL) {
  979. cred = nfs4_get_setclientid_cred(clp);
  980. if (cred == NULL)
  981. goto out;
  982. }
  983. status = ops->renew_lease(clp, cred);
  984. put_rpccred(cred);
  985. out:
  986. nfs4_recovery_handle_error(clp, status);
  987. return status;
  988. }
  989. static int nfs4_reclaim_lease(struct nfs_client *clp)
  990. {
  991. struct rpc_cred *cred;
  992. int status = -ENOENT;
  993. cred = nfs4_get_setclientid_cred(clp);
  994. if (cred != NULL) {
  995. status = nfs4_init_client(clp, cred);
  996. put_rpccred(cred);
  997. /* Handle case where the user hasn't set up machine creds */
  998. if (status == -EACCES && cred == clp->cl_machine_cred) {
  999. nfs4_clear_machine_cred(clp);
  1000. status = -EAGAIN;
  1001. }
  1002. if (status == -NFS4ERR_MINOR_VERS_MISMATCH)
  1003. status = -EPROTONOSUPPORT;
  1004. }
  1005. return status;
  1006. }
  1007. #ifdef CONFIG_NFS_V4_1
  1008. static void nfs4_session_recovery_handle_error(struct nfs_client *clp, int err)
  1009. {
  1010. switch (err) {
  1011. case -NFS4ERR_STALE_CLIENTID:
  1012. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  1013. set_bit(NFS4CLNT_SESSION_SETUP, &clp->cl_state);
  1014. }
  1015. }
  1016. static int nfs4_reset_session(struct nfs_client *clp)
  1017. {
  1018. int status;
  1019. status = nfs4_proc_destroy_session(clp->cl_session);
  1020. if (status && status != -NFS4ERR_BADSESSION &&
  1021. status != -NFS4ERR_DEADSESSION) {
  1022. nfs4_session_recovery_handle_error(clp, status);
  1023. goto out;
  1024. }
  1025. memset(clp->cl_session->sess_id.data, 0, NFS4_MAX_SESSIONID_LEN);
  1026. status = nfs4_proc_create_session(clp, 1);
  1027. if (status)
  1028. nfs4_session_recovery_handle_error(clp, status);
  1029. /* fall through*/
  1030. out:
  1031. /* Wake up the next rpc task even on error */
  1032. rpc_wake_up_next(&clp->cl_session->fc_slot_table.slot_tbl_waitq);
  1033. return status;
  1034. }
  1035. static int nfs4_initialize_session(struct nfs_client *clp)
  1036. {
  1037. int status;
  1038. status = nfs4_proc_create_session(clp, 0);
  1039. if (!status) {
  1040. nfs_mark_client_ready(clp, NFS_CS_READY);
  1041. } else if (status == -NFS4ERR_STALE_CLIENTID) {
  1042. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  1043. set_bit(NFS4CLNT_SESSION_SETUP, &clp->cl_state);
  1044. } else {
  1045. nfs_mark_client_ready(clp, status);
  1046. }
  1047. return status;
  1048. }
  1049. #else /* CONFIG_NFS_V4_1 */
  1050. static int nfs4_reset_session(struct nfs_client *clp) { return 0; }
  1051. static int nfs4_initialize_session(struct nfs_client *clp) { return 0; }
  1052. #endif /* CONFIG_NFS_V4_1 */
  1053. static void nfs4_state_manager(struct nfs_client *clp)
  1054. {
  1055. int status = 0;
  1056. /* Ensure exclusive access to NFSv4 state */
  1057. for(;;) {
  1058. if (test_and_clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state)) {
  1059. /* We're going to have to re-establish a clientid */
  1060. status = nfs4_reclaim_lease(clp);
  1061. if (status) {
  1062. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  1063. if (status == -EAGAIN)
  1064. continue;
  1065. if (clp->cl_cons_state ==
  1066. NFS_CS_SESSION_INITING)
  1067. nfs_mark_client_ready(clp, status);
  1068. goto out_error;
  1069. }
  1070. clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
  1071. }
  1072. if (test_and_clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state)) {
  1073. status = nfs4_check_lease(clp);
  1074. if (status != 0)
  1075. continue;
  1076. }
  1077. /* Initialize or reset the session */
  1078. if (nfs4_has_session(clp) &&
  1079. test_and_clear_bit(NFS4CLNT_SESSION_SETUP, &clp->cl_state)) {
  1080. if (clp->cl_cons_state == NFS_CS_SESSION_INITING)
  1081. status = nfs4_initialize_session(clp);
  1082. else
  1083. status = nfs4_reset_session(clp);
  1084. if (status) {
  1085. if (status == -NFS4ERR_STALE_CLIENTID)
  1086. continue;
  1087. goto out_error;
  1088. }
  1089. }
  1090. /* First recover reboot state... */
  1091. if (test_and_clear_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state)) {
  1092. status = nfs4_do_reclaim(clp, &nfs4_reboot_recovery_ops);
  1093. if (status == -NFS4ERR_STALE_CLIENTID)
  1094. continue;
  1095. if (test_bit(NFS4CLNT_SESSION_SETUP, &clp->cl_state))
  1096. continue;
  1097. nfs4_state_end_reclaim_reboot(clp);
  1098. continue;
  1099. }
  1100. /* Now recover expired state... */
  1101. if (test_and_clear_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state)) {
  1102. status = nfs4_do_reclaim(clp, &nfs4_nograce_recovery_ops);
  1103. if (status < 0) {
  1104. set_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state);
  1105. if (status == -NFS4ERR_STALE_CLIENTID)
  1106. continue;
  1107. if (status == -NFS4ERR_EXPIRED)
  1108. continue;
  1109. if (test_bit(NFS4CLNT_SESSION_SETUP,
  1110. &clp->cl_state))
  1111. continue;
  1112. goto out_error;
  1113. } else
  1114. nfs4_state_end_reclaim_nograce(clp);
  1115. continue;
  1116. }
  1117. if (test_and_clear_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state)) {
  1118. nfs_client_return_marked_delegations(clp);
  1119. continue;
  1120. }
  1121. nfs4_clear_state_manager_bit(clp);
  1122. /* Did we race with an attempt to give us more work? */
  1123. if (clp->cl_state == 0)
  1124. break;
  1125. if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
  1126. break;
  1127. }
  1128. return;
  1129. out_error:
  1130. printk(KERN_WARNING "Error: state manager failed on NFSv4 server %s"
  1131. " with error %d\n", clp->cl_hostname, -status);
  1132. if (test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
  1133. nfs4_state_end_reclaim_reboot(clp);
  1134. nfs4_clear_state_manager_bit(clp);
  1135. }
  1136. static int nfs4_run_state_manager(void *ptr)
  1137. {
  1138. struct nfs_client *clp = ptr;
  1139. allow_signal(SIGKILL);
  1140. nfs4_state_manager(clp);
  1141. nfs_put_client(clp);
  1142. module_put_and_exit(0);
  1143. return 0;
  1144. }
  1145. /*
  1146. * Local variables:
  1147. * c-basic-offset: 8
  1148. * End:
  1149. */