nfs4state.c 40 KB

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