nfs4state.c 34 KB

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