nfs4state.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  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/config.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/workqueue.h>
  46. #include <linux/bitops.h>
  47. #include "nfs4_fs.h"
  48. #include "callback.h"
  49. #include "delegation.h"
  50. #define OPENOWNER_POOL_SIZE 8
  51. const nfs4_stateid zero_stateid;
  52. static DEFINE_SPINLOCK(state_spinlock);
  53. static LIST_HEAD(nfs4_clientid_list);
  54. static void nfs4_recover_state(void *);
  55. void
  56. init_nfsv4_state(struct nfs_server *server)
  57. {
  58. server->nfs4_state = NULL;
  59. INIT_LIST_HEAD(&server->nfs4_siblings);
  60. }
  61. void
  62. destroy_nfsv4_state(struct nfs_server *server)
  63. {
  64. if (server->mnt_path) {
  65. kfree(server->mnt_path);
  66. server->mnt_path = NULL;
  67. }
  68. if (server->nfs4_state) {
  69. nfs4_put_client(server->nfs4_state);
  70. server->nfs4_state = NULL;
  71. }
  72. }
  73. /*
  74. * nfs4_get_client(): returns an empty client structure
  75. * nfs4_put_client(): drops reference to client structure
  76. *
  77. * Since these are allocated/deallocated very rarely, we don't
  78. * bother putting them in a slab cache...
  79. */
  80. static struct nfs4_client *
  81. nfs4_alloc_client(struct in_addr *addr)
  82. {
  83. struct nfs4_client *clp;
  84. if (nfs_callback_up() < 0)
  85. return NULL;
  86. if ((clp = kmalloc(sizeof(*clp), GFP_KERNEL)) == NULL) {
  87. nfs_callback_down();
  88. return NULL;
  89. }
  90. memset(clp, 0, sizeof(*clp));
  91. memcpy(&clp->cl_addr, addr, sizeof(clp->cl_addr));
  92. init_rwsem(&clp->cl_sem);
  93. INIT_LIST_HEAD(&clp->cl_delegations);
  94. INIT_LIST_HEAD(&clp->cl_state_owners);
  95. INIT_LIST_HEAD(&clp->cl_unused);
  96. spin_lock_init(&clp->cl_lock);
  97. atomic_set(&clp->cl_count, 1);
  98. INIT_WORK(&clp->cl_recoverd, nfs4_recover_state, clp);
  99. INIT_WORK(&clp->cl_renewd, nfs4_renew_state, clp);
  100. INIT_LIST_HEAD(&clp->cl_superblocks);
  101. init_waitqueue_head(&clp->cl_waitq);
  102. rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS4 client");
  103. clp->cl_rpcclient = ERR_PTR(-EINVAL);
  104. clp->cl_boot_time = CURRENT_TIME;
  105. clp->cl_state = 1 << NFS4CLNT_OK;
  106. return clp;
  107. }
  108. static void
  109. nfs4_free_client(struct nfs4_client *clp)
  110. {
  111. struct nfs4_state_owner *sp;
  112. while (!list_empty(&clp->cl_unused)) {
  113. sp = list_entry(clp->cl_unused.next,
  114. struct nfs4_state_owner,
  115. so_list);
  116. list_del(&sp->so_list);
  117. kfree(sp);
  118. }
  119. BUG_ON(!list_empty(&clp->cl_state_owners));
  120. if (clp->cl_cred)
  121. put_rpccred(clp->cl_cred);
  122. nfs_idmap_delete(clp);
  123. if (!IS_ERR(clp->cl_rpcclient))
  124. rpc_shutdown_client(clp->cl_rpcclient);
  125. kfree(clp);
  126. nfs_callback_down();
  127. }
  128. static struct nfs4_client *__nfs4_find_client(struct in_addr *addr)
  129. {
  130. struct nfs4_client *clp;
  131. list_for_each_entry(clp, &nfs4_clientid_list, cl_servers) {
  132. if (memcmp(&clp->cl_addr, addr, sizeof(clp->cl_addr)) == 0) {
  133. atomic_inc(&clp->cl_count);
  134. return clp;
  135. }
  136. }
  137. return NULL;
  138. }
  139. struct nfs4_client *nfs4_find_client(struct in_addr *addr)
  140. {
  141. struct nfs4_client *clp;
  142. spin_lock(&state_spinlock);
  143. clp = __nfs4_find_client(addr);
  144. spin_unlock(&state_spinlock);
  145. return clp;
  146. }
  147. struct nfs4_client *
  148. nfs4_get_client(struct in_addr *addr)
  149. {
  150. struct nfs4_client *clp, *new = NULL;
  151. spin_lock(&state_spinlock);
  152. for (;;) {
  153. clp = __nfs4_find_client(addr);
  154. if (clp != NULL)
  155. break;
  156. clp = new;
  157. if (clp != NULL) {
  158. list_add(&clp->cl_servers, &nfs4_clientid_list);
  159. new = NULL;
  160. break;
  161. }
  162. spin_unlock(&state_spinlock);
  163. new = nfs4_alloc_client(addr);
  164. spin_lock(&state_spinlock);
  165. if (new == NULL)
  166. break;
  167. }
  168. spin_unlock(&state_spinlock);
  169. if (new)
  170. nfs4_free_client(new);
  171. return clp;
  172. }
  173. void
  174. nfs4_put_client(struct nfs4_client *clp)
  175. {
  176. if (!atomic_dec_and_lock(&clp->cl_count, &state_spinlock))
  177. return;
  178. list_del(&clp->cl_servers);
  179. spin_unlock(&state_spinlock);
  180. BUG_ON(!list_empty(&clp->cl_superblocks));
  181. wake_up_all(&clp->cl_waitq);
  182. rpc_wake_up(&clp->cl_rpcwaitq);
  183. nfs4_kill_renewd(clp);
  184. nfs4_free_client(clp);
  185. }
  186. static int __nfs4_init_client(struct nfs4_client *clp)
  187. {
  188. int status = nfs4_proc_setclientid(clp, NFS4_CALLBACK, nfs_callback_tcpport);
  189. if (status == 0)
  190. status = nfs4_proc_setclientid_confirm(clp);
  191. if (status == 0)
  192. nfs4_schedule_state_renewal(clp);
  193. return status;
  194. }
  195. int nfs4_init_client(struct nfs4_client *clp)
  196. {
  197. return nfs4_map_errors(__nfs4_init_client(clp));
  198. }
  199. u32
  200. nfs4_alloc_lockowner_id(struct nfs4_client *clp)
  201. {
  202. return clp->cl_lockowner_id ++;
  203. }
  204. static struct nfs4_state_owner *
  205. nfs4_client_grab_unused(struct nfs4_client *clp, struct rpc_cred *cred)
  206. {
  207. struct nfs4_state_owner *sp = NULL;
  208. if (!list_empty(&clp->cl_unused)) {
  209. sp = list_entry(clp->cl_unused.next, struct nfs4_state_owner, so_list);
  210. atomic_inc(&sp->so_count);
  211. sp->so_cred = cred;
  212. list_move(&sp->so_list, &clp->cl_state_owners);
  213. clp->cl_nunused--;
  214. }
  215. return sp;
  216. }
  217. static struct nfs4_state_owner *
  218. nfs4_find_state_owner(struct nfs4_client *clp, struct rpc_cred *cred)
  219. {
  220. struct nfs4_state_owner *sp, *res = NULL;
  221. list_for_each_entry(sp, &clp->cl_state_owners, so_list) {
  222. if (sp->so_cred != cred)
  223. continue;
  224. atomic_inc(&sp->so_count);
  225. /* Move to the head of the list */
  226. list_move(&sp->so_list, &clp->cl_state_owners);
  227. res = sp;
  228. break;
  229. }
  230. return res;
  231. }
  232. /*
  233. * nfs4_alloc_state_owner(): this is called on the OPEN or CREATE path to
  234. * create a new state_owner.
  235. *
  236. */
  237. static struct nfs4_state_owner *
  238. nfs4_alloc_state_owner(void)
  239. {
  240. struct nfs4_state_owner *sp;
  241. sp = kzalloc(sizeof(*sp),GFP_KERNEL);
  242. if (!sp)
  243. return NULL;
  244. init_MUTEX(&sp->so_sema);
  245. INIT_LIST_HEAD(&sp->so_states);
  246. INIT_LIST_HEAD(&sp->so_delegations);
  247. rpc_init_wait_queue(&sp->so_sequence.wait, "Seqid_waitqueue");
  248. sp->so_seqid.sequence = &sp->so_sequence;
  249. spin_lock_init(&sp->so_sequence.lock);
  250. INIT_LIST_HEAD(&sp->so_sequence.list);
  251. atomic_set(&sp->so_count, 1);
  252. return sp;
  253. }
  254. void
  255. nfs4_drop_state_owner(struct nfs4_state_owner *sp)
  256. {
  257. struct nfs4_client *clp = sp->so_client;
  258. spin_lock(&clp->cl_lock);
  259. list_del_init(&sp->so_list);
  260. spin_unlock(&clp->cl_lock);
  261. }
  262. /*
  263. * Note: must be called with clp->cl_sem held in order to prevent races
  264. * with reboot recovery!
  265. */
  266. struct nfs4_state_owner *nfs4_get_state_owner(struct nfs_server *server, struct rpc_cred *cred)
  267. {
  268. struct nfs4_client *clp = server->nfs4_state;
  269. struct nfs4_state_owner *sp, *new;
  270. get_rpccred(cred);
  271. new = nfs4_alloc_state_owner();
  272. spin_lock(&clp->cl_lock);
  273. sp = nfs4_find_state_owner(clp, cred);
  274. if (sp == NULL)
  275. sp = nfs4_client_grab_unused(clp, cred);
  276. if (sp == NULL && new != NULL) {
  277. list_add(&new->so_list, &clp->cl_state_owners);
  278. new->so_client = clp;
  279. new->so_id = nfs4_alloc_lockowner_id(clp);
  280. new->so_cred = cred;
  281. sp = new;
  282. new = NULL;
  283. }
  284. spin_unlock(&clp->cl_lock);
  285. if (new)
  286. kfree(new);
  287. if (sp != NULL)
  288. return sp;
  289. put_rpccred(cred);
  290. return NULL;
  291. }
  292. /*
  293. * Must be called with clp->cl_sem held in order to avoid races
  294. * with state recovery...
  295. */
  296. void nfs4_put_state_owner(struct nfs4_state_owner *sp)
  297. {
  298. struct nfs4_client *clp = sp->so_client;
  299. struct rpc_cred *cred = sp->so_cred;
  300. if (!atomic_dec_and_lock(&sp->so_count, &clp->cl_lock))
  301. return;
  302. if (clp->cl_nunused >= OPENOWNER_POOL_SIZE)
  303. goto out_free;
  304. if (list_empty(&sp->so_list))
  305. goto out_free;
  306. list_move(&sp->so_list, &clp->cl_unused);
  307. clp->cl_nunused++;
  308. spin_unlock(&clp->cl_lock);
  309. put_rpccred(cred);
  310. cred = NULL;
  311. return;
  312. out_free:
  313. list_del(&sp->so_list);
  314. spin_unlock(&clp->cl_lock);
  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 = kmalloc(sizeof(*state), GFP_KERNEL);
  323. if (!state)
  324. return NULL;
  325. state->state = 0;
  326. state->nreaders = 0;
  327. state->nwriters = 0;
  328. state->flags = 0;
  329. memset(state->stateid.data, 0, sizeof(state->stateid.data));
  330. atomic_set(&state->count, 1);
  331. INIT_LIST_HEAD(&state->lock_states);
  332. init_MUTEX(&state->lock_sema);
  333. spin_lock_init(&state->state_lock);
  334. return state;
  335. }
  336. static struct nfs4_state *
  337. __nfs4_find_state(struct inode *inode, struct rpc_cred *cred, mode_t mode)
  338. {
  339. struct nfs_inode *nfsi = NFS_I(inode);
  340. struct nfs4_state *state;
  341. mode &= (FMODE_READ|FMODE_WRITE);
  342. list_for_each_entry(state, &nfsi->open_states, inode_states) {
  343. if (state->owner->so_cred != cred)
  344. continue;
  345. if ((mode & FMODE_READ) != 0 && state->nreaders == 0)
  346. continue;
  347. if ((mode & FMODE_WRITE) != 0 && state->nwriters == 0)
  348. continue;
  349. if ((state->state & mode) != mode)
  350. continue;
  351. atomic_inc(&state->count);
  352. if (mode & FMODE_READ)
  353. state->nreaders++;
  354. if (mode & FMODE_WRITE)
  355. state->nwriters++;
  356. return state;
  357. }
  358. return NULL;
  359. }
  360. static struct nfs4_state *
  361. __nfs4_find_state_byowner(struct inode *inode, struct nfs4_state_owner *owner)
  362. {
  363. struct nfs_inode *nfsi = NFS_I(inode);
  364. struct nfs4_state *state;
  365. list_for_each_entry(state, &nfsi->open_states, inode_states) {
  366. /* Is this in the process of being freed? */
  367. if (state->nreaders == 0 && state->nwriters == 0)
  368. continue;
  369. if (state->owner == owner) {
  370. atomic_inc(&state->count);
  371. return state;
  372. }
  373. }
  374. return NULL;
  375. }
  376. struct nfs4_state *
  377. nfs4_find_state(struct inode *inode, struct rpc_cred *cred, mode_t mode)
  378. {
  379. struct nfs4_state *state;
  380. spin_lock(&inode->i_lock);
  381. state = __nfs4_find_state(inode, cred, mode);
  382. spin_unlock(&inode->i_lock);
  383. return state;
  384. }
  385. static void
  386. nfs4_free_open_state(struct nfs4_state *state)
  387. {
  388. kfree(state);
  389. }
  390. struct nfs4_state *
  391. nfs4_get_open_state(struct inode *inode, struct nfs4_state_owner *owner)
  392. {
  393. struct nfs4_state *state, *new;
  394. struct nfs_inode *nfsi = NFS_I(inode);
  395. spin_lock(&inode->i_lock);
  396. state = __nfs4_find_state_byowner(inode, owner);
  397. spin_unlock(&inode->i_lock);
  398. if (state)
  399. goto out;
  400. new = nfs4_alloc_open_state();
  401. spin_lock(&inode->i_lock);
  402. state = __nfs4_find_state_byowner(inode, owner);
  403. if (state == NULL && new != NULL) {
  404. state = new;
  405. /* Caller *must* be holding owner->so_sem */
  406. /* Note: The reclaim code dictates that we add stateless
  407. * and read-only stateids to the end of the list */
  408. list_add_tail(&state->open_states, &owner->so_states);
  409. state->owner = owner;
  410. atomic_inc(&owner->so_count);
  411. list_add(&state->inode_states, &nfsi->open_states);
  412. state->inode = igrab(inode);
  413. spin_unlock(&inode->i_lock);
  414. } else {
  415. spin_unlock(&inode->i_lock);
  416. if (new)
  417. nfs4_free_open_state(new);
  418. }
  419. out:
  420. return state;
  421. }
  422. /*
  423. * Beware! Caller must be holding exactly one
  424. * reference to clp->cl_sem and owner->so_sema!
  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, &inode->i_lock))
  431. return;
  432. if (!list_empty(&state->inode_states))
  433. list_del(&state->inode_states);
  434. spin_unlock(&inode->i_lock);
  435. list_del(&state->open_states);
  436. iput(inode);
  437. BUG_ON (state->state != 0);
  438. nfs4_free_open_state(state);
  439. nfs4_put_state_owner(owner);
  440. }
  441. /*
  442. * Beware! Caller must be holding no references to clp->cl_sem!
  443. * of owner->so_sema!
  444. */
  445. void nfs4_close_state(struct nfs4_state *state, mode_t mode)
  446. {
  447. struct inode *inode = state->inode;
  448. struct nfs4_state_owner *owner = state->owner;
  449. struct nfs4_client *clp = owner->so_client;
  450. int newstate;
  451. atomic_inc(&owner->so_count);
  452. down_read(&clp->cl_sem);
  453. down(&owner->so_sema);
  454. /* Protect against nfs4_find_state() */
  455. spin_lock(&inode->i_lock);
  456. if (mode & FMODE_READ)
  457. state->nreaders--;
  458. if (mode & FMODE_WRITE)
  459. state->nwriters--;
  460. if (state->nwriters == 0) {
  461. if (state->nreaders == 0)
  462. list_del_init(&state->inode_states);
  463. /* See reclaim code */
  464. list_move_tail(&state->open_states, &owner->so_states);
  465. }
  466. spin_unlock(&inode->i_lock);
  467. newstate = 0;
  468. if (state->state != 0) {
  469. if (state->nreaders)
  470. newstate |= FMODE_READ;
  471. if (state->nwriters)
  472. newstate |= FMODE_WRITE;
  473. if (state->state == newstate)
  474. goto out;
  475. if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
  476. state->state = newstate;
  477. goto out;
  478. }
  479. if (nfs4_do_close(inode, state, newstate) == 0)
  480. return;
  481. }
  482. out:
  483. nfs4_put_open_state(state);
  484. up(&owner->so_sema);
  485. nfs4_put_state_owner(owner);
  486. up_read(&clp->cl_sem);
  487. }
  488. /*
  489. * Search the state->lock_states for an existing lock_owner
  490. * that is compatible with current->files
  491. */
  492. static struct nfs4_lock_state *
  493. __nfs4_find_lock_state(struct nfs4_state *state, fl_owner_t fl_owner)
  494. {
  495. struct nfs4_lock_state *pos;
  496. list_for_each_entry(pos, &state->lock_states, ls_locks) {
  497. if (pos->ls_owner != fl_owner)
  498. continue;
  499. atomic_inc(&pos->ls_count);
  500. return pos;
  501. }
  502. return NULL;
  503. }
  504. /*
  505. * Return a compatible lock_state. If no initialized lock_state structure
  506. * exists, return an uninitialized one.
  507. *
  508. * The caller must be holding state->lock_sema
  509. */
  510. static struct nfs4_lock_state *nfs4_alloc_lock_state(struct nfs4_state *state, fl_owner_t fl_owner)
  511. {
  512. struct nfs4_lock_state *lsp;
  513. struct nfs4_client *clp = state->owner->so_client;
  514. lsp = kzalloc(sizeof(*lsp), GFP_KERNEL);
  515. if (lsp == NULL)
  516. return NULL;
  517. lsp->ls_seqid.sequence = &state->owner->so_sequence;
  518. atomic_set(&lsp->ls_count, 1);
  519. lsp->ls_owner = fl_owner;
  520. spin_lock(&clp->cl_lock);
  521. lsp->ls_id = nfs4_alloc_lockowner_id(clp);
  522. spin_unlock(&clp->cl_lock);
  523. INIT_LIST_HEAD(&lsp->ls_locks);
  524. return lsp;
  525. }
  526. /*
  527. * Return a compatible lock_state. If no initialized lock_state structure
  528. * exists, return an uninitialized one.
  529. *
  530. * The caller must be holding state->lock_sema and clp->cl_sem
  531. */
  532. static struct nfs4_lock_state *nfs4_get_lock_state(struct nfs4_state *state, fl_owner_t owner)
  533. {
  534. struct nfs4_lock_state *lsp, *new = NULL;
  535. for(;;) {
  536. spin_lock(&state->state_lock);
  537. lsp = __nfs4_find_lock_state(state, owner);
  538. if (lsp != NULL)
  539. break;
  540. if (new != NULL) {
  541. new->ls_state = state;
  542. list_add(&new->ls_locks, &state->lock_states);
  543. set_bit(LK_STATE_IN_USE, &state->flags);
  544. lsp = new;
  545. new = NULL;
  546. break;
  547. }
  548. spin_unlock(&state->state_lock);
  549. new = nfs4_alloc_lock_state(state, owner);
  550. if (new == NULL)
  551. return NULL;
  552. }
  553. spin_unlock(&state->state_lock);
  554. kfree(new);
  555. return lsp;
  556. }
  557. /*
  558. * Release reference to lock_state, and free it if we see that
  559. * it is no longer in use
  560. */
  561. static void nfs4_put_lock_state(struct nfs4_lock_state *lsp)
  562. {
  563. struct nfs4_state *state;
  564. if (lsp == NULL)
  565. return;
  566. state = lsp->ls_state;
  567. if (!atomic_dec_and_lock(&lsp->ls_count, &state->state_lock))
  568. return;
  569. list_del(&lsp->ls_locks);
  570. if (list_empty(&state->lock_states))
  571. clear_bit(LK_STATE_IN_USE, &state->flags);
  572. spin_unlock(&state->state_lock);
  573. kfree(lsp);
  574. }
  575. static void nfs4_fl_copy_lock(struct file_lock *dst, struct file_lock *src)
  576. {
  577. struct nfs4_lock_state *lsp = src->fl_u.nfs4_fl.owner;
  578. dst->fl_u.nfs4_fl.owner = lsp;
  579. atomic_inc(&lsp->ls_count);
  580. }
  581. static void nfs4_fl_release_lock(struct file_lock *fl)
  582. {
  583. nfs4_put_lock_state(fl->fl_u.nfs4_fl.owner);
  584. }
  585. static struct file_lock_operations nfs4_fl_lock_ops = {
  586. .fl_copy_lock = nfs4_fl_copy_lock,
  587. .fl_release_private = nfs4_fl_release_lock,
  588. };
  589. int nfs4_set_lock_state(struct nfs4_state *state, struct file_lock *fl)
  590. {
  591. struct nfs4_lock_state *lsp;
  592. if (fl->fl_ops != NULL)
  593. return 0;
  594. lsp = nfs4_get_lock_state(state, fl->fl_owner);
  595. if (lsp == NULL)
  596. return -ENOMEM;
  597. fl->fl_u.nfs4_fl.owner = lsp;
  598. fl->fl_ops = &nfs4_fl_lock_ops;
  599. return 0;
  600. }
  601. /*
  602. * Byte-range lock aware utility to initialize the stateid of read/write
  603. * requests.
  604. */
  605. void nfs4_copy_stateid(nfs4_stateid *dst, struct nfs4_state *state, fl_owner_t fl_owner)
  606. {
  607. struct nfs4_lock_state *lsp;
  608. memcpy(dst, &state->stateid, sizeof(*dst));
  609. if (test_bit(LK_STATE_IN_USE, &state->flags) == 0)
  610. return;
  611. spin_lock(&state->state_lock);
  612. lsp = __nfs4_find_lock_state(state, fl_owner);
  613. if (lsp != NULL && (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
  614. memcpy(dst, &lsp->ls_stateid, sizeof(*dst));
  615. spin_unlock(&state->state_lock);
  616. nfs4_put_lock_state(lsp);
  617. }
  618. struct nfs_seqid *nfs_alloc_seqid(struct nfs_seqid_counter *counter)
  619. {
  620. struct rpc_sequence *sequence = counter->sequence;
  621. struct nfs_seqid *new;
  622. new = kmalloc(sizeof(*new), GFP_KERNEL);
  623. if (new != NULL) {
  624. new->sequence = counter;
  625. new->task = NULL;
  626. spin_lock(&sequence->lock);
  627. list_add_tail(&new->list, &sequence->list);
  628. spin_unlock(&sequence->lock);
  629. }
  630. return new;
  631. }
  632. void nfs_free_seqid(struct nfs_seqid *seqid)
  633. {
  634. struct rpc_sequence *sequence = seqid->sequence->sequence;
  635. struct rpc_task *next = NULL;
  636. spin_lock(&sequence->lock);
  637. list_del(&seqid->list);
  638. if (!list_empty(&sequence->list)) {
  639. next = list_entry(sequence->list.next, struct nfs_seqid, list)->task;
  640. if (next)
  641. rpc_wake_up_task(next);
  642. }
  643. spin_unlock(&sequence->lock);
  644. kfree(seqid);
  645. }
  646. /*
  647. * Called with sp->so_sema and clp->cl_sem held.
  648. *
  649. * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or
  650. * failed with a seqid incrementing error -
  651. * see comments nfs_fs.h:seqid_mutating_error()
  652. */
  653. static inline void nfs_increment_seqid(int status, struct nfs_seqid *seqid)
  654. {
  655. switch (status) {
  656. case 0:
  657. break;
  658. case -NFS4ERR_BAD_SEQID:
  659. case -NFS4ERR_STALE_CLIENTID:
  660. case -NFS4ERR_STALE_STATEID:
  661. case -NFS4ERR_BAD_STATEID:
  662. case -NFS4ERR_BADXDR:
  663. case -NFS4ERR_RESOURCE:
  664. case -NFS4ERR_NOFILEHANDLE:
  665. /* Non-seqid mutating errors */
  666. return;
  667. };
  668. /*
  669. * Note: no locking needed as we are guaranteed to be first
  670. * on the sequence list
  671. */
  672. seqid->sequence->counter++;
  673. }
  674. void nfs_increment_open_seqid(int status, struct nfs_seqid *seqid)
  675. {
  676. if (status == -NFS4ERR_BAD_SEQID) {
  677. struct nfs4_state_owner *sp = container_of(seqid->sequence,
  678. struct nfs4_state_owner, so_seqid);
  679. nfs4_drop_state_owner(sp);
  680. }
  681. return nfs_increment_seqid(status, seqid);
  682. }
  683. /*
  684. * Called with ls->lock_sema and clp->cl_sem held.
  685. *
  686. * Increment the seqid if the LOCK/LOCKU succeeded, or
  687. * failed with a seqid incrementing error -
  688. * see comments nfs_fs.h:seqid_mutating_error()
  689. */
  690. void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid)
  691. {
  692. return nfs_increment_seqid(status, seqid);
  693. }
  694. int nfs_wait_on_sequence(struct nfs_seqid *seqid, struct rpc_task *task)
  695. {
  696. struct rpc_sequence *sequence = seqid->sequence->sequence;
  697. int status = 0;
  698. spin_lock(&sequence->lock);
  699. if (sequence->list.next != &seqid->list) {
  700. seqid->task = task;
  701. rpc_sleep_on(&sequence->wait, task, NULL, NULL);
  702. status = -EAGAIN;
  703. }
  704. spin_unlock(&sequence->lock);
  705. return status;
  706. }
  707. static int reclaimer(void *);
  708. struct reclaimer_args {
  709. struct nfs4_client *clp;
  710. struct completion complete;
  711. };
  712. /*
  713. * State recovery routine
  714. */
  715. void
  716. nfs4_recover_state(void *data)
  717. {
  718. struct nfs4_client *clp = (struct nfs4_client *)data;
  719. struct reclaimer_args args = {
  720. .clp = clp,
  721. };
  722. might_sleep();
  723. init_completion(&args.complete);
  724. if (kernel_thread(reclaimer, &args, CLONE_KERNEL) < 0)
  725. goto out_failed_clear;
  726. wait_for_completion(&args.complete);
  727. return;
  728. out_failed_clear:
  729. set_bit(NFS4CLNT_OK, &clp->cl_state);
  730. wake_up_all(&clp->cl_waitq);
  731. rpc_wake_up(&clp->cl_rpcwaitq);
  732. }
  733. /*
  734. * Schedule a state recovery attempt
  735. */
  736. void
  737. nfs4_schedule_state_recovery(struct nfs4_client *clp)
  738. {
  739. if (!clp)
  740. return;
  741. if (test_and_clear_bit(NFS4CLNT_OK, &clp->cl_state))
  742. schedule_work(&clp->cl_recoverd);
  743. }
  744. static int nfs4_reclaim_locks(struct nfs4_state_recovery_ops *ops, struct nfs4_state *state)
  745. {
  746. struct inode *inode = state->inode;
  747. struct file_lock *fl;
  748. int status = 0;
  749. for (fl = inode->i_flock; fl != 0; fl = fl->fl_next) {
  750. if (!(fl->fl_flags & FL_POSIX))
  751. continue;
  752. if (((struct nfs_open_context *)fl->fl_file->private_data)->state != state)
  753. continue;
  754. status = ops->recover_lock(state, fl);
  755. if (status >= 0)
  756. continue;
  757. switch (status) {
  758. default:
  759. printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
  760. __FUNCTION__, status);
  761. case -NFS4ERR_EXPIRED:
  762. case -NFS4ERR_NO_GRACE:
  763. case -NFS4ERR_RECLAIM_BAD:
  764. case -NFS4ERR_RECLAIM_CONFLICT:
  765. /* kill_proc(fl->fl_owner, SIGLOST, 1); */
  766. break;
  767. case -NFS4ERR_STALE_CLIENTID:
  768. goto out_err;
  769. }
  770. }
  771. return 0;
  772. out_err:
  773. return status;
  774. }
  775. static int nfs4_reclaim_open_state(struct nfs4_state_recovery_ops *ops, struct nfs4_state_owner *sp)
  776. {
  777. struct nfs4_state *state;
  778. struct nfs4_lock_state *lock;
  779. int status = 0;
  780. /* Note: we rely on the sp->so_states list being ordered
  781. * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
  782. * states first.
  783. * This is needed to ensure that the server won't give us any
  784. * read delegations that we have to return if, say, we are
  785. * recovering after a network partition or a reboot from a
  786. * server that doesn't support a grace period.
  787. */
  788. list_for_each_entry(state, &sp->so_states, open_states) {
  789. if (state->state == 0)
  790. continue;
  791. status = ops->recover_open(sp, state);
  792. if (status >= 0) {
  793. status = nfs4_reclaim_locks(ops, state);
  794. if (status < 0)
  795. goto out_err;
  796. list_for_each_entry(lock, &state->lock_states, ls_locks) {
  797. if (!(lock->ls_flags & NFS_LOCK_INITIALIZED))
  798. printk("%s: Lock reclaim failed!\n",
  799. __FUNCTION__);
  800. }
  801. continue;
  802. }
  803. switch (status) {
  804. default:
  805. printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
  806. __FUNCTION__, status);
  807. case -ENOENT:
  808. case -NFS4ERR_RECLAIM_BAD:
  809. case -NFS4ERR_RECLAIM_CONFLICT:
  810. /*
  811. * Open state on this file cannot be recovered
  812. * All we can do is revert to using the zero stateid.
  813. */
  814. memset(state->stateid.data, 0,
  815. sizeof(state->stateid.data));
  816. /* Mark the file as being 'closed' */
  817. state->state = 0;
  818. break;
  819. case -NFS4ERR_EXPIRED:
  820. case -NFS4ERR_NO_GRACE:
  821. case -NFS4ERR_STALE_CLIENTID:
  822. goto out_err;
  823. }
  824. }
  825. return 0;
  826. out_err:
  827. return status;
  828. }
  829. static void nfs4_state_mark_reclaim(struct nfs4_client *clp)
  830. {
  831. struct nfs4_state_owner *sp;
  832. struct nfs4_state *state;
  833. struct nfs4_lock_state *lock;
  834. /* Reset all sequence ids to zero */
  835. list_for_each_entry(sp, &clp->cl_state_owners, so_list) {
  836. sp->so_seqid.counter = 0;
  837. sp->so_seqid.flags = 0;
  838. list_for_each_entry(state, &sp->so_states, open_states) {
  839. list_for_each_entry(lock, &state->lock_states, ls_locks) {
  840. lock->ls_seqid.counter = 0;
  841. lock->ls_seqid.flags = 0;
  842. lock->ls_flags &= ~NFS_LOCK_INITIALIZED;
  843. }
  844. }
  845. }
  846. }
  847. static int reclaimer(void *ptr)
  848. {
  849. struct reclaimer_args *args = (struct reclaimer_args *)ptr;
  850. struct nfs4_client *clp = args->clp;
  851. struct nfs4_state_owner *sp;
  852. struct nfs4_state_recovery_ops *ops;
  853. int status = 0;
  854. daemonize("%u.%u.%u.%u-reclaim", NIPQUAD(clp->cl_addr));
  855. allow_signal(SIGKILL);
  856. atomic_inc(&clp->cl_count);
  857. complete(&args->complete);
  858. /* Ensure exclusive access to NFSv4 state */
  859. lock_kernel();
  860. down_write(&clp->cl_sem);
  861. /* Are there any NFS mounts out there? */
  862. if (list_empty(&clp->cl_superblocks))
  863. goto out;
  864. restart_loop:
  865. status = nfs4_proc_renew(clp);
  866. switch (status) {
  867. case 0:
  868. case -NFS4ERR_CB_PATH_DOWN:
  869. goto out;
  870. case -NFS4ERR_STALE_CLIENTID:
  871. case -NFS4ERR_LEASE_MOVED:
  872. ops = &nfs4_reboot_recovery_ops;
  873. break;
  874. default:
  875. ops = &nfs4_network_partition_recovery_ops;
  876. };
  877. nfs4_state_mark_reclaim(clp);
  878. status = __nfs4_init_client(clp);
  879. if (status)
  880. goto out_error;
  881. /* Mark all delegations for reclaim */
  882. nfs_delegation_mark_reclaim(clp);
  883. /* Note: list is protected by exclusive lock on cl->cl_sem */
  884. list_for_each_entry(sp, &clp->cl_state_owners, so_list) {
  885. status = nfs4_reclaim_open_state(ops, sp);
  886. if (status < 0) {
  887. if (status == -NFS4ERR_NO_GRACE) {
  888. ops = &nfs4_network_partition_recovery_ops;
  889. status = nfs4_reclaim_open_state(ops, sp);
  890. }
  891. if (status == -NFS4ERR_STALE_CLIENTID)
  892. goto restart_loop;
  893. if (status == -NFS4ERR_EXPIRED)
  894. goto restart_loop;
  895. }
  896. }
  897. nfs_delegation_reap_unclaimed(clp);
  898. out:
  899. set_bit(NFS4CLNT_OK, &clp->cl_state);
  900. up_write(&clp->cl_sem);
  901. unlock_kernel();
  902. wake_up_all(&clp->cl_waitq);
  903. rpc_wake_up(&clp->cl_rpcwaitq);
  904. if (status == -NFS4ERR_CB_PATH_DOWN)
  905. nfs_handle_cb_pathdown(clp);
  906. nfs4_put_client(clp);
  907. return 0;
  908. out_error:
  909. printk(KERN_WARNING "Error: state recovery failed on NFSv4 server %u.%u.%u.%u with error %d\n",
  910. NIPQUAD(clp->cl_addr.s_addr), -status);
  911. goto out;
  912. }
  913. /*
  914. * Local variables:
  915. * c-basic-offset: 8
  916. * End:
  917. */