nfs4state.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340
  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_state = state;
  498. lsp->ls_owner = fl_owner;
  499. spin_lock(&clp->cl_lock);
  500. nfs_alloc_unique_id(&clp->cl_lockowner_id, &lsp->ls_id, 1, 64);
  501. spin_unlock(&clp->cl_lock);
  502. INIT_LIST_HEAD(&lsp->ls_locks);
  503. return lsp;
  504. }
  505. static void nfs4_free_lock_state(struct nfs4_lock_state *lsp)
  506. {
  507. struct nfs_client *clp = lsp->ls_state->owner->so_client;
  508. spin_lock(&clp->cl_lock);
  509. nfs_free_unique_id(&clp->cl_lockowner_id, &lsp->ls_id);
  510. spin_unlock(&clp->cl_lock);
  511. rpc_destroy_wait_queue(&lsp->ls_sequence.wait);
  512. kfree(lsp);
  513. }
  514. /*
  515. * Return a compatible lock_state. If no initialized lock_state structure
  516. * exists, return an uninitialized one.
  517. *
  518. */
  519. static struct nfs4_lock_state *nfs4_get_lock_state(struct nfs4_state *state, fl_owner_t owner)
  520. {
  521. struct nfs4_lock_state *lsp, *new = NULL;
  522. for(;;) {
  523. spin_lock(&state->state_lock);
  524. lsp = __nfs4_find_lock_state(state, owner);
  525. if (lsp != NULL)
  526. break;
  527. if (new != NULL) {
  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 const 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. struct nfs4_state_owner *sp = container_of(seqid->sequence,
  666. struct nfs4_state_owner, so_seqid);
  667. struct nfs_server *server = sp->so_server;
  668. if (status == -NFS4ERR_BAD_SEQID)
  669. nfs4_drop_state_owner(sp);
  670. if (!nfs4_has_session(server->nfs_client))
  671. nfs_increment_seqid(status, seqid);
  672. }
  673. /*
  674. * Increment the seqid if the LOCK/LOCKU succeeded, or
  675. * failed with a seqid incrementing error -
  676. * see comments nfs_fs.h:seqid_mutating_error()
  677. */
  678. void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid)
  679. {
  680. nfs_increment_seqid(status, seqid);
  681. }
  682. int nfs_wait_on_sequence(struct nfs_seqid *seqid, struct rpc_task *task)
  683. {
  684. struct rpc_sequence *sequence = seqid->sequence->sequence;
  685. int status = 0;
  686. spin_lock(&sequence->lock);
  687. if (list_empty(&seqid->list))
  688. list_add_tail(&seqid->list, &sequence->list);
  689. if (list_first_entry(&sequence->list, struct nfs_seqid, list) == seqid)
  690. goto unlock;
  691. rpc_sleep_on(&sequence->wait, task, NULL);
  692. status = -EAGAIN;
  693. unlock:
  694. spin_unlock(&sequence->lock);
  695. return status;
  696. }
  697. static int nfs4_run_state_manager(void *);
  698. static void nfs4_clear_state_manager_bit(struct nfs_client *clp)
  699. {
  700. smp_mb__before_clear_bit();
  701. clear_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state);
  702. smp_mb__after_clear_bit();
  703. wake_up_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING);
  704. rpc_wake_up(&clp->cl_rpcwaitq);
  705. }
  706. /*
  707. * Schedule the nfs_client asynchronous state management routine
  708. */
  709. void nfs4_schedule_state_manager(struct nfs_client *clp)
  710. {
  711. struct task_struct *task;
  712. if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
  713. return;
  714. __module_get(THIS_MODULE);
  715. atomic_inc(&clp->cl_count);
  716. task = kthread_run(nfs4_run_state_manager, clp, "%s-manager",
  717. rpc_peeraddr2str(clp->cl_rpcclient,
  718. RPC_DISPLAY_ADDR));
  719. if (!IS_ERR(task))
  720. return;
  721. nfs4_clear_state_manager_bit(clp);
  722. nfs_put_client(clp);
  723. module_put(THIS_MODULE);
  724. }
  725. /*
  726. * Schedule a state recovery attempt
  727. */
  728. void nfs4_schedule_state_recovery(struct nfs_client *clp)
  729. {
  730. if (!clp)
  731. return;
  732. if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
  733. set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
  734. nfs4_schedule_state_manager(clp);
  735. }
  736. static int nfs4_state_mark_reclaim_reboot(struct nfs_client *clp, struct nfs4_state *state)
  737. {
  738. set_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
  739. /* Don't recover state that expired before the reboot */
  740. if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags)) {
  741. clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
  742. return 0;
  743. }
  744. set_bit(NFS_OWNER_RECLAIM_REBOOT, &state->owner->so_flags);
  745. set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
  746. return 1;
  747. }
  748. int nfs4_state_mark_reclaim_nograce(struct nfs_client *clp, struct nfs4_state *state)
  749. {
  750. set_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags);
  751. clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
  752. set_bit(NFS_OWNER_RECLAIM_NOGRACE, &state->owner->so_flags);
  753. set_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state);
  754. return 1;
  755. }
  756. static int nfs4_reclaim_locks(struct nfs4_state *state, const struct nfs4_state_recovery_ops *ops)
  757. {
  758. struct inode *inode = state->inode;
  759. struct nfs_inode *nfsi = NFS_I(inode);
  760. struct file_lock *fl;
  761. int status = 0;
  762. if (inode->i_flock == NULL)
  763. return 0;
  764. /* Guard against delegation returns and new lock/unlock calls */
  765. down_write(&nfsi->rwsem);
  766. /* Protect inode->i_flock using the BKL */
  767. lock_kernel();
  768. for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
  769. if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
  770. continue;
  771. if (nfs_file_open_context(fl->fl_file)->state != state)
  772. continue;
  773. unlock_kernel();
  774. status = ops->recover_lock(state, fl);
  775. switch (status) {
  776. case 0:
  777. break;
  778. case -ESTALE:
  779. case -NFS4ERR_ADMIN_REVOKED:
  780. case -NFS4ERR_STALE_STATEID:
  781. case -NFS4ERR_BAD_STATEID:
  782. case -NFS4ERR_EXPIRED:
  783. case -NFS4ERR_NO_GRACE:
  784. case -NFS4ERR_STALE_CLIENTID:
  785. case -NFS4ERR_BADSESSION:
  786. case -NFS4ERR_BADSLOT:
  787. case -NFS4ERR_BAD_HIGH_SLOT:
  788. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  789. goto out;
  790. default:
  791. printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
  792. __func__, status);
  793. case -ENOMEM:
  794. case -NFS4ERR_DENIED:
  795. case -NFS4ERR_RECLAIM_BAD:
  796. case -NFS4ERR_RECLAIM_CONFLICT:
  797. /* kill_proc(fl->fl_pid, SIGLOST, 1); */
  798. status = 0;
  799. }
  800. lock_kernel();
  801. }
  802. unlock_kernel();
  803. out:
  804. up_write(&nfsi->rwsem);
  805. return status;
  806. }
  807. static int nfs4_reclaim_open_state(struct nfs4_state_owner *sp, const struct nfs4_state_recovery_ops *ops)
  808. {
  809. struct nfs4_state *state;
  810. struct nfs4_lock_state *lock;
  811. int status = 0;
  812. /* Note: we rely on the sp->so_states list being ordered
  813. * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
  814. * states first.
  815. * This is needed to ensure that the server won't give us any
  816. * read delegations that we have to return if, say, we are
  817. * recovering after a network partition or a reboot from a
  818. * server that doesn't support a grace period.
  819. */
  820. restart:
  821. spin_lock(&sp->so_lock);
  822. list_for_each_entry(state, &sp->so_states, open_states) {
  823. if (!test_and_clear_bit(ops->state_flag_bit, &state->flags))
  824. continue;
  825. if (state->state == 0)
  826. continue;
  827. atomic_inc(&state->count);
  828. spin_unlock(&sp->so_lock);
  829. status = ops->recover_open(sp, state);
  830. if (status >= 0) {
  831. status = nfs4_reclaim_locks(state, ops);
  832. if (status >= 0) {
  833. list_for_each_entry(lock, &state->lock_states, ls_locks) {
  834. if (!(lock->ls_flags & NFS_LOCK_INITIALIZED))
  835. printk("%s: Lock reclaim failed!\n",
  836. __func__);
  837. }
  838. nfs4_put_open_state(state);
  839. goto restart;
  840. }
  841. }
  842. switch (status) {
  843. default:
  844. printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
  845. __func__, status);
  846. case -ENOENT:
  847. case -ENOMEM:
  848. case -ESTALE:
  849. /*
  850. * Open state on this file cannot be recovered
  851. * All we can do is revert to using the zero stateid.
  852. */
  853. memset(state->stateid.data, 0,
  854. sizeof(state->stateid.data));
  855. /* Mark the file as being 'closed' */
  856. state->state = 0;
  857. break;
  858. case -NFS4ERR_ADMIN_REVOKED:
  859. case -NFS4ERR_STALE_STATEID:
  860. case -NFS4ERR_BAD_STATEID:
  861. case -NFS4ERR_RECLAIM_BAD:
  862. case -NFS4ERR_RECLAIM_CONFLICT:
  863. nfs4_state_mark_reclaim_nograce(sp->so_client, state);
  864. break;
  865. case -NFS4ERR_EXPIRED:
  866. case -NFS4ERR_NO_GRACE:
  867. nfs4_state_mark_reclaim_nograce(sp->so_client, state);
  868. case -NFS4ERR_STALE_CLIENTID:
  869. case -NFS4ERR_BADSESSION:
  870. case -NFS4ERR_BADSLOT:
  871. case -NFS4ERR_BAD_HIGH_SLOT:
  872. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  873. goto out_err;
  874. }
  875. nfs4_put_open_state(state);
  876. goto restart;
  877. }
  878. spin_unlock(&sp->so_lock);
  879. return 0;
  880. out_err:
  881. nfs4_put_open_state(state);
  882. return status;
  883. }
  884. static void nfs4_clear_open_state(struct nfs4_state *state)
  885. {
  886. struct nfs4_lock_state *lock;
  887. clear_bit(NFS_DELEGATED_STATE, &state->flags);
  888. clear_bit(NFS_O_RDONLY_STATE, &state->flags);
  889. clear_bit(NFS_O_WRONLY_STATE, &state->flags);
  890. clear_bit(NFS_O_RDWR_STATE, &state->flags);
  891. list_for_each_entry(lock, &state->lock_states, ls_locks) {
  892. lock->ls_seqid.flags = 0;
  893. lock->ls_flags &= ~NFS_LOCK_INITIALIZED;
  894. }
  895. }
  896. static void nfs4_state_mark_reclaim_helper(struct nfs_client *clp, int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
  897. {
  898. struct nfs4_state_owner *sp;
  899. struct rb_node *pos;
  900. struct nfs4_state *state;
  901. /* Reset all sequence ids to zero */
  902. for (pos = rb_first(&clp->cl_state_owners); pos != NULL; pos = rb_next(pos)) {
  903. sp = rb_entry(pos, struct nfs4_state_owner, so_client_node);
  904. sp->so_seqid.flags = 0;
  905. spin_lock(&sp->so_lock);
  906. list_for_each_entry(state, &sp->so_states, open_states) {
  907. if (mark_reclaim(clp, state))
  908. nfs4_clear_open_state(state);
  909. }
  910. spin_unlock(&sp->so_lock);
  911. }
  912. }
  913. static void nfs4_state_start_reclaim_reboot(struct nfs_client *clp)
  914. {
  915. /* Mark all delegations for reclaim */
  916. nfs_delegation_mark_reclaim(clp);
  917. nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_reboot);
  918. }
  919. static void nfs4_state_end_reclaim_reboot(struct nfs_client *clp)
  920. {
  921. struct nfs4_state_owner *sp;
  922. struct rb_node *pos;
  923. struct nfs4_state *state;
  924. if (!test_and_clear_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
  925. return;
  926. for (pos = rb_first(&clp->cl_state_owners); pos != NULL; pos = rb_next(pos)) {
  927. sp = rb_entry(pos, struct nfs4_state_owner, so_client_node);
  928. spin_lock(&sp->so_lock);
  929. list_for_each_entry(state, &sp->so_states, open_states) {
  930. if (!test_and_clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags))
  931. continue;
  932. nfs4_state_mark_reclaim_nograce(clp, state);
  933. }
  934. spin_unlock(&sp->so_lock);
  935. }
  936. nfs_delegation_reap_unclaimed(clp);
  937. }
  938. static void nfs_delegation_clear_all(struct nfs_client *clp)
  939. {
  940. nfs_delegation_mark_reclaim(clp);
  941. nfs_delegation_reap_unclaimed(clp);
  942. }
  943. static void nfs4_state_start_reclaim_nograce(struct nfs_client *clp)
  944. {
  945. nfs_delegation_clear_all(clp);
  946. nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_nograce);
  947. }
  948. static void nfs4_state_end_reclaim_nograce(struct nfs_client *clp)
  949. {
  950. clear_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state);
  951. }
  952. static void nfs4_recovery_handle_error(struct nfs_client *clp, int error)
  953. {
  954. switch (error) {
  955. case -NFS4ERR_CB_PATH_DOWN:
  956. nfs_handle_cb_pathdown(clp);
  957. break;
  958. case -NFS4ERR_STALE_CLIENTID:
  959. case -NFS4ERR_LEASE_MOVED:
  960. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  961. nfs4_state_start_reclaim_reboot(clp);
  962. break;
  963. case -NFS4ERR_EXPIRED:
  964. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  965. nfs4_state_start_reclaim_nograce(clp);
  966. case -NFS4ERR_BADSESSION:
  967. case -NFS4ERR_BADSLOT:
  968. case -NFS4ERR_BAD_HIGH_SLOT:
  969. case -NFS4ERR_DEADSESSION:
  970. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  971. case -NFS4ERR_SEQ_FALSE_RETRY:
  972. case -NFS4ERR_SEQ_MISORDERED:
  973. set_bit(NFS4CLNT_SESSION_SETUP, &clp->cl_state);
  974. }
  975. }
  976. static int nfs4_do_reclaim(struct nfs_client *clp, const struct nfs4_state_recovery_ops *ops)
  977. {
  978. struct rb_node *pos;
  979. int status = 0;
  980. restart:
  981. spin_lock(&clp->cl_lock);
  982. for (pos = rb_first(&clp->cl_state_owners); pos != NULL; pos = rb_next(pos)) {
  983. struct nfs4_state_owner *sp = rb_entry(pos, struct nfs4_state_owner, so_client_node);
  984. if (!test_and_clear_bit(ops->owner_flag_bit, &sp->so_flags))
  985. continue;
  986. atomic_inc(&sp->so_count);
  987. spin_unlock(&clp->cl_lock);
  988. status = nfs4_reclaim_open_state(sp, ops);
  989. if (status < 0) {
  990. set_bit(ops->owner_flag_bit, &sp->so_flags);
  991. nfs4_put_state_owner(sp);
  992. nfs4_recovery_handle_error(clp, status);
  993. return status;
  994. }
  995. nfs4_put_state_owner(sp);
  996. goto restart;
  997. }
  998. spin_unlock(&clp->cl_lock);
  999. return status;
  1000. }
  1001. static int nfs4_check_lease(struct nfs_client *clp)
  1002. {
  1003. struct rpc_cred *cred;
  1004. struct nfs4_state_maintenance_ops *ops =
  1005. nfs4_state_renewal_ops[clp->cl_minorversion];
  1006. int status = -NFS4ERR_EXPIRED;
  1007. /* Is the client already known to have an expired lease? */
  1008. if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
  1009. return 0;
  1010. spin_lock(&clp->cl_lock);
  1011. cred = ops->get_state_renewal_cred_locked(clp);
  1012. spin_unlock(&clp->cl_lock);
  1013. if (cred == NULL) {
  1014. cred = nfs4_get_setclientid_cred(clp);
  1015. if (cred == NULL)
  1016. goto out;
  1017. }
  1018. status = ops->renew_lease(clp, cred);
  1019. put_rpccred(cred);
  1020. out:
  1021. nfs4_recovery_handle_error(clp, status);
  1022. return status;
  1023. }
  1024. static int nfs4_reclaim_lease(struct nfs_client *clp)
  1025. {
  1026. struct rpc_cred *cred;
  1027. struct nfs4_state_recovery_ops *ops =
  1028. nfs4_reboot_recovery_ops[clp->cl_minorversion];
  1029. int status = -ENOENT;
  1030. cred = ops->get_clid_cred(clp);
  1031. if (cred != NULL) {
  1032. status = ops->establish_clid(clp, cred);
  1033. put_rpccred(cred);
  1034. /* Handle case where the user hasn't set up machine creds */
  1035. if (status == -EACCES && cred == clp->cl_machine_cred) {
  1036. nfs4_clear_machine_cred(clp);
  1037. status = -EAGAIN;
  1038. }
  1039. if (status == -NFS4ERR_MINOR_VERS_MISMATCH)
  1040. status = -EPROTONOSUPPORT;
  1041. }
  1042. return status;
  1043. }
  1044. #ifdef CONFIG_NFS_V4_1
  1045. static void nfs4_session_recovery_handle_error(struct nfs_client *clp, int err)
  1046. {
  1047. switch (err) {
  1048. case -NFS4ERR_STALE_CLIENTID:
  1049. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  1050. set_bit(NFS4CLNT_SESSION_SETUP, &clp->cl_state);
  1051. }
  1052. }
  1053. static int nfs4_reset_session(struct nfs_client *clp)
  1054. {
  1055. int status;
  1056. status = nfs4_proc_destroy_session(clp->cl_session);
  1057. if (status && status != -NFS4ERR_BADSESSION &&
  1058. status != -NFS4ERR_DEADSESSION) {
  1059. nfs4_session_recovery_handle_error(clp, status);
  1060. goto out;
  1061. }
  1062. memset(clp->cl_session->sess_id.data, 0, NFS4_MAX_SESSIONID_LEN);
  1063. status = nfs4_proc_create_session(clp, 1);
  1064. if (status)
  1065. nfs4_session_recovery_handle_error(clp, status);
  1066. /* fall through*/
  1067. out:
  1068. /* Wake up the next rpc task even on error */
  1069. rpc_wake_up_next(&clp->cl_session->fc_slot_table.slot_tbl_waitq);
  1070. return status;
  1071. }
  1072. static int nfs4_initialize_session(struct nfs_client *clp)
  1073. {
  1074. int status;
  1075. status = nfs4_proc_create_session(clp, 0);
  1076. if (!status) {
  1077. nfs_mark_client_ready(clp, NFS_CS_READY);
  1078. } else if (status == -NFS4ERR_STALE_CLIENTID) {
  1079. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  1080. set_bit(NFS4CLNT_SESSION_SETUP, &clp->cl_state);
  1081. } else {
  1082. nfs_mark_client_ready(clp, status);
  1083. }
  1084. return status;
  1085. }
  1086. #else /* CONFIG_NFS_V4_1 */
  1087. static int nfs4_reset_session(struct nfs_client *clp) { return 0; }
  1088. static int nfs4_initialize_session(struct nfs_client *clp) { return 0; }
  1089. #endif /* CONFIG_NFS_V4_1 */
  1090. /* Set NFS4CLNT_LEASE_EXPIRED for all v4.0 errors and for recoverable errors
  1091. * on EXCHANGE_ID for v4.1
  1092. */
  1093. static void nfs4_set_lease_expired(struct nfs_client *clp, int status)
  1094. {
  1095. if (nfs4_has_session(clp)) {
  1096. switch (status) {
  1097. case -NFS4ERR_DELAY:
  1098. case -NFS4ERR_CLID_INUSE:
  1099. case -EAGAIN:
  1100. break;
  1101. case -NFS4ERR_NOT_SAME: /* FixMe: implement recovery
  1102. * in nfs4_exchange_id */
  1103. default:
  1104. return;
  1105. }
  1106. }
  1107. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  1108. }
  1109. static void nfs4_state_manager(struct nfs_client *clp)
  1110. {
  1111. int status = 0;
  1112. /* Ensure exclusive access to NFSv4 state */
  1113. for(;;) {
  1114. if (test_and_clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state)) {
  1115. /* We're going to have to re-establish a clientid */
  1116. status = nfs4_reclaim_lease(clp);
  1117. if (status) {
  1118. nfs4_set_lease_expired(clp, status);
  1119. if (status == -EAGAIN)
  1120. continue;
  1121. if (clp->cl_cons_state ==
  1122. NFS_CS_SESSION_INITING)
  1123. nfs_mark_client_ready(clp, status);
  1124. goto out_error;
  1125. }
  1126. clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
  1127. }
  1128. if (test_and_clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state)) {
  1129. status = nfs4_check_lease(clp);
  1130. if (status != 0)
  1131. continue;
  1132. }
  1133. /* Initialize or reset the session */
  1134. if (test_and_clear_bit(NFS4CLNT_SESSION_SETUP, &clp->cl_state)
  1135. && nfs4_has_session(clp)) {
  1136. if (clp->cl_cons_state == NFS_CS_SESSION_INITING)
  1137. status = nfs4_initialize_session(clp);
  1138. else
  1139. status = nfs4_reset_session(clp);
  1140. if (status) {
  1141. if (status == -NFS4ERR_STALE_CLIENTID)
  1142. continue;
  1143. goto out_error;
  1144. }
  1145. }
  1146. /* First recover reboot state... */
  1147. if (test_and_clear_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state)) {
  1148. status = nfs4_do_reclaim(clp,
  1149. nfs4_reboot_recovery_ops[clp->cl_minorversion]);
  1150. if (status == -NFS4ERR_STALE_CLIENTID)
  1151. continue;
  1152. if (test_bit(NFS4CLNT_SESSION_SETUP, &clp->cl_state))
  1153. continue;
  1154. nfs4_state_end_reclaim_reboot(clp);
  1155. continue;
  1156. }
  1157. /* Now recover expired state... */
  1158. if (test_and_clear_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state)) {
  1159. status = nfs4_do_reclaim(clp,
  1160. nfs4_nograce_recovery_ops[clp->cl_minorversion]);
  1161. if (status < 0) {
  1162. set_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state);
  1163. if (status == -NFS4ERR_STALE_CLIENTID)
  1164. continue;
  1165. if (status == -NFS4ERR_EXPIRED)
  1166. continue;
  1167. if (test_bit(NFS4CLNT_SESSION_SETUP,
  1168. &clp->cl_state))
  1169. continue;
  1170. goto out_error;
  1171. } else
  1172. nfs4_state_end_reclaim_nograce(clp);
  1173. continue;
  1174. }
  1175. if (test_and_clear_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state)) {
  1176. nfs_client_return_marked_delegations(clp);
  1177. continue;
  1178. }
  1179. nfs4_clear_state_manager_bit(clp);
  1180. /* Did we race with an attempt to give us more work? */
  1181. if (clp->cl_state == 0)
  1182. break;
  1183. if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
  1184. break;
  1185. }
  1186. return;
  1187. out_error:
  1188. printk(KERN_WARNING "Error: state manager failed on NFSv4 server %s"
  1189. " with error %d\n", clp->cl_hostname, -status);
  1190. if (test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
  1191. nfs4_state_end_reclaim_reboot(clp);
  1192. nfs4_clear_state_manager_bit(clp);
  1193. }
  1194. static int nfs4_run_state_manager(void *ptr)
  1195. {
  1196. struct nfs_client *clp = ptr;
  1197. allow_signal(SIGKILL);
  1198. nfs4_state_manager(clp);
  1199. nfs_put_client(clp);
  1200. module_put_and_exit(0);
  1201. return 0;
  1202. }
  1203. /*
  1204. * Local variables:
  1205. * c-basic-offset: 8
  1206. * End:
  1207. */