nfs4state.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  1. /*
  2. * fs/nfs/nfs4state.c
  3. *
  4. * Client-side XDR for NFSv4.
  5. *
  6. * Copyright (c) 2002 The Regents of the University of Michigan.
  7. * All rights reserved.
  8. *
  9. * Kendrick Smith <kmsmith@umich.edu>
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in the
  19. * documentation and/or other materials provided with the distribution.
  20. * 3. Neither the name of the University nor the names of its
  21. * contributors may be used to endorse or promote products derived
  22. * from this software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  25. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  26. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  27. * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  31. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  32. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  33. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  34. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. * Implementation of the NFSv4 state model. For the time being,
  37. * this is minimal, but will be made much more complex in a
  38. * subsequent patch.
  39. */
  40. #include <linux/kernel.h>
  41. #include <linux/slab.h>
  42. #include <linux/smp_lock.h>
  43. #include <linux/nfs_fs.h>
  44. #include <linux/nfs_idmap.h>
  45. #include <linux/kthread.h>
  46. #include <linux/module.h>
  47. #include <linux/random.h>
  48. #include <linux/workqueue.h>
  49. #include <linux/bitops.h>
  50. #include "nfs4_fs.h"
  51. #include "callback.h"
  52. #include "delegation.h"
  53. #include "internal.h"
  54. #define OPENOWNER_POOL_SIZE 8
  55. const nfs4_stateid zero_stateid;
  56. static LIST_HEAD(nfs4_clientid_list);
  57. static int nfs4_init_client(struct nfs_client *clp, struct rpc_cred *cred)
  58. {
  59. int status = nfs4_proc_setclientid(clp, NFS4_CALLBACK,
  60. nfs_callback_tcpport, cred);
  61. if (status == 0)
  62. status = nfs4_proc_setclientid_confirm(clp, cred);
  63. if (status == 0)
  64. nfs4_schedule_state_renewal(clp);
  65. return status;
  66. }
  67. static struct rpc_cred *nfs4_get_machine_cred(struct nfs_client *clp)
  68. {
  69. struct rpc_cred *cred = NULL;
  70. spin_lock(&clp->cl_lock);
  71. if (clp->cl_machine_cred != NULL)
  72. cred = get_rpccred(clp->cl_machine_cred);
  73. spin_unlock(&clp->cl_lock);
  74. return cred;
  75. }
  76. static void nfs4_clear_machine_cred(struct nfs_client *clp)
  77. {
  78. struct rpc_cred *cred;
  79. spin_lock(&clp->cl_lock);
  80. cred = clp->cl_machine_cred;
  81. clp->cl_machine_cred = NULL;
  82. spin_unlock(&clp->cl_lock);
  83. if (cred != NULL)
  84. put_rpccred(cred);
  85. }
  86. struct rpc_cred *nfs4_get_renew_cred(struct nfs_client *clp)
  87. {
  88. struct nfs4_state_owner *sp;
  89. struct rb_node *pos;
  90. struct rpc_cred *cred = NULL;
  91. for (pos = rb_first(&clp->cl_state_owners); pos != NULL; pos = rb_next(pos)) {
  92. sp = rb_entry(pos, struct nfs4_state_owner, so_client_node);
  93. if (list_empty(&sp->so_states))
  94. continue;
  95. cred = get_rpccred(sp->so_cred);
  96. break;
  97. }
  98. return cred;
  99. }
  100. static struct rpc_cred *nfs4_get_setclientid_cred(struct nfs_client *clp)
  101. {
  102. struct nfs4_state_owner *sp;
  103. struct rb_node *pos;
  104. struct rpc_cred *cred;
  105. cred = nfs4_get_machine_cred(clp);
  106. if (cred != NULL)
  107. goto out;
  108. pos = rb_first(&clp->cl_state_owners);
  109. if (pos != NULL) {
  110. sp = rb_entry(pos, struct nfs4_state_owner, so_client_node);
  111. cred = get_rpccred(sp->so_cred);
  112. }
  113. out:
  114. return cred;
  115. }
  116. static void nfs_alloc_unique_id(struct rb_root *root, struct nfs_unique_id *new,
  117. __u64 minval, int maxbits)
  118. {
  119. struct rb_node **p, *parent;
  120. struct nfs_unique_id *pos;
  121. __u64 mask = ~0ULL;
  122. if (maxbits < 64)
  123. mask = (1ULL << maxbits) - 1ULL;
  124. /* Ensure distribution is more or less flat */
  125. get_random_bytes(&new->id, sizeof(new->id));
  126. new->id &= mask;
  127. if (new->id < minval)
  128. new->id += minval;
  129. retry:
  130. p = &root->rb_node;
  131. parent = NULL;
  132. while (*p != NULL) {
  133. parent = *p;
  134. pos = rb_entry(parent, struct nfs_unique_id, rb_node);
  135. if (new->id < pos->id)
  136. p = &(*p)->rb_left;
  137. else if (new->id > pos->id)
  138. p = &(*p)->rb_right;
  139. else
  140. goto id_exists;
  141. }
  142. rb_link_node(&new->rb_node, parent, p);
  143. rb_insert_color(&new->rb_node, root);
  144. return;
  145. id_exists:
  146. for (;;) {
  147. new->id++;
  148. if (new->id < minval || (new->id & mask) != new->id) {
  149. new->id = minval;
  150. break;
  151. }
  152. parent = rb_next(parent);
  153. if (parent == NULL)
  154. break;
  155. pos = rb_entry(parent, struct nfs_unique_id, rb_node);
  156. if (new->id < pos->id)
  157. break;
  158. }
  159. goto retry;
  160. }
  161. static void nfs_free_unique_id(struct rb_root *root, struct nfs_unique_id *id)
  162. {
  163. rb_erase(&id->rb_node, root);
  164. }
  165. static struct nfs4_state_owner *
  166. nfs4_find_state_owner(struct nfs_server *server, struct rpc_cred *cred)
  167. {
  168. struct nfs_client *clp = server->nfs_client;
  169. struct rb_node **p = &clp->cl_state_owners.rb_node,
  170. *parent = NULL;
  171. struct nfs4_state_owner *sp, *res = NULL;
  172. while (*p != NULL) {
  173. parent = *p;
  174. sp = rb_entry(parent, struct nfs4_state_owner, so_client_node);
  175. if (server < sp->so_server) {
  176. p = &parent->rb_left;
  177. continue;
  178. }
  179. if (server > sp->so_server) {
  180. p = &parent->rb_right;
  181. continue;
  182. }
  183. if (cred < sp->so_cred)
  184. p = &parent->rb_left;
  185. else if (cred > sp->so_cred)
  186. p = &parent->rb_right;
  187. else {
  188. atomic_inc(&sp->so_count);
  189. res = sp;
  190. break;
  191. }
  192. }
  193. return res;
  194. }
  195. static struct nfs4_state_owner *
  196. nfs4_insert_state_owner(struct nfs_client *clp, struct nfs4_state_owner *new)
  197. {
  198. struct rb_node **p = &clp->cl_state_owners.rb_node,
  199. *parent = NULL;
  200. struct nfs4_state_owner *sp;
  201. while (*p != NULL) {
  202. parent = *p;
  203. sp = rb_entry(parent, struct nfs4_state_owner, so_client_node);
  204. if (new->so_server < sp->so_server) {
  205. p = &parent->rb_left;
  206. continue;
  207. }
  208. if (new->so_server > sp->so_server) {
  209. p = &parent->rb_right;
  210. continue;
  211. }
  212. if (new->so_cred < sp->so_cred)
  213. p = &parent->rb_left;
  214. else if (new->so_cred > sp->so_cred)
  215. p = &parent->rb_right;
  216. else {
  217. atomic_inc(&sp->so_count);
  218. return sp;
  219. }
  220. }
  221. nfs_alloc_unique_id(&clp->cl_openowner_id, &new->so_owner_id, 1, 64);
  222. rb_link_node(&new->so_client_node, parent, p);
  223. rb_insert_color(&new->so_client_node, &clp->cl_state_owners);
  224. return new;
  225. }
  226. static void
  227. nfs4_remove_state_owner(struct nfs_client *clp, struct nfs4_state_owner *sp)
  228. {
  229. if (!RB_EMPTY_NODE(&sp->so_client_node))
  230. rb_erase(&sp->so_client_node, &clp->cl_state_owners);
  231. nfs_free_unique_id(&clp->cl_openowner_id, &sp->so_owner_id);
  232. }
  233. /*
  234. * nfs4_alloc_state_owner(): this is called on the OPEN or CREATE path to
  235. * create a new state_owner.
  236. *
  237. */
  238. static struct nfs4_state_owner *
  239. nfs4_alloc_state_owner(void)
  240. {
  241. struct nfs4_state_owner *sp;
  242. sp = kzalloc(sizeof(*sp),GFP_KERNEL);
  243. if (!sp)
  244. return NULL;
  245. spin_lock_init(&sp->so_lock);
  246. INIT_LIST_HEAD(&sp->so_states);
  247. INIT_LIST_HEAD(&sp->so_delegations);
  248. rpc_init_wait_queue(&sp->so_sequence.wait, "Seqid_waitqueue");
  249. sp->so_seqid.sequence = &sp->so_sequence;
  250. spin_lock_init(&sp->so_sequence.lock);
  251. INIT_LIST_HEAD(&sp->so_sequence.list);
  252. atomic_set(&sp->so_count, 1);
  253. return sp;
  254. }
  255. static void
  256. nfs4_drop_state_owner(struct nfs4_state_owner *sp)
  257. {
  258. if (!RB_EMPTY_NODE(&sp->so_client_node)) {
  259. struct nfs_client *clp = sp->so_client;
  260. spin_lock(&clp->cl_lock);
  261. rb_erase(&sp->so_client_node, &clp->cl_state_owners);
  262. RB_CLEAR_NODE(&sp->so_client_node);
  263. spin_unlock(&clp->cl_lock);
  264. }
  265. }
  266. /*
  267. * Note: must be called with clp->cl_sem held in order to prevent races
  268. * with reboot recovery!
  269. */
  270. struct nfs4_state_owner *nfs4_get_state_owner(struct nfs_server *server, struct rpc_cred *cred)
  271. {
  272. struct nfs_client *clp = server->nfs_client;
  273. struct nfs4_state_owner *sp, *new;
  274. spin_lock(&clp->cl_lock);
  275. sp = nfs4_find_state_owner(server, cred);
  276. spin_unlock(&clp->cl_lock);
  277. if (sp != NULL)
  278. return sp;
  279. new = nfs4_alloc_state_owner();
  280. if (new == NULL)
  281. return NULL;
  282. new->so_client = clp;
  283. new->so_server = server;
  284. new->so_cred = cred;
  285. spin_lock(&clp->cl_lock);
  286. sp = nfs4_insert_state_owner(clp, new);
  287. spin_unlock(&clp->cl_lock);
  288. if (sp == new)
  289. get_rpccred(cred);
  290. else {
  291. rpc_destroy_wait_queue(&new->so_sequence.wait);
  292. kfree(new);
  293. }
  294. return sp;
  295. }
  296. /*
  297. * Must be called with clp->cl_sem held in order to avoid races
  298. * with state recovery...
  299. */
  300. void nfs4_put_state_owner(struct nfs4_state_owner *sp)
  301. {
  302. struct nfs_client *clp = sp->so_client;
  303. struct rpc_cred *cred = sp->so_cred;
  304. if (!atomic_dec_and_lock(&sp->so_count, &clp->cl_lock))
  305. return;
  306. nfs4_remove_state_owner(clp, sp);
  307. spin_unlock(&clp->cl_lock);
  308. rpc_destroy_wait_queue(&sp->so_sequence.wait);
  309. put_rpccred(cred);
  310. kfree(sp);
  311. }
  312. static struct nfs4_state *
  313. nfs4_alloc_open_state(void)
  314. {
  315. struct nfs4_state *state;
  316. state = kzalloc(sizeof(*state), GFP_KERNEL);
  317. if (!state)
  318. return NULL;
  319. atomic_set(&state->count, 1);
  320. INIT_LIST_HEAD(&state->lock_states);
  321. spin_lock_init(&state->state_lock);
  322. seqlock_init(&state->seqlock);
  323. return state;
  324. }
  325. void
  326. nfs4_state_set_mode_locked(struct nfs4_state *state, mode_t mode)
  327. {
  328. if (state->state == mode)
  329. return;
  330. /* NB! List reordering - see the reclaim code for why. */
  331. if ((mode & FMODE_WRITE) != (state->state & FMODE_WRITE)) {
  332. if (mode & FMODE_WRITE)
  333. list_move(&state->open_states, &state->owner->so_states);
  334. else
  335. list_move_tail(&state->open_states, &state->owner->so_states);
  336. }
  337. state->state = mode;
  338. }
  339. static struct nfs4_state *
  340. __nfs4_find_state_byowner(struct inode *inode, struct nfs4_state_owner *owner)
  341. {
  342. struct nfs_inode *nfsi = NFS_I(inode);
  343. struct nfs4_state *state;
  344. list_for_each_entry(state, &nfsi->open_states, inode_states) {
  345. if (state->owner != owner)
  346. continue;
  347. if (atomic_inc_not_zero(&state->count))
  348. return state;
  349. }
  350. return NULL;
  351. }
  352. static void
  353. nfs4_free_open_state(struct nfs4_state *state)
  354. {
  355. kfree(state);
  356. }
  357. struct nfs4_state *
  358. nfs4_get_open_state(struct inode *inode, struct nfs4_state_owner *owner)
  359. {
  360. struct nfs4_state *state, *new;
  361. struct nfs_inode *nfsi = NFS_I(inode);
  362. spin_lock(&inode->i_lock);
  363. state = __nfs4_find_state_byowner(inode, owner);
  364. spin_unlock(&inode->i_lock);
  365. if (state)
  366. goto out;
  367. new = nfs4_alloc_open_state();
  368. spin_lock(&owner->so_lock);
  369. spin_lock(&inode->i_lock);
  370. state = __nfs4_find_state_byowner(inode, owner);
  371. if (state == NULL && new != NULL) {
  372. state = new;
  373. state->owner = owner;
  374. atomic_inc(&owner->so_count);
  375. list_add(&state->inode_states, &nfsi->open_states);
  376. state->inode = igrab(inode);
  377. spin_unlock(&inode->i_lock);
  378. /* Note: The reclaim code dictates that we add stateless
  379. * and read-only stateids to the end of the list */
  380. list_add_tail(&state->open_states, &owner->so_states);
  381. spin_unlock(&owner->so_lock);
  382. } else {
  383. spin_unlock(&inode->i_lock);
  384. spin_unlock(&owner->so_lock);
  385. if (new)
  386. nfs4_free_open_state(new);
  387. }
  388. out:
  389. return state;
  390. }
  391. /*
  392. * Beware! Caller must be holding exactly one
  393. * reference to clp->cl_sem!
  394. */
  395. void nfs4_put_open_state(struct nfs4_state *state)
  396. {
  397. struct inode *inode = state->inode;
  398. struct nfs4_state_owner *owner = state->owner;
  399. if (!atomic_dec_and_lock(&state->count, &owner->so_lock))
  400. return;
  401. spin_lock(&inode->i_lock);
  402. list_del(&state->inode_states);
  403. list_del(&state->open_states);
  404. spin_unlock(&inode->i_lock);
  405. spin_unlock(&owner->so_lock);
  406. iput(inode);
  407. nfs4_free_open_state(state);
  408. nfs4_put_state_owner(owner);
  409. }
  410. /*
  411. * Close the current file.
  412. */
  413. static void __nfs4_close(struct path *path, struct nfs4_state *state, mode_t mode, int wait)
  414. {
  415. struct nfs4_state_owner *owner = state->owner;
  416. int call_close = 0;
  417. int newstate;
  418. atomic_inc(&owner->so_count);
  419. /* Protect against nfs4_find_state() */
  420. spin_lock(&owner->so_lock);
  421. switch (mode & (FMODE_READ | FMODE_WRITE)) {
  422. case FMODE_READ:
  423. state->n_rdonly--;
  424. break;
  425. case FMODE_WRITE:
  426. state->n_wronly--;
  427. break;
  428. case FMODE_READ|FMODE_WRITE:
  429. state->n_rdwr--;
  430. }
  431. newstate = FMODE_READ|FMODE_WRITE;
  432. if (state->n_rdwr == 0) {
  433. if (state->n_rdonly == 0) {
  434. newstate &= ~FMODE_READ;
  435. call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
  436. call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
  437. }
  438. if (state->n_wronly == 0) {
  439. newstate &= ~FMODE_WRITE;
  440. call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
  441. call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
  442. }
  443. if (newstate == 0)
  444. clear_bit(NFS_DELEGATED_STATE, &state->flags);
  445. }
  446. nfs4_state_set_mode_locked(state, newstate);
  447. spin_unlock(&owner->so_lock);
  448. if (!call_close) {
  449. nfs4_put_open_state(state);
  450. nfs4_put_state_owner(owner);
  451. } else
  452. nfs4_do_close(path, state, wait);
  453. }
  454. void nfs4_close_state(struct path *path, struct nfs4_state *state, mode_t mode)
  455. {
  456. __nfs4_close(path, state, mode, 0);
  457. }
  458. void nfs4_close_sync(struct path *path, struct nfs4_state *state, mode_t mode)
  459. {
  460. __nfs4_close(path, state, mode, 1);
  461. }
  462. /*
  463. * Search the state->lock_states for an existing lock_owner
  464. * that is compatible with current->files
  465. */
  466. static struct nfs4_lock_state *
  467. __nfs4_find_lock_state(struct nfs4_state *state, fl_owner_t fl_owner)
  468. {
  469. struct nfs4_lock_state *pos;
  470. list_for_each_entry(pos, &state->lock_states, ls_locks) {
  471. if (pos->ls_owner != fl_owner)
  472. continue;
  473. atomic_inc(&pos->ls_count);
  474. return pos;
  475. }
  476. return NULL;
  477. }
  478. /*
  479. * Return a compatible lock_state. If no initialized lock_state structure
  480. * exists, return an uninitialized one.
  481. *
  482. */
  483. static struct nfs4_lock_state *nfs4_alloc_lock_state(struct nfs4_state *state, fl_owner_t fl_owner)
  484. {
  485. struct nfs4_lock_state *lsp;
  486. struct nfs_client *clp = state->owner->so_client;
  487. lsp = kzalloc(sizeof(*lsp), GFP_KERNEL);
  488. if (lsp == NULL)
  489. return NULL;
  490. rpc_init_wait_queue(&lsp->ls_sequence.wait, "lock_seqid_waitqueue");
  491. spin_lock_init(&lsp->ls_sequence.lock);
  492. INIT_LIST_HEAD(&lsp->ls_sequence.list);
  493. lsp->ls_seqid.sequence = &lsp->ls_sequence;
  494. atomic_set(&lsp->ls_count, 1);
  495. lsp->ls_owner = fl_owner;
  496. spin_lock(&clp->cl_lock);
  497. nfs_alloc_unique_id(&clp->cl_lockowner_id, &lsp->ls_id, 1, 64);
  498. spin_unlock(&clp->cl_lock);
  499. INIT_LIST_HEAD(&lsp->ls_locks);
  500. return lsp;
  501. }
  502. static void nfs4_free_lock_state(struct nfs4_lock_state *lsp)
  503. {
  504. struct nfs_client *clp = lsp->ls_state->owner->so_client;
  505. spin_lock(&clp->cl_lock);
  506. nfs_free_unique_id(&clp->cl_lockowner_id, &lsp->ls_id);
  507. spin_unlock(&clp->cl_lock);
  508. rpc_destroy_wait_queue(&lsp->ls_sequence.wait);
  509. kfree(lsp);
  510. }
  511. /*
  512. * Return a compatible lock_state. If no initialized lock_state structure
  513. * exists, return an uninitialized one.
  514. *
  515. * The caller must be holding clp->cl_sem
  516. */
  517. static struct nfs4_lock_state *nfs4_get_lock_state(struct nfs4_state *state, fl_owner_t owner)
  518. {
  519. struct nfs4_lock_state *lsp, *new = NULL;
  520. for(;;) {
  521. spin_lock(&state->state_lock);
  522. lsp = __nfs4_find_lock_state(state, owner);
  523. if (lsp != NULL)
  524. break;
  525. if (new != NULL) {
  526. new->ls_state = state;
  527. list_add(&new->ls_locks, &state->lock_states);
  528. set_bit(LK_STATE_IN_USE, &state->flags);
  529. lsp = new;
  530. new = NULL;
  531. break;
  532. }
  533. spin_unlock(&state->state_lock);
  534. new = nfs4_alloc_lock_state(state, owner);
  535. if (new == NULL)
  536. return NULL;
  537. }
  538. spin_unlock(&state->state_lock);
  539. if (new != NULL)
  540. nfs4_free_lock_state(new);
  541. return lsp;
  542. }
  543. /*
  544. * Release reference to lock_state, and free it if we see that
  545. * it is no longer in use
  546. */
  547. void nfs4_put_lock_state(struct nfs4_lock_state *lsp)
  548. {
  549. struct nfs4_state *state;
  550. if (lsp == NULL)
  551. return;
  552. state = lsp->ls_state;
  553. if (!atomic_dec_and_lock(&lsp->ls_count, &state->state_lock))
  554. return;
  555. list_del(&lsp->ls_locks);
  556. if (list_empty(&state->lock_states))
  557. clear_bit(LK_STATE_IN_USE, &state->flags);
  558. spin_unlock(&state->state_lock);
  559. nfs4_free_lock_state(lsp);
  560. }
  561. static void nfs4_fl_copy_lock(struct file_lock *dst, struct file_lock *src)
  562. {
  563. struct nfs4_lock_state *lsp = src->fl_u.nfs4_fl.owner;
  564. dst->fl_u.nfs4_fl.owner = lsp;
  565. atomic_inc(&lsp->ls_count);
  566. }
  567. static void nfs4_fl_release_lock(struct file_lock *fl)
  568. {
  569. nfs4_put_lock_state(fl->fl_u.nfs4_fl.owner);
  570. }
  571. static struct file_lock_operations nfs4_fl_lock_ops = {
  572. .fl_copy_lock = nfs4_fl_copy_lock,
  573. .fl_release_private = nfs4_fl_release_lock,
  574. };
  575. int nfs4_set_lock_state(struct nfs4_state *state, struct file_lock *fl)
  576. {
  577. struct nfs4_lock_state *lsp;
  578. if (fl->fl_ops != NULL)
  579. return 0;
  580. lsp = nfs4_get_lock_state(state, fl->fl_owner);
  581. if (lsp == NULL)
  582. return -ENOMEM;
  583. fl->fl_u.nfs4_fl.owner = lsp;
  584. fl->fl_ops = &nfs4_fl_lock_ops;
  585. return 0;
  586. }
  587. /*
  588. * Byte-range lock aware utility to initialize the stateid of read/write
  589. * requests.
  590. */
  591. void nfs4_copy_stateid(nfs4_stateid *dst, struct nfs4_state *state, fl_owner_t fl_owner)
  592. {
  593. struct nfs4_lock_state *lsp;
  594. int seq;
  595. do {
  596. seq = read_seqbegin(&state->seqlock);
  597. memcpy(dst, &state->stateid, sizeof(*dst));
  598. } while (read_seqretry(&state->seqlock, seq));
  599. if (test_bit(LK_STATE_IN_USE, &state->flags) == 0)
  600. return;
  601. spin_lock(&state->state_lock);
  602. lsp = __nfs4_find_lock_state(state, fl_owner);
  603. if (lsp != NULL && (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
  604. memcpy(dst, &lsp->ls_stateid, sizeof(*dst));
  605. spin_unlock(&state->state_lock);
  606. nfs4_put_lock_state(lsp);
  607. }
  608. struct nfs_seqid *nfs_alloc_seqid(struct nfs_seqid_counter *counter)
  609. {
  610. struct nfs_seqid *new;
  611. new = kmalloc(sizeof(*new), GFP_KERNEL);
  612. if (new != NULL) {
  613. new->sequence = counter;
  614. INIT_LIST_HEAD(&new->list);
  615. }
  616. return new;
  617. }
  618. void nfs_free_seqid(struct nfs_seqid *seqid)
  619. {
  620. if (!list_empty(&seqid->list)) {
  621. struct rpc_sequence *sequence = seqid->sequence->sequence;
  622. spin_lock(&sequence->lock);
  623. list_del(&seqid->list);
  624. spin_unlock(&sequence->lock);
  625. rpc_wake_up(&sequence->wait);
  626. }
  627. kfree(seqid);
  628. }
  629. /*
  630. * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or
  631. * failed with a seqid incrementing error -
  632. * see comments nfs_fs.h:seqid_mutating_error()
  633. */
  634. static void nfs_increment_seqid(int status, struct nfs_seqid *seqid)
  635. {
  636. BUG_ON(list_first_entry(&seqid->sequence->sequence->list, struct nfs_seqid, list) != seqid);
  637. switch (status) {
  638. case 0:
  639. break;
  640. case -NFS4ERR_BAD_SEQID:
  641. if (seqid->sequence->flags & NFS_SEQID_CONFIRMED)
  642. return;
  643. printk(KERN_WARNING "NFS: v4 server returned a bad"
  644. " sequence-id error on an"
  645. " unconfirmed sequence %p!\n",
  646. seqid->sequence);
  647. case -NFS4ERR_STALE_CLIENTID:
  648. case -NFS4ERR_STALE_STATEID:
  649. case -NFS4ERR_BAD_STATEID:
  650. case -NFS4ERR_BADXDR:
  651. case -NFS4ERR_RESOURCE:
  652. case -NFS4ERR_NOFILEHANDLE:
  653. /* Non-seqid mutating errors */
  654. return;
  655. };
  656. /*
  657. * Note: no locking needed as we are guaranteed to be first
  658. * on the sequence list
  659. */
  660. seqid->sequence->counter++;
  661. }
  662. void nfs_increment_open_seqid(int status, struct nfs_seqid *seqid)
  663. {
  664. if (status == -NFS4ERR_BAD_SEQID) {
  665. struct nfs4_state_owner *sp = container_of(seqid->sequence,
  666. struct nfs4_state_owner, so_seqid);
  667. nfs4_drop_state_owner(sp);
  668. }
  669. nfs_increment_seqid(status, seqid);
  670. }
  671. /*
  672. * Increment the seqid if the LOCK/LOCKU succeeded, or
  673. * failed with a seqid incrementing error -
  674. * see comments nfs_fs.h:seqid_mutating_error()
  675. */
  676. void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid)
  677. {
  678. nfs_increment_seqid(status, seqid);
  679. }
  680. int nfs_wait_on_sequence(struct nfs_seqid *seqid, struct rpc_task *task)
  681. {
  682. struct rpc_sequence *sequence = seqid->sequence->sequence;
  683. int status = 0;
  684. spin_lock(&sequence->lock);
  685. if (list_empty(&seqid->list))
  686. list_add_tail(&seqid->list, &sequence->list);
  687. if (list_first_entry(&sequence->list, struct nfs_seqid, list) == seqid)
  688. goto unlock;
  689. rpc_sleep_on(&sequence->wait, task, NULL);
  690. status = -EAGAIN;
  691. unlock:
  692. spin_unlock(&sequence->lock);
  693. return status;
  694. }
  695. static int reclaimer(void *);
  696. static inline void nfs4_clear_recover_bit(struct nfs_client *clp)
  697. {
  698. smp_mb__before_clear_bit();
  699. clear_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state);
  700. smp_mb__after_clear_bit();
  701. wake_up_bit(&clp->cl_state, NFS4CLNT_STATE_RECOVER);
  702. rpc_wake_up(&clp->cl_rpcwaitq);
  703. }
  704. /*
  705. * State recovery routine
  706. */
  707. static void nfs4_recover_state(struct nfs_client *clp)
  708. {
  709. struct task_struct *task;
  710. __module_get(THIS_MODULE);
  711. atomic_inc(&clp->cl_count);
  712. task = kthread_run(reclaimer, clp, "%s-reclaim",
  713. rpc_peeraddr2str(clp->cl_rpcclient,
  714. RPC_DISPLAY_ADDR));
  715. if (!IS_ERR(task))
  716. return;
  717. nfs4_clear_recover_bit(clp);
  718. nfs_put_client(clp);
  719. module_put(THIS_MODULE);
  720. }
  721. /*
  722. * Schedule a state recovery attempt
  723. */
  724. void nfs4_schedule_state_recovery(struct nfs_client *clp)
  725. {
  726. if (!clp)
  727. return;
  728. if (test_and_set_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) == 0)
  729. nfs4_recover_state(clp);
  730. }
  731. static int nfs4_reclaim_locks(struct nfs4_state_recovery_ops *ops, struct nfs4_state *state)
  732. {
  733. struct inode *inode = state->inode;
  734. struct file_lock *fl;
  735. int status = 0;
  736. for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
  737. if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
  738. continue;
  739. if (nfs_file_open_context(fl->fl_file)->state != state)
  740. continue;
  741. status = ops->recover_lock(state, fl);
  742. if (status >= 0)
  743. continue;
  744. switch (status) {
  745. default:
  746. printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
  747. __func__, status);
  748. case -NFS4ERR_EXPIRED:
  749. case -NFS4ERR_NO_GRACE:
  750. case -NFS4ERR_RECLAIM_BAD:
  751. case -NFS4ERR_RECLAIM_CONFLICT:
  752. /* kill_proc(fl->fl_pid, SIGLOST, 1); */
  753. break;
  754. case -NFS4ERR_STALE_CLIENTID:
  755. goto out_err;
  756. }
  757. }
  758. return 0;
  759. out_err:
  760. return status;
  761. }
  762. static int nfs4_reclaim_open_state(struct nfs4_state_recovery_ops *ops, struct nfs4_state_owner *sp)
  763. {
  764. struct nfs4_state *state;
  765. struct nfs4_lock_state *lock;
  766. int status = 0;
  767. /* Note: we rely on the sp->so_states list being ordered
  768. * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
  769. * states first.
  770. * This is needed to ensure that the server won't give us any
  771. * read delegations that we have to return if, say, we are
  772. * recovering after a network partition or a reboot from a
  773. * server that doesn't support a grace period.
  774. */
  775. list_for_each_entry(state, &sp->so_states, open_states) {
  776. if (state->state == 0)
  777. continue;
  778. status = ops->recover_open(sp, state);
  779. if (status >= 0) {
  780. status = nfs4_reclaim_locks(ops, state);
  781. if (status < 0)
  782. goto out_err;
  783. list_for_each_entry(lock, &state->lock_states, ls_locks) {
  784. if (!(lock->ls_flags & NFS_LOCK_INITIALIZED))
  785. printk("%s: Lock reclaim failed!\n",
  786. __func__);
  787. }
  788. continue;
  789. }
  790. switch (status) {
  791. default:
  792. printk(KERN_ERR "%s: unhandled error %d. Zeroing state\n",
  793. __func__, status);
  794. case -ENOENT:
  795. case -NFS4ERR_RECLAIM_BAD:
  796. case -NFS4ERR_RECLAIM_CONFLICT:
  797. /*
  798. * Open state on this file cannot be recovered
  799. * All we can do is revert to using the zero stateid.
  800. */
  801. memset(state->stateid.data, 0,
  802. sizeof(state->stateid.data));
  803. /* Mark the file as being 'closed' */
  804. state->state = 0;
  805. break;
  806. case -NFS4ERR_EXPIRED:
  807. case -NFS4ERR_NO_GRACE:
  808. case -NFS4ERR_STALE_CLIENTID:
  809. goto out_err;
  810. }
  811. }
  812. return 0;
  813. out_err:
  814. return status;
  815. }
  816. static void nfs4_state_mark_reclaim(struct nfs_client *clp)
  817. {
  818. struct nfs4_state_owner *sp;
  819. struct rb_node *pos;
  820. struct nfs4_state *state;
  821. struct nfs4_lock_state *lock;
  822. /* Reset all sequence ids to zero */
  823. for (pos = rb_first(&clp->cl_state_owners); pos != NULL; pos = rb_next(pos)) {
  824. sp = rb_entry(pos, struct nfs4_state_owner, so_client_node);
  825. sp->so_seqid.counter = 0;
  826. sp->so_seqid.flags = 0;
  827. spin_lock(&sp->so_lock);
  828. list_for_each_entry(state, &sp->so_states, open_states) {
  829. clear_bit(NFS_DELEGATED_STATE, &state->flags);
  830. clear_bit(NFS_O_RDONLY_STATE, &state->flags);
  831. clear_bit(NFS_O_WRONLY_STATE, &state->flags);
  832. clear_bit(NFS_O_RDWR_STATE, &state->flags);
  833. list_for_each_entry(lock, &state->lock_states, ls_locks) {
  834. lock->ls_seqid.counter = 0;
  835. lock->ls_seqid.flags = 0;
  836. lock->ls_flags &= ~NFS_LOCK_INITIALIZED;
  837. }
  838. }
  839. spin_unlock(&sp->so_lock);
  840. }
  841. }
  842. static int reclaimer(void *ptr)
  843. {
  844. struct nfs_client *clp = ptr;
  845. struct nfs4_state_owner *sp;
  846. struct rb_node *pos;
  847. struct nfs4_state_recovery_ops *ops;
  848. struct rpc_cred *cred;
  849. int status = 0;
  850. allow_signal(SIGKILL);
  851. /* Ensure exclusive access to NFSv4 state */
  852. down_write(&clp->cl_sem);
  853. /* Are there any NFS mounts out there? */
  854. if (list_empty(&clp->cl_superblocks))
  855. goto out;
  856. restart_loop:
  857. ops = &nfs4_network_partition_recovery_ops;
  858. /* Are there any open files on this volume? */
  859. cred = nfs4_get_renew_cred(clp);
  860. if (cred != NULL) {
  861. /* Yes there are: try to renew the old lease */
  862. status = nfs4_proc_renew(clp, cred);
  863. put_rpccred(cred);
  864. switch (status) {
  865. case 0:
  866. case -NFS4ERR_CB_PATH_DOWN:
  867. goto out;
  868. case -NFS4ERR_STALE_CLIENTID:
  869. case -NFS4ERR_LEASE_MOVED:
  870. ops = &nfs4_reboot_recovery_ops;
  871. }
  872. } else {
  873. /* "reboot" to ensure we clear all state on the server */
  874. clp->cl_boot_time = CURRENT_TIME;
  875. }
  876. /* We're going to have to re-establish a clientid */
  877. nfs4_state_mark_reclaim(clp);
  878. status = -ENOENT;
  879. cred = nfs4_get_setclientid_cred(clp);
  880. if (cred != NULL) {
  881. status = nfs4_init_client(clp, cred);
  882. put_rpccred(cred);
  883. /* Handle case where the user hasn't set up machine creds */
  884. if (status == -EACCES && cred == clp->cl_machine_cred) {
  885. nfs4_clear_machine_cred(clp);
  886. goto restart_loop;
  887. }
  888. }
  889. if (status)
  890. goto out_error;
  891. /* Mark all delegations for reclaim */
  892. nfs_delegation_mark_reclaim(clp);
  893. /* Note: list is protected by exclusive lock on cl->cl_sem */
  894. for (pos = rb_first(&clp->cl_state_owners); pos != NULL; pos = rb_next(pos)) {
  895. sp = rb_entry(pos, struct nfs4_state_owner, so_client_node);
  896. status = nfs4_reclaim_open_state(ops, sp);
  897. if (status < 0) {
  898. if (status == -NFS4ERR_NO_GRACE) {
  899. ops = &nfs4_network_partition_recovery_ops;
  900. status = nfs4_reclaim_open_state(ops, sp);
  901. }
  902. if (status == -NFS4ERR_STALE_CLIENTID)
  903. goto restart_loop;
  904. if (status == -NFS4ERR_EXPIRED)
  905. goto restart_loop;
  906. }
  907. }
  908. nfs_delegation_reap_unclaimed(clp);
  909. out:
  910. up_write(&clp->cl_sem);
  911. if (status == -NFS4ERR_CB_PATH_DOWN)
  912. nfs_handle_cb_pathdown(clp);
  913. nfs4_clear_recover_bit(clp);
  914. nfs_put_client(clp);
  915. module_put_and_exit(0);
  916. return 0;
  917. out_error:
  918. printk(KERN_WARNING "Error: state recovery failed on NFSv4 server %s"
  919. " with error %d\n", clp->cl_hostname, -status);
  920. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  921. goto out;
  922. }
  923. /*
  924. * Local variables:
  925. * c-basic-offset: 8
  926. * End:
  927. */