nfs4state.c 44 KB

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