nfs4state.c 37 KB

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