nfs4state.c 40 KB

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