nfs4state.c 45 KB

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