nfs4state.c 42 KB

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