nfs4state.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247
  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 <linux/jiffies.h>
  52. #include <linux/sunrpc/clnt.h>
  53. #include "nfs4_fs.h"
  54. #include "callback.h"
  55. #include "delegation.h"
  56. #include "internal.h"
  57. #include "nfs4session.h"
  58. #include "pnfs.h"
  59. #include "netns.h"
  60. #define NFSDBG_FACILITY NFSDBG_STATE
  61. #define OPENOWNER_POOL_SIZE 8
  62. const nfs4_stateid zero_stateid;
  63. static DEFINE_MUTEX(nfs_clid_init_mutex);
  64. int nfs4_init_clientid(struct nfs_client *clp, struct rpc_cred *cred)
  65. {
  66. struct nfs4_setclientid_res clid = {
  67. .clientid = clp->cl_clientid,
  68. .confirm = clp->cl_confirm,
  69. };
  70. unsigned short port;
  71. int status;
  72. struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
  73. if (test_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state))
  74. goto do_confirm;
  75. port = nn->nfs_callback_tcpport;
  76. if (clp->cl_addr.ss_family == AF_INET6)
  77. port = nn->nfs_callback_tcpport6;
  78. status = nfs4_proc_setclientid(clp, NFS4_CALLBACK, port, cred, &clid);
  79. if (status != 0)
  80. goto out;
  81. clp->cl_clientid = clid.clientid;
  82. clp->cl_confirm = clid.confirm;
  83. set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
  84. do_confirm:
  85. status = nfs4_proc_setclientid_confirm(clp, &clid, cred);
  86. if (status != 0)
  87. goto out;
  88. clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
  89. nfs4_schedule_state_renewal(clp);
  90. out:
  91. return status;
  92. }
  93. /**
  94. * nfs40_discover_server_trunking - Detect server IP address trunking (mv0)
  95. *
  96. * @clp: nfs_client under test
  97. * @result: OUT: found nfs_client, or clp
  98. * @cred: credential to use for trunking test
  99. *
  100. * Returns zero, a negative errno, or a negative NFS4ERR status.
  101. * If zero is returned, an nfs_client pointer is planted in
  102. * "result".
  103. *
  104. * Note: The returned client may not yet be marked ready.
  105. */
  106. int nfs40_discover_server_trunking(struct nfs_client *clp,
  107. struct nfs_client **result,
  108. struct rpc_cred *cred)
  109. {
  110. struct nfs4_setclientid_res clid = {
  111. .clientid = clp->cl_clientid,
  112. .confirm = clp->cl_confirm,
  113. };
  114. struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
  115. unsigned short port;
  116. int status;
  117. port = nn->nfs_callback_tcpport;
  118. if (clp->cl_addr.ss_family == AF_INET6)
  119. port = nn->nfs_callback_tcpport6;
  120. status = nfs4_proc_setclientid(clp, NFS4_CALLBACK, port, cred, &clid);
  121. if (status != 0)
  122. goto out;
  123. clp->cl_clientid = clid.clientid;
  124. clp->cl_confirm = clid.confirm;
  125. status = nfs40_walk_client_list(clp, result, cred);
  126. if (status == 0) {
  127. /* Sustain the lease, even if it's empty. If the clientid4
  128. * goes stale it's of no use for trunking discovery. */
  129. nfs4_schedule_state_renewal(*result);
  130. }
  131. out:
  132. return status;
  133. }
  134. struct rpc_cred *nfs4_get_machine_cred_locked(struct nfs_client *clp)
  135. {
  136. struct rpc_cred *cred = NULL;
  137. if (clp->cl_machine_cred != NULL)
  138. cred = get_rpccred(clp->cl_machine_cred);
  139. return cred;
  140. }
  141. static struct rpc_cred *
  142. nfs4_get_renew_cred_server_locked(struct nfs_server *server)
  143. {
  144. struct rpc_cred *cred = NULL;
  145. struct nfs4_state_owner *sp;
  146. struct rb_node *pos;
  147. for (pos = rb_first(&server->state_owners);
  148. pos != NULL;
  149. pos = rb_next(pos)) {
  150. sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
  151. if (list_empty(&sp->so_states))
  152. continue;
  153. cred = get_rpccred(sp->so_cred);
  154. break;
  155. }
  156. return cred;
  157. }
  158. /**
  159. * nfs4_get_renew_cred_locked - Acquire credential for a renew operation
  160. * @clp: client state handle
  161. *
  162. * Returns an rpc_cred with reference count bumped, or NULL.
  163. * Caller must hold clp->cl_lock.
  164. */
  165. struct rpc_cred *nfs4_get_renew_cred_locked(struct nfs_client *clp)
  166. {
  167. struct rpc_cred *cred = NULL;
  168. struct nfs_server *server;
  169. /* Use machine credentials if available */
  170. cred = nfs4_get_machine_cred_locked(clp);
  171. if (cred != NULL)
  172. goto out;
  173. rcu_read_lock();
  174. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  175. cred = nfs4_get_renew_cred_server_locked(server);
  176. if (cred != NULL)
  177. break;
  178. }
  179. rcu_read_unlock();
  180. out:
  181. return cred;
  182. }
  183. #if defined(CONFIG_NFS_V4_1)
  184. static int nfs41_setup_state_renewal(struct nfs_client *clp)
  185. {
  186. int status;
  187. struct nfs_fsinfo fsinfo;
  188. if (!test_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state)) {
  189. nfs4_schedule_state_renewal(clp);
  190. return 0;
  191. }
  192. status = nfs4_proc_get_lease_time(clp, &fsinfo);
  193. if (status == 0) {
  194. /* Update lease time and schedule renewal */
  195. spin_lock(&clp->cl_lock);
  196. clp->cl_lease_time = fsinfo.lease_time * HZ;
  197. clp->cl_last_renewal = jiffies;
  198. spin_unlock(&clp->cl_lock);
  199. nfs4_schedule_state_renewal(clp);
  200. }
  201. return status;
  202. }
  203. static void nfs4_end_drain_slot_table(struct nfs4_slot_table *tbl)
  204. {
  205. if (test_and_clear_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state)) {
  206. spin_lock(&tbl->slot_tbl_lock);
  207. nfs41_wake_slot_table(tbl);
  208. spin_unlock(&tbl->slot_tbl_lock);
  209. }
  210. }
  211. static void nfs4_end_drain_session(struct nfs_client *clp)
  212. {
  213. struct nfs4_session *ses = clp->cl_session;
  214. if (ses != NULL) {
  215. nfs4_end_drain_slot_table(&ses->bc_slot_table);
  216. nfs4_end_drain_slot_table(&ses->fc_slot_table);
  217. }
  218. }
  219. /*
  220. * Signal state manager thread if session fore channel is drained
  221. */
  222. void nfs4_slot_tbl_drain_complete(struct nfs4_slot_table *tbl)
  223. {
  224. if (nfs4_slot_tbl_draining(tbl))
  225. complete(&tbl->complete);
  226. }
  227. static int nfs4_drain_slot_tbl(struct nfs4_slot_table *tbl)
  228. {
  229. set_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state);
  230. spin_lock(&tbl->slot_tbl_lock);
  231. if (tbl->highest_used_slotid != NFS4_NO_SLOT) {
  232. INIT_COMPLETION(tbl->complete);
  233. spin_unlock(&tbl->slot_tbl_lock);
  234. return wait_for_completion_interruptible(&tbl->complete);
  235. }
  236. spin_unlock(&tbl->slot_tbl_lock);
  237. return 0;
  238. }
  239. static int nfs4_begin_drain_session(struct nfs_client *clp)
  240. {
  241. struct nfs4_session *ses = clp->cl_session;
  242. int ret = 0;
  243. /* back channel */
  244. ret = nfs4_drain_slot_tbl(&ses->bc_slot_table);
  245. if (ret)
  246. return ret;
  247. /* fore channel */
  248. return nfs4_drain_slot_tbl(&ses->fc_slot_table);
  249. }
  250. static void nfs41_finish_session_reset(struct nfs_client *clp)
  251. {
  252. clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
  253. clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
  254. /* create_session negotiated new slot table */
  255. clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
  256. nfs41_setup_state_renewal(clp);
  257. }
  258. int nfs41_init_clientid(struct nfs_client *clp, struct rpc_cred *cred)
  259. {
  260. int status;
  261. if (test_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state))
  262. goto do_confirm;
  263. nfs4_begin_drain_session(clp);
  264. status = nfs4_proc_exchange_id(clp, cred);
  265. if (status != 0)
  266. goto out;
  267. set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
  268. do_confirm:
  269. status = nfs4_proc_create_session(clp, cred);
  270. if (status != 0)
  271. goto out;
  272. nfs41_finish_session_reset(clp);
  273. nfs_mark_client_ready(clp, NFS_CS_READY);
  274. out:
  275. return status;
  276. }
  277. /**
  278. * nfs41_discover_server_trunking - Detect server IP address trunking (mv1)
  279. *
  280. * @clp: nfs_client under test
  281. * @result: OUT: found nfs_client, or clp
  282. * @cred: credential to use for trunking test
  283. *
  284. * Returns NFS4_OK, a negative errno, or a negative NFS4ERR status.
  285. * If NFS4_OK is returned, an nfs_client pointer is planted in
  286. * "result".
  287. *
  288. * Note: The returned client may not yet be marked ready.
  289. */
  290. int nfs41_discover_server_trunking(struct nfs_client *clp,
  291. struct nfs_client **result,
  292. struct rpc_cred *cred)
  293. {
  294. int status;
  295. status = nfs4_proc_exchange_id(clp, cred);
  296. if (status != NFS4_OK)
  297. return status;
  298. set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
  299. return nfs41_walk_client_list(clp, result, cred);
  300. }
  301. struct rpc_cred *nfs4_get_exchange_id_cred(struct nfs_client *clp)
  302. {
  303. struct rpc_cred *cred;
  304. spin_lock(&clp->cl_lock);
  305. cred = nfs4_get_machine_cred_locked(clp);
  306. spin_unlock(&clp->cl_lock);
  307. return cred;
  308. }
  309. #endif /* CONFIG_NFS_V4_1 */
  310. static struct rpc_cred *
  311. nfs4_get_setclientid_cred_server(struct nfs_server *server)
  312. {
  313. struct nfs_client *clp = server->nfs_client;
  314. struct rpc_cred *cred = NULL;
  315. struct nfs4_state_owner *sp;
  316. struct rb_node *pos;
  317. spin_lock(&clp->cl_lock);
  318. pos = rb_first(&server->state_owners);
  319. if (pos != NULL) {
  320. sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
  321. cred = get_rpccred(sp->so_cred);
  322. }
  323. spin_unlock(&clp->cl_lock);
  324. return cred;
  325. }
  326. /**
  327. * nfs4_get_setclientid_cred - Acquire credential for a setclientid operation
  328. * @clp: client state handle
  329. *
  330. * Returns an rpc_cred with reference count bumped, or NULL.
  331. */
  332. struct rpc_cred *nfs4_get_setclientid_cred(struct nfs_client *clp)
  333. {
  334. struct nfs_server *server;
  335. struct rpc_cred *cred;
  336. spin_lock(&clp->cl_lock);
  337. cred = nfs4_get_machine_cred_locked(clp);
  338. spin_unlock(&clp->cl_lock);
  339. if (cred != NULL)
  340. goto out;
  341. rcu_read_lock();
  342. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  343. cred = nfs4_get_setclientid_cred_server(server);
  344. if (cred != NULL)
  345. break;
  346. }
  347. rcu_read_unlock();
  348. out:
  349. return cred;
  350. }
  351. static struct nfs4_state_owner *
  352. nfs4_find_state_owner_locked(struct nfs_server *server, struct rpc_cred *cred)
  353. {
  354. struct rb_node **p = &server->state_owners.rb_node,
  355. *parent = NULL;
  356. struct nfs4_state_owner *sp;
  357. while (*p != NULL) {
  358. parent = *p;
  359. sp = rb_entry(parent, struct nfs4_state_owner, so_server_node);
  360. if (cred < sp->so_cred)
  361. p = &parent->rb_left;
  362. else if (cred > sp->so_cred)
  363. p = &parent->rb_right;
  364. else {
  365. if (!list_empty(&sp->so_lru))
  366. list_del_init(&sp->so_lru);
  367. atomic_inc(&sp->so_count);
  368. return sp;
  369. }
  370. }
  371. return NULL;
  372. }
  373. static struct nfs4_state_owner *
  374. nfs4_insert_state_owner_locked(struct nfs4_state_owner *new)
  375. {
  376. struct nfs_server *server = new->so_server;
  377. struct rb_node **p = &server->state_owners.rb_node,
  378. *parent = NULL;
  379. struct nfs4_state_owner *sp;
  380. int err;
  381. while (*p != NULL) {
  382. parent = *p;
  383. sp = rb_entry(parent, struct nfs4_state_owner, so_server_node);
  384. if (new->so_cred < sp->so_cred)
  385. p = &parent->rb_left;
  386. else if (new->so_cred > sp->so_cred)
  387. p = &parent->rb_right;
  388. else {
  389. if (!list_empty(&sp->so_lru))
  390. list_del_init(&sp->so_lru);
  391. atomic_inc(&sp->so_count);
  392. return sp;
  393. }
  394. }
  395. err = ida_get_new(&server->openowner_id, &new->so_seqid.owner_id);
  396. if (err)
  397. return ERR_PTR(err);
  398. rb_link_node(&new->so_server_node, parent, p);
  399. rb_insert_color(&new->so_server_node, &server->state_owners);
  400. return new;
  401. }
  402. static void
  403. nfs4_remove_state_owner_locked(struct nfs4_state_owner *sp)
  404. {
  405. struct nfs_server *server = sp->so_server;
  406. if (!RB_EMPTY_NODE(&sp->so_server_node))
  407. rb_erase(&sp->so_server_node, &server->state_owners);
  408. ida_remove(&server->openowner_id, sp->so_seqid.owner_id);
  409. }
  410. static void
  411. nfs4_init_seqid_counter(struct nfs_seqid_counter *sc)
  412. {
  413. sc->create_time = ktime_get();
  414. sc->flags = 0;
  415. sc->counter = 0;
  416. spin_lock_init(&sc->lock);
  417. INIT_LIST_HEAD(&sc->list);
  418. rpc_init_wait_queue(&sc->wait, "Seqid_waitqueue");
  419. }
  420. static void
  421. nfs4_destroy_seqid_counter(struct nfs_seqid_counter *sc)
  422. {
  423. rpc_destroy_wait_queue(&sc->wait);
  424. }
  425. /*
  426. * nfs4_alloc_state_owner(): this is called on the OPEN or CREATE path to
  427. * create a new state_owner.
  428. *
  429. */
  430. static struct nfs4_state_owner *
  431. nfs4_alloc_state_owner(struct nfs_server *server,
  432. struct rpc_cred *cred,
  433. gfp_t gfp_flags)
  434. {
  435. struct nfs4_state_owner *sp;
  436. sp = kzalloc(sizeof(*sp), gfp_flags);
  437. if (!sp)
  438. return NULL;
  439. sp->so_server = server;
  440. sp->so_cred = get_rpccred(cred);
  441. spin_lock_init(&sp->so_lock);
  442. INIT_LIST_HEAD(&sp->so_states);
  443. nfs4_init_seqid_counter(&sp->so_seqid);
  444. atomic_set(&sp->so_count, 1);
  445. INIT_LIST_HEAD(&sp->so_lru);
  446. seqcount_init(&sp->so_reclaim_seqcount);
  447. mutex_init(&sp->so_delegreturn_mutex);
  448. return sp;
  449. }
  450. static void
  451. nfs4_drop_state_owner(struct nfs4_state_owner *sp)
  452. {
  453. struct rb_node *rb_node = &sp->so_server_node;
  454. if (!RB_EMPTY_NODE(rb_node)) {
  455. struct nfs_server *server = sp->so_server;
  456. struct nfs_client *clp = server->nfs_client;
  457. spin_lock(&clp->cl_lock);
  458. if (!RB_EMPTY_NODE(rb_node)) {
  459. rb_erase(rb_node, &server->state_owners);
  460. RB_CLEAR_NODE(rb_node);
  461. }
  462. spin_unlock(&clp->cl_lock);
  463. }
  464. }
  465. static void nfs4_free_state_owner(struct nfs4_state_owner *sp)
  466. {
  467. nfs4_destroy_seqid_counter(&sp->so_seqid);
  468. put_rpccred(sp->so_cred);
  469. kfree(sp);
  470. }
  471. static void nfs4_gc_state_owners(struct nfs_server *server)
  472. {
  473. struct nfs_client *clp = server->nfs_client;
  474. struct nfs4_state_owner *sp, *tmp;
  475. unsigned long time_min, time_max;
  476. LIST_HEAD(doomed);
  477. spin_lock(&clp->cl_lock);
  478. time_max = jiffies;
  479. time_min = (long)time_max - (long)clp->cl_lease_time;
  480. list_for_each_entry_safe(sp, tmp, &server->state_owners_lru, so_lru) {
  481. /* NB: LRU is sorted so that oldest is at the head */
  482. if (time_in_range(sp->so_expires, time_min, time_max))
  483. break;
  484. list_move(&sp->so_lru, &doomed);
  485. nfs4_remove_state_owner_locked(sp);
  486. }
  487. spin_unlock(&clp->cl_lock);
  488. list_for_each_entry_safe(sp, tmp, &doomed, so_lru) {
  489. list_del(&sp->so_lru);
  490. nfs4_free_state_owner(sp);
  491. }
  492. }
  493. /**
  494. * nfs4_get_state_owner - Look up a state owner given a credential
  495. * @server: nfs_server to search
  496. * @cred: RPC credential to match
  497. *
  498. * Returns a pointer to an instantiated nfs4_state_owner struct, or NULL.
  499. */
  500. struct nfs4_state_owner *nfs4_get_state_owner(struct nfs_server *server,
  501. struct rpc_cred *cred,
  502. gfp_t gfp_flags)
  503. {
  504. struct nfs_client *clp = server->nfs_client;
  505. struct nfs4_state_owner *sp, *new;
  506. spin_lock(&clp->cl_lock);
  507. sp = nfs4_find_state_owner_locked(server, cred);
  508. spin_unlock(&clp->cl_lock);
  509. if (sp != NULL)
  510. goto out;
  511. new = nfs4_alloc_state_owner(server, cred, gfp_flags);
  512. if (new == NULL)
  513. goto out;
  514. do {
  515. if (ida_pre_get(&server->openowner_id, gfp_flags) == 0)
  516. break;
  517. spin_lock(&clp->cl_lock);
  518. sp = nfs4_insert_state_owner_locked(new);
  519. spin_unlock(&clp->cl_lock);
  520. } while (sp == ERR_PTR(-EAGAIN));
  521. if (sp != new)
  522. nfs4_free_state_owner(new);
  523. out:
  524. nfs4_gc_state_owners(server);
  525. return sp;
  526. }
  527. /**
  528. * nfs4_put_state_owner - Release a nfs4_state_owner
  529. * @sp: state owner data to release
  530. *
  531. * Note that we keep released state owners on an LRU
  532. * list.
  533. * This caches valid state owners so that they can be
  534. * reused, to avoid the OPEN_CONFIRM on minor version 0.
  535. * It also pins the uniquifier of dropped state owners for
  536. * a while, to ensure that those state owner names are
  537. * never reused.
  538. */
  539. void nfs4_put_state_owner(struct nfs4_state_owner *sp)
  540. {
  541. struct nfs_server *server = sp->so_server;
  542. struct nfs_client *clp = server->nfs_client;
  543. if (!atomic_dec_and_lock(&sp->so_count, &clp->cl_lock))
  544. return;
  545. sp->so_expires = jiffies;
  546. list_add_tail(&sp->so_lru, &server->state_owners_lru);
  547. spin_unlock(&clp->cl_lock);
  548. }
  549. /**
  550. * nfs4_purge_state_owners - Release all cached state owners
  551. * @server: nfs_server with cached state owners to release
  552. *
  553. * Called at umount time. Remaining state owners will be on
  554. * the LRU with ref count of zero.
  555. */
  556. void nfs4_purge_state_owners(struct nfs_server *server)
  557. {
  558. struct nfs_client *clp = server->nfs_client;
  559. struct nfs4_state_owner *sp, *tmp;
  560. LIST_HEAD(doomed);
  561. spin_lock(&clp->cl_lock);
  562. list_for_each_entry_safe(sp, tmp, &server->state_owners_lru, so_lru) {
  563. list_move(&sp->so_lru, &doomed);
  564. nfs4_remove_state_owner_locked(sp);
  565. }
  566. spin_unlock(&clp->cl_lock);
  567. list_for_each_entry_safe(sp, tmp, &doomed, so_lru) {
  568. list_del(&sp->so_lru);
  569. nfs4_free_state_owner(sp);
  570. }
  571. }
  572. static struct nfs4_state *
  573. nfs4_alloc_open_state(void)
  574. {
  575. struct nfs4_state *state;
  576. state = kzalloc(sizeof(*state), GFP_NOFS);
  577. if (!state)
  578. return NULL;
  579. atomic_set(&state->count, 1);
  580. INIT_LIST_HEAD(&state->lock_states);
  581. spin_lock_init(&state->state_lock);
  582. seqlock_init(&state->seqlock);
  583. return state;
  584. }
  585. void
  586. nfs4_state_set_mode_locked(struct nfs4_state *state, fmode_t fmode)
  587. {
  588. if (state->state == fmode)
  589. return;
  590. /* NB! List reordering - see the reclaim code for why. */
  591. if ((fmode & FMODE_WRITE) != (state->state & FMODE_WRITE)) {
  592. if (fmode & FMODE_WRITE)
  593. list_move(&state->open_states, &state->owner->so_states);
  594. else
  595. list_move_tail(&state->open_states, &state->owner->so_states);
  596. }
  597. state->state = fmode;
  598. }
  599. static struct nfs4_state *
  600. __nfs4_find_state_byowner(struct inode *inode, struct nfs4_state_owner *owner)
  601. {
  602. struct nfs_inode *nfsi = NFS_I(inode);
  603. struct nfs4_state *state;
  604. list_for_each_entry(state, &nfsi->open_states, inode_states) {
  605. if (state->owner != owner)
  606. continue;
  607. if (!nfs4_valid_open_stateid(state))
  608. continue;
  609. if (atomic_inc_not_zero(&state->count))
  610. return state;
  611. }
  612. return NULL;
  613. }
  614. static void
  615. nfs4_free_open_state(struct nfs4_state *state)
  616. {
  617. kfree(state);
  618. }
  619. struct nfs4_state *
  620. nfs4_get_open_state(struct inode *inode, struct nfs4_state_owner *owner)
  621. {
  622. struct nfs4_state *state, *new;
  623. struct nfs_inode *nfsi = NFS_I(inode);
  624. spin_lock(&inode->i_lock);
  625. state = __nfs4_find_state_byowner(inode, owner);
  626. spin_unlock(&inode->i_lock);
  627. if (state)
  628. goto out;
  629. new = nfs4_alloc_open_state();
  630. spin_lock(&owner->so_lock);
  631. spin_lock(&inode->i_lock);
  632. state = __nfs4_find_state_byowner(inode, owner);
  633. if (state == NULL && new != NULL) {
  634. state = new;
  635. state->owner = owner;
  636. atomic_inc(&owner->so_count);
  637. list_add(&state->inode_states, &nfsi->open_states);
  638. ihold(inode);
  639. state->inode = inode;
  640. spin_unlock(&inode->i_lock);
  641. /* Note: The reclaim code dictates that we add stateless
  642. * and read-only stateids to the end of the list */
  643. list_add_tail(&state->open_states, &owner->so_states);
  644. spin_unlock(&owner->so_lock);
  645. } else {
  646. spin_unlock(&inode->i_lock);
  647. spin_unlock(&owner->so_lock);
  648. if (new)
  649. nfs4_free_open_state(new);
  650. }
  651. out:
  652. return state;
  653. }
  654. void nfs4_put_open_state(struct nfs4_state *state)
  655. {
  656. struct inode *inode = state->inode;
  657. struct nfs4_state_owner *owner = state->owner;
  658. if (!atomic_dec_and_lock(&state->count, &owner->so_lock))
  659. return;
  660. spin_lock(&inode->i_lock);
  661. list_del(&state->inode_states);
  662. list_del(&state->open_states);
  663. spin_unlock(&inode->i_lock);
  664. spin_unlock(&owner->so_lock);
  665. iput(inode);
  666. nfs4_free_open_state(state);
  667. nfs4_put_state_owner(owner);
  668. }
  669. /*
  670. * Close the current file.
  671. */
  672. static void __nfs4_close(struct nfs4_state *state,
  673. fmode_t fmode, gfp_t gfp_mask, int wait)
  674. {
  675. struct nfs4_state_owner *owner = state->owner;
  676. int call_close = 0;
  677. fmode_t newstate;
  678. atomic_inc(&owner->so_count);
  679. /* Protect against nfs4_find_state() */
  680. spin_lock(&owner->so_lock);
  681. switch (fmode & (FMODE_READ | FMODE_WRITE)) {
  682. case FMODE_READ:
  683. state->n_rdonly--;
  684. break;
  685. case FMODE_WRITE:
  686. state->n_wronly--;
  687. break;
  688. case FMODE_READ|FMODE_WRITE:
  689. state->n_rdwr--;
  690. }
  691. newstate = FMODE_READ|FMODE_WRITE;
  692. if (state->n_rdwr == 0) {
  693. if (state->n_rdonly == 0) {
  694. newstate &= ~FMODE_READ;
  695. call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
  696. call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
  697. }
  698. if (state->n_wronly == 0) {
  699. newstate &= ~FMODE_WRITE;
  700. call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
  701. call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
  702. }
  703. if (newstate == 0)
  704. clear_bit(NFS_DELEGATED_STATE, &state->flags);
  705. }
  706. nfs4_state_set_mode_locked(state, newstate);
  707. spin_unlock(&owner->so_lock);
  708. if (!call_close) {
  709. nfs4_put_open_state(state);
  710. nfs4_put_state_owner(owner);
  711. } else
  712. nfs4_do_close(state, gfp_mask, wait);
  713. }
  714. void nfs4_close_state(struct nfs4_state *state, fmode_t fmode)
  715. {
  716. __nfs4_close(state, fmode, GFP_NOFS, 0);
  717. }
  718. void nfs4_close_sync(struct nfs4_state *state, fmode_t fmode)
  719. {
  720. __nfs4_close(state, fmode, GFP_KERNEL, 1);
  721. }
  722. /*
  723. * Search the state->lock_states for an existing lock_owner
  724. * that is compatible with current->files
  725. */
  726. static struct nfs4_lock_state *
  727. __nfs4_find_lock_state(struct nfs4_state *state, fl_owner_t fl_owner, pid_t fl_pid, unsigned int type)
  728. {
  729. struct nfs4_lock_state *pos;
  730. list_for_each_entry(pos, &state->lock_states, ls_locks) {
  731. if (type != NFS4_ANY_LOCK_TYPE && pos->ls_owner.lo_type != type)
  732. continue;
  733. switch (pos->ls_owner.lo_type) {
  734. case NFS4_POSIX_LOCK_TYPE:
  735. if (pos->ls_owner.lo_u.posix_owner != fl_owner)
  736. continue;
  737. break;
  738. case NFS4_FLOCK_LOCK_TYPE:
  739. if (pos->ls_owner.lo_u.flock_owner != fl_pid)
  740. continue;
  741. }
  742. atomic_inc(&pos->ls_count);
  743. return pos;
  744. }
  745. return NULL;
  746. }
  747. /*
  748. * Return a compatible lock_state. If no initialized lock_state structure
  749. * exists, return an uninitialized one.
  750. *
  751. */
  752. 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)
  753. {
  754. struct nfs4_lock_state *lsp;
  755. struct nfs_server *server = state->owner->so_server;
  756. lsp = kzalloc(sizeof(*lsp), GFP_NOFS);
  757. if (lsp == NULL)
  758. return NULL;
  759. nfs4_init_seqid_counter(&lsp->ls_seqid);
  760. atomic_set(&lsp->ls_count, 1);
  761. lsp->ls_state = state;
  762. lsp->ls_owner.lo_type = type;
  763. switch (lsp->ls_owner.lo_type) {
  764. case NFS4_FLOCK_LOCK_TYPE:
  765. lsp->ls_owner.lo_u.flock_owner = fl_pid;
  766. break;
  767. case NFS4_POSIX_LOCK_TYPE:
  768. lsp->ls_owner.lo_u.posix_owner = fl_owner;
  769. break;
  770. default:
  771. goto out_free;
  772. }
  773. lsp->ls_seqid.owner_id = ida_simple_get(&server->lockowner_id, 0, 0, GFP_NOFS);
  774. if (lsp->ls_seqid.owner_id < 0)
  775. goto out_free;
  776. INIT_LIST_HEAD(&lsp->ls_locks);
  777. return lsp;
  778. out_free:
  779. kfree(lsp);
  780. return NULL;
  781. }
  782. void nfs4_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
  783. {
  784. ida_simple_remove(&server->lockowner_id, lsp->ls_seqid.owner_id);
  785. nfs4_destroy_seqid_counter(&lsp->ls_seqid);
  786. kfree(lsp);
  787. }
  788. /*
  789. * Return a compatible lock_state. If no initialized lock_state structure
  790. * exists, return an uninitialized one.
  791. *
  792. */
  793. static struct nfs4_lock_state *nfs4_get_lock_state(struct nfs4_state *state, fl_owner_t owner, pid_t pid, unsigned int type)
  794. {
  795. struct nfs4_lock_state *lsp, *new = NULL;
  796. for(;;) {
  797. spin_lock(&state->state_lock);
  798. lsp = __nfs4_find_lock_state(state, owner, pid, type);
  799. if (lsp != NULL)
  800. break;
  801. if (new != NULL) {
  802. list_add(&new->ls_locks, &state->lock_states);
  803. set_bit(LK_STATE_IN_USE, &state->flags);
  804. lsp = new;
  805. new = NULL;
  806. break;
  807. }
  808. spin_unlock(&state->state_lock);
  809. new = nfs4_alloc_lock_state(state, owner, pid, type);
  810. if (new == NULL)
  811. return NULL;
  812. }
  813. spin_unlock(&state->state_lock);
  814. if (new != NULL)
  815. nfs4_free_lock_state(state->owner->so_server, new);
  816. return lsp;
  817. }
  818. /*
  819. * Release reference to lock_state, and free it if we see that
  820. * it is no longer in use
  821. */
  822. void nfs4_put_lock_state(struct nfs4_lock_state *lsp)
  823. {
  824. struct nfs_server *server;
  825. struct nfs4_state *state;
  826. if (lsp == NULL)
  827. return;
  828. state = lsp->ls_state;
  829. if (!atomic_dec_and_lock(&lsp->ls_count, &state->state_lock))
  830. return;
  831. list_del(&lsp->ls_locks);
  832. if (list_empty(&state->lock_states))
  833. clear_bit(LK_STATE_IN_USE, &state->flags);
  834. spin_unlock(&state->state_lock);
  835. server = state->owner->so_server;
  836. if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
  837. struct nfs_client *clp = server->nfs_client;
  838. clp->cl_mvops->free_lock_state(server, lsp);
  839. } else
  840. nfs4_free_lock_state(server, lsp);
  841. }
  842. static void nfs4_fl_copy_lock(struct file_lock *dst, struct file_lock *src)
  843. {
  844. struct nfs4_lock_state *lsp = src->fl_u.nfs4_fl.owner;
  845. dst->fl_u.nfs4_fl.owner = lsp;
  846. atomic_inc(&lsp->ls_count);
  847. }
  848. static void nfs4_fl_release_lock(struct file_lock *fl)
  849. {
  850. nfs4_put_lock_state(fl->fl_u.nfs4_fl.owner);
  851. }
  852. static const struct file_lock_operations nfs4_fl_lock_ops = {
  853. .fl_copy_lock = nfs4_fl_copy_lock,
  854. .fl_release_private = nfs4_fl_release_lock,
  855. };
  856. int nfs4_set_lock_state(struct nfs4_state *state, struct file_lock *fl)
  857. {
  858. struct nfs4_lock_state *lsp;
  859. if (fl->fl_ops != NULL)
  860. return 0;
  861. if (fl->fl_flags & FL_POSIX)
  862. lsp = nfs4_get_lock_state(state, fl->fl_owner, 0, NFS4_POSIX_LOCK_TYPE);
  863. else if (fl->fl_flags & FL_FLOCK)
  864. lsp = nfs4_get_lock_state(state, NULL, fl->fl_pid,
  865. NFS4_FLOCK_LOCK_TYPE);
  866. else
  867. return -EINVAL;
  868. if (lsp == NULL)
  869. return -ENOMEM;
  870. fl->fl_u.nfs4_fl.owner = lsp;
  871. fl->fl_ops = &nfs4_fl_lock_ops;
  872. return 0;
  873. }
  874. static int nfs4_copy_lock_stateid(nfs4_stateid *dst,
  875. struct nfs4_state *state,
  876. const struct nfs_lockowner *lockowner)
  877. {
  878. struct nfs4_lock_state *lsp;
  879. fl_owner_t fl_owner;
  880. pid_t fl_pid;
  881. int ret = -ENOENT;
  882. if (lockowner == NULL)
  883. goto out;
  884. if (test_bit(LK_STATE_IN_USE, &state->flags) == 0)
  885. goto out;
  886. fl_owner = lockowner->l_owner;
  887. fl_pid = lockowner->l_pid;
  888. spin_lock(&state->state_lock);
  889. lsp = __nfs4_find_lock_state(state, fl_owner, fl_pid, NFS4_ANY_LOCK_TYPE);
  890. if (lsp != NULL && test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0) {
  891. nfs4_stateid_copy(dst, &lsp->ls_stateid);
  892. ret = 0;
  893. smp_rmb();
  894. if (!list_empty(&lsp->ls_seqid.list))
  895. ret = -EWOULDBLOCK;
  896. }
  897. spin_unlock(&state->state_lock);
  898. nfs4_put_lock_state(lsp);
  899. out:
  900. return ret;
  901. }
  902. static int nfs4_copy_open_stateid(nfs4_stateid *dst, struct nfs4_state *state)
  903. {
  904. const nfs4_stateid *src;
  905. int ret;
  906. int seq;
  907. do {
  908. src = &zero_stateid;
  909. seq = read_seqbegin(&state->seqlock);
  910. if (test_bit(NFS_OPEN_STATE, &state->flags))
  911. src = &state->open_stateid;
  912. nfs4_stateid_copy(dst, src);
  913. ret = 0;
  914. smp_rmb();
  915. if (!list_empty(&state->owner->so_seqid.list))
  916. ret = -EWOULDBLOCK;
  917. } while (read_seqretry(&state->seqlock, seq));
  918. return ret;
  919. }
  920. /*
  921. * Byte-range lock aware utility to initialize the stateid of read/write
  922. * requests.
  923. */
  924. int nfs4_select_rw_stateid(nfs4_stateid *dst, struct nfs4_state *state,
  925. fmode_t fmode, const struct nfs_lockowner *lockowner)
  926. {
  927. int ret = 0;
  928. if (nfs4_copy_delegation_stateid(dst, state->inode, fmode))
  929. goto out;
  930. ret = nfs4_copy_lock_stateid(dst, state, lockowner);
  931. if (ret != -ENOENT)
  932. goto out;
  933. ret = nfs4_copy_open_stateid(dst, state);
  934. out:
  935. if (nfs_server_capable(state->inode, NFS_CAP_STATEID_NFSV41))
  936. dst->seqid = 0;
  937. return ret;
  938. }
  939. struct nfs_seqid *nfs_alloc_seqid(struct nfs_seqid_counter *counter, gfp_t gfp_mask)
  940. {
  941. struct nfs_seqid *new;
  942. new = kmalloc(sizeof(*new), gfp_mask);
  943. if (new != NULL) {
  944. new->sequence = counter;
  945. INIT_LIST_HEAD(&new->list);
  946. new->task = NULL;
  947. }
  948. return new;
  949. }
  950. void nfs_release_seqid(struct nfs_seqid *seqid)
  951. {
  952. struct nfs_seqid_counter *sequence;
  953. if (list_empty(&seqid->list))
  954. return;
  955. sequence = seqid->sequence;
  956. spin_lock(&sequence->lock);
  957. list_del_init(&seqid->list);
  958. if (!list_empty(&sequence->list)) {
  959. struct nfs_seqid *next;
  960. next = list_first_entry(&sequence->list,
  961. struct nfs_seqid, list);
  962. rpc_wake_up_queued_task(&sequence->wait, next->task);
  963. }
  964. spin_unlock(&sequence->lock);
  965. }
  966. void nfs_free_seqid(struct nfs_seqid *seqid)
  967. {
  968. nfs_release_seqid(seqid);
  969. kfree(seqid);
  970. }
  971. /*
  972. * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or
  973. * failed with a seqid incrementing error -
  974. * see comments nfs_fs.h:seqid_mutating_error()
  975. */
  976. static void nfs_increment_seqid(int status, struct nfs_seqid *seqid)
  977. {
  978. switch (status) {
  979. case 0:
  980. break;
  981. case -NFS4ERR_BAD_SEQID:
  982. if (seqid->sequence->flags & NFS_SEQID_CONFIRMED)
  983. return;
  984. pr_warn_ratelimited("NFS: v4 server returned a bad"
  985. " sequence-id error on an"
  986. " unconfirmed sequence %p!\n",
  987. seqid->sequence);
  988. case -NFS4ERR_STALE_CLIENTID:
  989. case -NFS4ERR_STALE_STATEID:
  990. case -NFS4ERR_BAD_STATEID:
  991. case -NFS4ERR_BADXDR:
  992. case -NFS4ERR_RESOURCE:
  993. case -NFS4ERR_NOFILEHANDLE:
  994. /* Non-seqid mutating errors */
  995. return;
  996. };
  997. /*
  998. * Note: no locking needed as we are guaranteed to be first
  999. * on the sequence list
  1000. */
  1001. seqid->sequence->counter++;
  1002. }
  1003. void nfs_increment_open_seqid(int status, struct nfs_seqid *seqid)
  1004. {
  1005. struct nfs4_state_owner *sp = container_of(seqid->sequence,
  1006. struct nfs4_state_owner, so_seqid);
  1007. struct nfs_server *server = sp->so_server;
  1008. if (status == -NFS4ERR_BAD_SEQID)
  1009. nfs4_drop_state_owner(sp);
  1010. if (!nfs4_has_session(server->nfs_client))
  1011. nfs_increment_seqid(status, seqid);
  1012. }
  1013. /*
  1014. * Increment the seqid if the LOCK/LOCKU succeeded, or
  1015. * failed with a seqid incrementing error -
  1016. * see comments nfs_fs.h:seqid_mutating_error()
  1017. */
  1018. void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid)
  1019. {
  1020. nfs_increment_seqid(status, seqid);
  1021. }
  1022. int nfs_wait_on_sequence(struct nfs_seqid *seqid, struct rpc_task *task)
  1023. {
  1024. struct nfs_seqid_counter *sequence = seqid->sequence;
  1025. int status = 0;
  1026. spin_lock(&sequence->lock);
  1027. seqid->task = task;
  1028. if (list_empty(&seqid->list))
  1029. list_add_tail(&seqid->list, &sequence->list);
  1030. if (list_first_entry(&sequence->list, struct nfs_seqid, list) == seqid)
  1031. goto unlock;
  1032. rpc_sleep_on(&sequence->wait, task, NULL);
  1033. status = -EAGAIN;
  1034. unlock:
  1035. spin_unlock(&sequence->lock);
  1036. return status;
  1037. }
  1038. static int nfs4_run_state_manager(void *);
  1039. static void nfs4_clear_state_manager_bit(struct nfs_client *clp)
  1040. {
  1041. smp_mb__before_clear_bit();
  1042. clear_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state);
  1043. smp_mb__after_clear_bit();
  1044. wake_up_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING);
  1045. rpc_wake_up(&clp->cl_rpcwaitq);
  1046. }
  1047. /*
  1048. * Schedule the nfs_client asynchronous state management routine
  1049. */
  1050. void nfs4_schedule_state_manager(struct nfs_client *clp)
  1051. {
  1052. struct task_struct *task;
  1053. char buf[INET6_ADDRSTRLEN + sizeof("-manager") + 1];
  1054. if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
  1055. return;
  1056. __module_get(THIS_MODULE);
  1057. atomic_inc(&clp->cl_count);
  1058. /* The rcu_read_lock() is not strictly necessary, as the state
  1059. * manager is the only thread that ever changes the rpc_xprt
  1060. * after it's initialized. At this point, we're single threaded. */
  1061. rcu_read_lock();
  1062. snprintf(buf, sizeof(buf), "%s-manager",
  1063. rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR));
  1064. rcu_read_unlock();
  1065. task = kthread_run(nfs4_run_state_manager, clp, "%s", buf);
  1066. if (IS_ERR(task)) {
  1067. printk(KERN_ERR "%s: kthread_run: %ld\n",
  1068. __func__, PTR_ERR(task));
  1069. nfs4_clear_state_manager_bit(clp);
  1070. nfs_put_client(clp);
  1071. module_put(THIS_MODULE);
  1072. }
  1073. }
  1074. /*
  1075. * Schedule a lease recovery attempt
  1076. */
  1077. void nfs4_schedule_lease_recovery(struct nfs_client *clp)
  1078. {
  1079. if (!clp)
  1080. return;
  1081. if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
  1082. set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
  1083. dprintk("%s: scheduling lease recovery for server %s\n", __func__,
  1084. clp->cl_hostname);
  1085. nfs4_schedule_state_manager(clp);
  1086. }
  1087. EXPORT_SYMBOL_GPL(nfs4_schedule_lease_recovery);
  1088. int nfs4_wait_clnt_recover(struct nfs_client *clp)
  1089. {
  1090. int res;
  1091. might_sleep();
  1092. res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
  1093. nfs_wait_bit_killable, TASK_KILLABLE);
  1094. if (res)
  1095. return res;
  1096. if (clp->cl_cons_state < 0)
  1097. return clp->cl_cons_state;
  1098. return 0;
  1099. }
  1100. int nfs4_client_recover_expired_lease(struct nfs_client *clp)
  1101. {
  1102. unsigned int loop;
  1103. int ret;
  1104. for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
  1105. ret = nfs4_wait_clnt_recover(clp);
  1106. if (ret != 0)
  1107. break;
  1108. if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
  1109. !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
  1110. break;
  1111. nfs4_schedule_state_manager(clp);
  1112. ret = -EIO;
  1113. }
  1114. return ret;
  1115. }
  1116. /*
  1117. * nfs40_handle_cb_pathdown - return all delegations after NFS4ERR_CB_PATH_DOWN
  1118. * @clp: client to process
  1119. *
  1120. * Set the NFS4CLNT_LEASE_EXPIRED state in order to force a
  1121. * resend of the SETCLIENTID and hence re-establish the
  1122. * callback channel. Then return all existing delegations.
  1123. */
  1124. static void nfs40_handle_cb_pathdown(struct nfs_client *clp)
  1125. {
  1126. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  1127. nfs_expire_all_delegations(clp);
  1128. dprintk("%s: handling CB_PATHDOWN recovery for server %s\n", __func__,
  1129. clp->cl_hostname);
  1130. }
  1131. void nfs4_schedule_path_down_recovery(struct nfs_client *clp)
  1132. {
  1133. nfs40_handle_cb_pathdown(clp);
  1134. nfs4_schedule_state_manager(clp);
  1135. }
  1136. static int nfs4_state_mark_reclaim_reboot(struct nfs_client *clp, struct nfs4_state *state)
  1137. {
  1138. set_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
  1139. /* Don't recover state that expired before the reboot */
  1140. if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags)) {
  1141. clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
  1142. return 0;
  1143. }
  1144. set_bit(NFS_OWNER_RECLAIM_REBOOT, &state->owner->so_flags);
  1145. set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
  1146. return 1;
  1147. }
  1148. static int nfs4_state_mark_reclaim_nograce(struct nfs_client *clp, struct nfs4_state *state)
  1149. {
  1150. set_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags);
  1151. clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
  1152. set_bit(NFS_OWNER_RECLAIM_NOGRACE, &state->owner->so_flags);
  1153. set_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state);
  1154. return 1;
  1155. }
  1156. int nfs4_schedule_stateid_recovery(const struct nfs_server *server, struct nfs4_state *state)
  1157. {
  1158. struct nfs_client *clp = server->nfs_client;
  1159. if (!nfs4_valid_open_stateid(state))
  1160. return -EBADF;
  1161. nfs4_state_mark_reclaim_nograce(clp, state);
  1162. dprintk("%s: scheduling stateid recovery for server %s\n", __func__,
  1163. clp->cl_hostname);
  1164. nfs4_schedule_state_manager(clp);
  1165. return 0;
  1166. }
  1167. EXPORT_SYMBOL_GPL(nfs4_schedule_stateid_recovery);
  1168. void nfs_inode_find_state_and_recover(struct inode *inode,
  1169. const nfs4_stateid *stateid)
  1170. {
  1171. struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
  1172. struct nfs_inode *nfsi = NFS_I(inode);
  1173. struct nfs_open_context *ctx;
  1174. struct nfs4_state *state;
  1175. bool found = false;
  1176. spin_lock(&inode->i_lock);
  1177. list_for_each_entry(ctx, &nfsi->open_files, list) {
  1178. state = ctx->state;
  1179. if (state == NULL)
  1180. continue;
  1181. if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
  1182. continue;
  1183. if (!nfs4_stateid_match(&state->stateid, stateid))
  1184. continue;
  1185. nfs4_state_mark_reclaim_nograce(clp, state);
  1186. found = true;
  1187. }
  1188. spin_unlock(&inode->i_lock);
  1189. if (found)
  1190. nfs4_schedule_state_manager(clp);
  1191. }
  1192. static void nfs4_state_mark_open_context_bad(struct nfs4_state *state)
  1193. {
  1194. struct inode *inode = state->inode;
  1195. struct nfs_inode *nfsi = NFS_I(inode);
  1196. struct nfs_open_context *ctx;
  1197. spin_lock(&inode->i_lock);
  1198. list_for_each_entry(ctx, &nfsi->open_files, list) {
  1199. if (ctx->state != state)
  1200. continue;
  1201. set_bit(NFS_CONTEXT_BAD, &ctx->flags);
  1202. }
  1203. spin_unlock(&inode->i_lock);
  1204. }
  1205. static void nfs4_state_mark_recovery_failed(struct nfs4_state *state, int error)
  1206. {
  1207. set_bit(NFS_STATE_RECOVERY_FAILED, &state->flags);
  1208. nfs4_state_mark_open_context_bad(state);
  1209. }
  1210. static int nfs4_reclaim_locks(struct nfs4_state *state, const struct nfs4_state_recovery_ops *ops)
  1211. {
  1212. struct inode *inode = state->inode;
  1213. struct nfs_inode *nfsi = NFS_I(inode);
  1214. struct file_lock *fl;
  1215. int status = 0;
  1216. if (inode->i_flock == NULL)
  1217. return 0;
  1218. /* Guard against delegation returns and new lock/unlock calls */
  1219. down_write(&nfsi->rwsem);
  1220. /* Protect inode->i_flock using the BKL */
  1221. spin_lock(&inode->i_lock);
  1222. for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
  1223. if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
  1224. continue;
  1225. if (nfs_file_open_context(fl->fl_file)->state != state)
  1226. continue;
  1227. spin_unlock(&inode->i_lock);
  1228. status = ops->recover_lock(state, fl);
  1229. switch (status) {
  1230. case 0:
  1231. break;
  1232. case -ESTALE:
  1233. case -NFS4ERR_ADMIN_REVOKED:
  1234. case -NFS4ERR_STALE_STATEID:
  1235. case -NFS4ERR_BAD_STATEID:
  1236. case -NFS4ERR_EXPIRED:
  1237. case -NFS4ERR_NO_GRACE:
  1238. case -NFS4ERR_STALE_CLIENTID:
  1239. case -NFS4ERR_BADSESSION:
  1240. case -NFS4ERR_BADSLOT:
  1241. case -NFS4ERR_BAD_HIGH_SLOT:
  1242. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  1243. goto out;
  1244. default:
  1245. printk(KERN_ERR "NFS: %s: unhandled error %d. "
  1246. "Zeroing state\n", __func__, status);
  1247. case -ENOMEM:
  1248. case -NFS4ERR_DENIED:
  1249. case -NFS4ERR_RECLAIM_BAD:
  1250. case -NFS4ERR_RECLAIM_CONFLICT:
  1251. /* kill_proc(fl->fl_pid, SIGLOST, 1); */
  1252. status = 0;
  1253. }
  1254. spin_lock(&inode->i_lock);
  1255. }
  1256. spin_unlock(&inode->i_lock);
  1257. out:
  1258. up_write(&nfsi->rwsem);
  1259. return status;
  1260. }
  1261. static int nfs4_reclaim_open_state(struct nfs4_state_owner *sp, const struct nfs4_state_recovery_ops *ops)
  1262. {
  1263. struct nfs4_state *state;
  1264. struct nfs4_lock_state *lock;
  1265. int status = 0;
  1266. /* Note: we rely on the sp->so_states list being ordered
  1267. * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
  1268. * states first.
  1269. * This is needed to ensure that the server won't give us any
  1270. * read delegations that we have to return if, say, we are
  1271. * recovering after a network partition or a reboot from a
  1272. * server that doesn't support a grace period.
  1273. */
  1274. spin_lock(&sp->so_lock);
  1275. write_seqcount_begin(&sp->so_reclaim_seqcount);
  1276. restart:
  1277. list_for_each_entry(state, &sp->so_states, open_states) {
  1278. if (!test_and_clear_bit(ops->state_flag_bit, &state->flags))
  1279. continue;
  1280. if (!nfs4_valid_open_stateid(state))
  1281. continue;
  1282. if (state->state == 0)
  1283. continue;
  1284. atomic_inc(&state->count);
  1285. spin_unlock(&sp->so_lock);
  1286. status = ops->recover_open(sp, state);
  1287. if (status >= 0) {
  1288. status = nfs4_reclaim_locks(state, ops);
  1289. if (status >= 0) {
  1290. spin_lock(&state->state_lock);
  1291. list_for_each_entry(lock, &state->lock_states, ls_locks) {
  1292. if (!test_bit(NFS_LOCK_INITIALIZED, &lock->ls_flags))
  1293. pr_warn_ratelimited("NFS: "
  1294. "%s: Lock reclaim "
  1295. "failed!\n", __func__);
  1296. }
  1297. spin_unlock(&state->state_lock);
  1298. nfs4_put_open_state(state);
  1299. spin_lock(&sp->so_lock);
  1300. goto restart;
  1301. }
  1302. }
  1303. switch (status) {
  1304. default:
  1305. printk(KERN_ERR "NFS: %s: unhandled error %d. "
  1306. "Zeroing state\n", __func__, status);
  1307. case -ENOENT:
  1308. case -ENOMEM:
  1309. case -ESTALE:
  1310. /*
  1311. * Open state on this file cannot be recovered
  1312. * All we can do is revert to using the zero stateid.
  1313. */
  1314. nfs4_state_mark_recovery_failed(state, status);
  1315. break;
  1316. case -EAGAIN:
  1317. ssleep(1);
  1318. case -NFS4ERR_ADMIN_REVOKED:
  1319. case -NFS4ERR_STALE_STATEID:
  1320. case -NFS4ERR_BAD_STATEID:
  1321. case -NFS4ERR_RECLAIM_BAD:
  1322. case -NFS4ERR_RECLAIM_CONFLICT:
  1323. nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
  1324. break;
  1325. case -NFS4ERR_EXPIRED:
  1326. case -NFS4ERR_NO_GRACE:
  1327. nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
  1328. case -NFS4ERR_STALE_CLIENTID:
  1329. case -NFS4ERR_BADSESSION:
  1330. case -NFS4ERR_BADSLOT:
  1331. case -NFS4ERR_BAD_HIGH_SLOT:
  1332. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  1333. goto out_err;
  1334. }
  1335. nfs4_put_open_state(state);
  1336. spin_lock(&sp->so_lock);
  1337. goto restart;
  1338. }
  1339. write_seqcount_end(&sp->so_reclaim_seqcount);
  1340. spin_unlock(&sp->so_lock);
  1341. return 0;
  1342. out_err:
  1343. nfs4_put_open_state(state);
  1344. spin_lock(&sp->so_lock);
  1345. write_seqcount_end(&sp->so_reclaim_seqcount);
  1346. spin_unlock(&sp->so_lock);
  1347. return status;
  1348. }
  1349. static void nfs4_clear_open_state(struct nfs4_state *state)
  1350. {
  1351. struct nfs4_lock_state *lock;
  1352. clear_bit(NFS_DELEGATED_STATE, &state->flags);
  1353. clear_bit(NFS_O_RDONLY_STATE, &state->flags);
  1354. clear_bit(NFS_O_WRONLY_STATE, &state->flags);
  1355. clear_bit(NFS_O_RDWR_STATE, &state->flags);
  1356. spin_lock(&state->state_lock);
  1357. list_for_each_entry(lock, &state->lock_states, ls_locks) {
  1358. lock->ls_seqid.flags = 0;
  1359. clear_bit(NFS_LOCK_INITIALIZED, &lock->ls_flags);
  1360. }
  1361. spin_unlock(&state->state_lock);
  1362. }
  1363. static void nfs4_reset_seqids(struct nfs_server *server,
  1364. int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
  1365. {
  1366. struct nfs_client *clp = server->nfs_client;
  1367. struct nfs4_state_owner *sp;
  1368. struct rb_node *pos;
  1369. struct nfs4_state *state;
  1370. spin_lock(&clp->cl_lock);
  1371. for (pos = rb_first(&server->state_owners);
  1372. pos != NULL;
  1373. pos = rb_next(pos)) {
  1374. sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
  1375. sp->so_seqid.flags = 0;
  1376. spin_lock(&sp->so_lock);
  1377. list_for_each_entry(state, &sp->so_states, open_states) {
  1378. if (mark_reclaim(clp, state))
  1379. nfs4_clear_open_state(state);
  1380. }
  1381. spin_unlock(&sp->so_lock);
  1382. }
  1383. spin_unlock(&clp->cl_lock);
  1384. }
  1385. static void nfs4_state_mark_reclaim_helper(struct nfs_client *clp,
  1386. int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
  1387. {
  1388. struct nfs_server *server;
  1389. rcu_read_lock();
  1390. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  1391. nfs4_reset_seqids(server, mark_reclaim);
  1392. rcu_read_unlock();
  1393. }
  1394. static void nfs4_state_start_reclaim_reboot(struct nfs_client *clp)
  1395. {
  1396. /* Mark all delegations for reclaim */
  1397. nfs_delegation_mark_reclaim(clp);
  1398. nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_reboot);
  1399. }
  1400. static void nfs4_reclaim_complete(struct nfs_client *clp,
  1401. const struct nfs4_state_recovery_ops *ops,
  1402. struct rpc_cred *cred)
  1403. {
  1404. /* Notify the server we're done reclaiming our state */
  1405. if (ops->reclaim_complete)
  1406. (void)ops->reclaim_complete(clp, cred);
  1407. }
  1408. static void nfs4_clear_reclaim_server(struct nfs_server *server)
  1409. {
  1410. struct nfs_client *clp = server->nfs_client;
  1411. struct nfs4_state_owner *sp;
  1412. struct rb_node *pos;
  1413. struct nfs4_state *state;
  1414. spin_lock(&clp->cl_lock);
  1415. for (pos = rb_first(&server->state_owners);
  1416. pos != NULL;
  1417. pos = rb_next(pos)) {
  1418. sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
  1419. spin_lock(&sp->so_lock);
  1420. list_for_each_entry(state, &sp->so_states, open_states) {
  1421. if (!test_and_clear_bit(NFS_STATE_RECLAIM_REBOOT,
  1422. &state->flags))
  1423. continue;
  1424. nfs4_state_mark_reclaim_nograce(clp, state);
  1425. }
  1426. spin_unlock(&sp->so_lock);
  1427. }
  1428. spin_unlock(&clp->cl_lock);
  1429. }
  1430. static int nfs4_state_clear_reclaim_reboot(struct nfs_client *clp)
  1431. {
  1432. struct nfs_server *server;
  1433. if (!test_and_clear_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
  1434. return 0;
  1435. rcu_read_lock();
  1436. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
  1437. nfs4_clear_reclaim_server(server);
  1438. rcu_read_unlock();
  1439. nfs_delegation_reap_unclaimed(clp);
  1440. return 1;
  1441. }
  1442. static void nfs4_state_end_reclaim_reboot(struct nfs_client *clp)
  1443. {
  1444. const struct nfs4_state_recovery_ops *ops;
  1445. struct rpc_cred *cred;
  1446. if (!nfs4_state_clear_reclaim_reboot(clp))
  1447. return;
  1448. ops = clp->cl_mvops->reboot_recovery_ops;
  1449. cred = ops->get_clid_cred(clp);
  1450. nfs4_reclaim_complete(clp, ops, cred);
  1451. put_rpccred(cred);
  1452. }
  1453. static void nfs_delegation_clear_all(struct nfs_client *clp)
  1454. {
  1455. nfs_delegation_mark_reclaim(clp);
  1456. nfs_delegation_reap_unclaimed(clp);
  1457. }
  1458. static void nfs4_state_start_reclaim_nograce(struct nfs_client *clp)
  1459. {
  1460. nfs_delegation_clear_all(clp);
  1461. nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_nograce);
  1462. }
  1463. static int nfs4_recovery_handle_error(struct nfs_client *clp, int error)
  1464. {
  1465. switch (error) {
  1466. case 0:
  1467. break;
  1468. case -NFS4ERR_CB_PATH_DOWN:
  1469. nfs40_handle_cb_pathdown(clp);
  1470. break;
  1471. case -NFS4ERR_NO_GRACE:
  1472. nfs4_state_end_reclaim_reboot(clp);
  1473. break;
  1474. case -NFS4ERR_STALE_CLIENTID:
  1475. case -NFS4ERR_LEASE_MOVED:
  1476. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  1477. nfs4_state_clear_reclaim_reboot(clp);
  1478. nfs4_state_start_reclaim_reboot(clp);
  1479. break;
  1480. case -NFS4ERR_EXPIRED:
  1481. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  1482. nfs4_state_start_reclaim_nograce(clp);
  1483. break;
  1484. case -NFS4ERR_BADSESSION:
  1485. case -NFS4ERR_BADSLOT:
  1486. case -NFS4ERR_BAD_HIGH_SLOT:
  1487. case -NFS4ERR_DEADSESSION:
  1488. case -NFS4ERR_SEQ_FALSE_RETRY:
  1489. case -NFS4ERR_SEQ_MISORDERED:
  1490. set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
  1491. /* Zero session reset errors */
  1492. break;
  1493. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  1494. set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
  1495. break;
  1496. default:
  1497. dprintk("%s: failed to handle error %d for server %s\n",
  1498. __func__, error, clp->cl_hostname);
  1499. return error;
  1500. }
  1501. dprintk("%s: handled error %d for server %s\n", __func__, error,
  1502. clp->cl_hostname);
  1503. return 0;
  1504. }
  1505. static int nfs4_do_reclaim(struct nfs_client *clp, const struct nfs4_state_recovery_ops *ops)
  1506. {
  1507. struct nfs4_state_owner *sp;
  1508. struct nfs_server *server;
  1509. struct rb_node *pos;
  1510. int status = 0;
  1511. restart:
  1512. rcu_read_lock();
  1513. list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
  1514. nfs4_purge_state_owners(server);
  1515. spin_lock(&clp->cl_lock);
  1516. for (pos = rb_first(&server->state_owners);
  1517. pos != NULL;
  1518. pos = rb_next(pos)) {
  1519. sp = rb_entry(pos,
  1520. struct nfs4_state_owner, so_server_node);
  1521. if (!test_and_clear_bit(ops->owner_flag_bit,
  1522. &sp->so_flags))
  1523. continue;
  1524. atomic_inc(&sp->so_count);
  1525. spin_unlock(&clp->cl_lock);
  1526. rcu_read_unlock();
  1527. status = nfs4_reclaim_open_state(sp, ops);
  1528. if (status < 0) {
  1529. set_bit(ops->owner_flag_bit, &sp->so_flags);
  1530. nfs4_put_state_owner(sp);
  1531. return nfs4_recovery_handle_error(clp, status);
  1532. }
  1533. nfs4_put_state_owner(sp);
  1534. goto restart;
  1535. }
  1536. spin_unlock(&clp->cl_lock);
  1537. }
  1538. rcu_read_unlock();
  1539. return status;
  1540. }
  1541. static int nfs4_check_lease(struct nfs_client *clp)
  1542. {
  1543. struct rpc_cred *cred;
  1544. const struct nfs4_state_maintenance_ops *ops =
  1545. clp->cl_mvops->state_renewal_ops;
  1546. int status;
  1547. /* Is the client already known to have an expired lease? */
  1548. if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
  1549. return 0;
  1550. spin_lock(&clp->cl_lock);
  1551. cred = ops->get_state_renewal_cred_locked(clp);
  1552. spin_unlock(&clp->cl_lock);
  1553. if (cred == NULL) {
  1554. cred = nfs4_get_setclientid_cred(clp);
  1555. status = -ENOKEY;
  1556. if (cred == NULL)
  1557. goto out;
  1558. }
  1559. status = ops->renew_lease(clp, cred);
  1560. put_rpccred(cred);
  1561. if (status == -ETIMEDOUT) {
  1562. set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
  1563. return 0;
  1564. }
  1565. out:
  1566. return nfs4_recovery_handle_error(clp, status);
  1567. }
  1568. /* Set NFS4CLNT_LEASE_EXPIRED and reclaim reboot state for all v4.0 errors
  1569. * and for recoverable errors on EXCHANGE_ID for v4.1
  1570. */
  1571. static int nfs4_handle_reclaim_lease_error(struct nfs_client *clp, int status)
  1572. {
  1573. switch (status) {
  1574. case -NFS4ERR_SEQ_MISORDERED:
  1575. if (test_and_set_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state))
  1576. return -ESERVERFAULT;
  1577. /* Lease confirmation error: retry after purging the lease */
  1578. ssleep(1);
  1579. clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
  1580. break;
  1581. case -NFS4ERR_STALE_CLIENTID:
  1582. clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
  1583. nfs4_state_clear_reclaim_reboot(clp);
  1584. nfs4_state_start_reclaim_reboot(clp);
  1585. break;
  1586. case -NFS4ERR_CLID_INUSE:
  1587. pr_err("NFS: Server %s reports our clientid is in use\n",
  1588. clp->cl_hostname);
  1589. nfs_mark_client_ready(clp, -EPERM);
  1590. clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
  1591. return -EPERM;
  1592. case -EACCES:
  1593. case -NFS4ERR_DELAY:
  1594. case -ETIMEDOUT:
  1595. case -EAGAIN:
  1596. ssleep(1);
  1597. break;
  1598. case -NFS4ERR_MINOR_VERS_MISMATCH:
  1599. if (clp->cl_cons_state == NFS_CS_SESSION_INITING)
  1600. nfs_mark_client_ready(clp, -EPROTONOSUPPORT);
  1601. dprintk("%s: exit with error %d for server %s\n",
  1602. __func__, -EPROTONOSUPPORT, clp->cl_hostname);
  1603. return -EPROTONOSUPPORT;
  1604. case -NFS4ERR_NOT_SAME: /* FixMe: implement recovery
  1605. * in nfs4_exchange_id */
  1606. default:
  1607. dprintk("%s: exit with error %d for server %s\n", __func__,
  1608. status, clp->cl_hostname);
  1609. return status;
  1610. }
  1611. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  1612. dprintk("%s: handled error %d for server %s\n", __func__, status,
  1613. clp->cl_hostname);
  1614. return 0;
  1615. }
  1616. static int nfs4_establish_lease(struct nfs_client *clp)
  1617. {
  1618. struct rpc_cred *cred;
  1619. const struct nfs4_state_recovery_ops *ops =
  1620. clp->cl_mvops->reboot_recovery_ops;
  1621. int status;
  1622. cred = ops->get_clid_cred(clp);
  1623. if (cred == NULL)
  1624. return -ENOENT;
  1625. status = ops->establish_clid(clp, cred);
  1626. put_rpccred(cred);
  1627. if (status != 0)
  1628. return status;
  1629. pnfs_destroy_all_layouts(clp);
  1630. return 0;
  1631. }
  1632. /*
  1633. * Returns zero or a negative errno. NFS4ERR values are converted
  1634. * to local errno values.
  1635. */
  1636. static int nfs4_reclaim_lease(struct nfs_client *clp)
  1637. {
  1638. int status;
  1639. status = nfs4_establish_lease(clp);
  1640. if (status < 0)
  1641. return nfs4_handle_reclaim_lease_error(clp, status);
  1642. if (test_and_clear_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state))
  1643. nfs4_state_start_reclaim_nograce(clp);
  1644. if (!test_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state))
  1645. set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
  1646. clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
  1647. clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  1648. return 0;
  1649. }
  1650. static int nfs4_purge_lease(struct nfs_client *clp)
  1651. {
  1652. int status;
  1653. status = nfs4_establish_lease(clp);
  1654. if (status < 0)
  1655. return nfs4_handle_reclaim_lease_error(clp, status);
  1656. clear_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state);
  1657. set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
  1658. nfs4_state_start_reclaim_nograce(clp);
  1659. return 0;
  1660. }
  1661. /**
  1662. * nfs4_discover_server_trunking - Detect server IP address trunking
  1663. *
  1664. * @clp: nfs_client under test
  1665. * @result: OUT: found nfs_client, or clp
  1666. *
  1667. * Returns zero or a negative errno. If zero is returned,
  1668. * an nfs_client pointer is planted in "result".
  1669. *
  1670. * Note: since we are invoked in process context, and
  1671. * not from inside the state manager, we cannot use
  1672. * nfs4_handle_reclaim_lease_error().
  1673. */
  1674. int nfs4_discover_server_trunking(struct nfs_client *clp,
  1675. struct nfs_client **result)
  1676. {
  1677. const struct nfs4_state_recovery_ops *ops =
  1678. clp->cl_mvops->reboot_recovery_ops;
  1679. struct rpc_clnt *clnt;
  1680. struct rpc_cred *cred;
  1681. int i, status;
  1682. dprintk("NFS: %s: testing '%s'\n", __func__, clp->cl_hostname);
  1683. clnt = clp->cl_rpcclient;
  1684. i = 0;
  1685. mutex_lock(&nfs_clid_init_mutex);
  1686. again:
  1687. status = -ENOENT;
  1688. cred = ops->get_clid_cred(clp);
  1689. if (cred == NULL)
  1690. goto out_unlock;
  1691. status = ops->detect_trunking(clp, result, cred);
  1692. put_rpccred(cred);
  1693. switch (status) {
  1694. case 0:
  1695. break;
  1696. case -NFS4ERR_DELAY:
  1697. case -ETIMEDOUT:
  1698. case -EAGAIN:
  1699. ssleep(1);
  1700. case -NFS4ERR_STALE_CLIENTID:
  1701. dprintk("NFS: %s after status %d, retrying\n",
  1702. __func__, status);
  1703. goto again;
  1704. case -EACCES:
  1705. if (i++)
  1706. break;
  1707. case -NFS4ERR_CLID_INUSE:
  1708. case -NFS4ERR_WRONGSEC:
  1709. clnt = rpc_clone_client_set_auth(clnt, RPC_AUTH_UNIX);
  1710. if (IS_ERR(clnt)) {
  1711. status = PTR_ERR(clnt);
  1712. break;
  1713. }
  1714. /* Note: this is safe because we haven't yet marked the
  1715. * client as ready, so we are the only user of
  1716. * clp->cl_rpcclient
  1717. */
  1718. clnt = xchg(&clp->cl_rpcclient, clnt);
  1719. rpc_shutdown_client(clnt);
  1720. clnt = clp->cl_rpcclient;
  1721. goto again;
  1722. case -NFS4ERR_MINOR_VERS_MISMATCH:
  1723. status = -EPROTONOSUPPORT;
  1724. break;
  1725. case -EKEYEXPIRED:
  1726. case -NFS4ERR_NOT_SAME: /* FixMe: implement recovery
  1727. * in nfs4_exchange_id */
  1728. status = -EKEYEXPIRED;
  1729. break;
  1730. default:
  1731. pr_warn("NFS: %s unhandled error %d. Exiting with error EIO\n",
  1732. __func__, status);
  1733. status = -EIO;
  1734. }
  1735. out_unlock:
  1736. mutex_unlock(&nfs_clid_init_mutex);
  1737. dprintk("NFS: %s: status = %d\n", __func__, status);
  1738. return status;
  1739. }
  1740. #ifdef CONFIG_NFS_V4_1
  1741. void nfs4_schedule_session_recovery(struct nfs4_session *session, int err)
  1742. {
  1743. struct nfs_client *clp = session->clp;
  1744. switch (err) {
  1745. default:
  1746. set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
  1747. break;
  1748. case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
  1749. set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
  1750. }
  1751. nfs4_schedule_lease_recovery(clp);
  1752. }
  1753. EXPORT_SYMBOL_GPL(nfs4_schedule_session_recovery);
  1754. static void nfs41_ping_server(struct nfs_client *clp)
  1755. {
  1756. /* Use CHECK_LEASE to ping the server with a SEQUENCE */
  1757. set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
  1758. nfs4_schedule_state_manager(clp);
  1759. }
  1760. void nfs41_server_notify_target_slotid_update(struct nfs_client *clp)
  1761. {
  1762. nfs41_ping_server(clp);
  1763. }
  1764. void nfs41_server_notify_highest_slotid_update(struct nfs_client *clp)
  1765. {
  1766. nfs41_ping_server(clp);
  1767. }
  1768. static void nfs4_reset_all_state(struct nfs_client *clp)
  1769. {
  1770. if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
  1771. set_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state);
  1772. clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
  1773. nfs4_state_start_reclaim_nograce(clp);
  1774. dprintk("%s: scheduling reset of all state for server %s!\n",
  1775. __func__, clp->cl_hostname);
  1776. nfs4_schedule_state_manager(clp);
  1777. }
  1778. }
  1779. static void nfs41_handle_server_reboot(struct nfs_client *clp)
  1780. {
  1781. if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
  1782. nfs4_state_start_reclaim_reboot(clp);
  1783. dprintk("%s: server %s rebooted!\n", __func__,
  1784. clp->cl_hostname);
  1785. nfs4_schedule_state_manager(clp);
  1786. }
  1787. }
  1788. static void nfs41_handle_state_revoked(struct nfs_client *clp)
  1789. {
  1790. nfs4_reset_all_state(clp);
  1791. dprintk("%s: state revoked on server %s\n", __func__, clp->cl_hostname);
  1792. }
  1793. static void nfs41_handle_recallable_state_revoked(struct nfs_client *clp)
  1794. {
  1795. /* This will need to handle layouts too */
  1796. nfs_expire_all_delegations(clp);
  1797. dprintk("%s: Recallable state revoked on server %s!\n", __func__,
  1798. clp->cl_hostname);
  1799. }
  1800. static void nfs41_handle_backchannel_fault(struct nfs_client *clp)
  1801. {
  1802. nfs_expire_all_delegations(clp);
  1803. if (test_and_set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state) == 0)
  1804. nfs4_schedule_state_manager(clp);
  1805. dprintk("%s: server %s declared a backchannel fault\n", __func__,
  1806. clp->cl_hostname);
  1807. }
  1808. static void nfs41_handle_cb_path_down(struct nfs_client *clp)
  1809. {
  1810. if (test_and_set_bit(NFS4CLNT_BIND_CONN_TO_SESSION,
  1811. &clp->cl_state) == 0)
  1812. nfs4_schedule_state_manager(clp);
  1813. }
  1814. void nfs41_handle_sequence_flag_errors(struct nfs_client *clp, u32 flags)
  1815. {
  1816. if (!flags)
  1817. return;
  1818. dprintk("%s: \"%s\" (client ID %llx) flags=0x%08x\n",
  1819. __func__, clp->cl_hostname, clp->cl_clientid, flags);
  1820. if (flags & SEQ4_STATUS_RESTART_RECLAIM_NEEDED)
  1821. nfs41_handle_server_reboot(clp);
  1822. if (flags & (SEQ4_STATUS_EXPIRED_ALL_STATE_REVOKED |
  1823. SEQ4_STATUS_EXPIRED_SOME_STATE_REVOKED |
  1824. SEQ4_STATUS_ADMIN_STATE_REVOKED |
  1825. SEQ4_STATUS_LEASE_MOVED))
  1826. nfs41_handle_state_revoked(clp);
  1827. if (flags & SEQ4_STATUS_RECALLABLE_STATE_REVOKED)
  1828. nfs41_handle_recallable_state_revoked(clp);
  1829. if (flags & SEQ4_STATUS_BACKCHANNEL_FAULT)
  1830. nfs41_handle_backchannel_fault(clp);
  1831. else if (flags & (SEQ4_STATUS_CB_PATH_DOWN |
  1832. SEQ4_STATUS_CB_PATH_DOWN_SESSION))
  1833. nfs41_handle_cb_path_down(clp);
  1834. }
  1835. static int nfs4_reset_session(struct nfs_client *clp)
  1836. {
  1837. struct rpc_cred *cred;
  1838. int status;
  1839. if (!nfs4_has_session(clp))
  1840. return 0;
  1841. nfs4_begin_drain_session(clp);
  1842. cred = nfs4_get_exchange_id_cred(clp);
  1843. status = nfs4_proc_destroy_session(clp->cl_session, cred);
  1844. switch (status) {
  1845. case 0:
  1846. case -NFS4ERR_BADSESSION:
  1847. case -NFS4ERR_DEADSESSION:
  1848. break;
  1849. case -NFS4ERR_BACK_CHAN_BUSY:
  1850. case -NFS4ERR_DELAY:
  1851. set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
  1852. status = 0;
  1853. ssleep(1);
  1854. goto out;
  1855. default:
  1856. status = nfs4_recovery_handle_error(clp, status);
  1857. goto out;
  1858. }
  1859. memset(clp->cl_session->sess_id.data, 0, NFS4_MAX_SESSIONID_LEN);
  1860. status = nfs4_proc_create_session(clp, cred);
  1861. if (status) {
  1862. dprintk("%s: session reset failed with status %d for server %s!\n",
  1863. __func__, status, clp->cl_hostname);
  1864. status = nfs4_handle_reclaim_lease_error(clp, status);
  1865. goto out;
  1866. }
  1867. nfs41_finish_session_reset(clp);
  1868. dprintk("%s: session reset was successful for server %s!\n",
  1869. __func__, clp->cl_hostname);
  1870. out:
  1871. if (cred)
  1872. put_rpccred(cred);
  1873. return status;
  1874. }
  1875. static int nfs4_bind_conn_to_session(struct nfs_client *clp)
  1876. {
  1877. struct rpc_cred *cred;
  1878. int ret;
  1879. if (!nfs4_has_session(clp))
  1880. return 0;
  1881. nfs4_begin_drain_session(clp);
  1882. cred = nfs4_get_exchange_id_cred(clp);
  1883. ret = nfs4_proc_bind_conn_to_session(clp, cred);
  1884. if (cred)
  1885. put_rpccred(cred);
  1886. clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
  1887. switch (ret) {
  1888. case 0:
  1889. dprintk("%s: bind_conn_to_session was successful for server %s!\n",
  1890. __func__, clp->cl_hostname);
  1891. break;
  1892. case -NFS4ERR_DELAY:
  1893. ssleep(1);
  1894. set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
  1895. break;
  1896. default:
  1897. return nfs4_recovery_handle_error(clp, ret);
  1898. }
  1899. return 0;
  1900. }
  1901. #else /* CONFIG_NFS_V4_1 */
  1902. static int nfs4_reset_session(struct nfs_client *clp) { return 0; }
  1903. static int nfs4_end_drain_session(struct nfs_client *clp) { return 0; }
  1904. static int nfs4_bind_conn_to_session(struct nfs_client *clp)
  1905. {
  1906. return 0;
  1907. }
  1908. #endif /* CONFIG_NFS_V4_1 */
  1909. static void nfs4_state_manager(struct nfs_client *clp)
  1910. {
  1911. int status = 0;
  1912. const char *section = "", *section_sep = "";
  1913. /* Ensure exclusive access to NFSv4 state */
  1914. do {
  1915. if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
  1916. section = "purge state";
  1917. status = nfs4_purge_lease(clp);
  1918. if (status < 0)
  1919. goto out_error;
  1920. continue;
  1921. }
  1922. if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state)) {
  1923. section = "lease expired";
  1924. /* We're going to have to re-establish a clientid */
  1925. status = nfs4_reclaim_lease(clp);
  1926. if (status < 0)
  1927. goto out_error;
  1928. continue;
  1929. }
  1930. /* Initialize or reset the session */
  1931. if (test_and_clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state)) {
  1932. section = "reset session";
  1933. status = nfs4_reset_session(clp);
  1934. if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
  1935. continue;
  1936. if (status < 0)
  1937. goto out_error;
  1938. }
  1939. /* Send BIND_CONN_TO_SESSION */
  1940. if (test_and_clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION,
  1941. &clp->cl_state)) {
  1942. section = "bind conn to session";
  1943. status = nfs4_bind_conn_to_session(clp);
  1944. if (status < 0)
  1945. goto out_error;
  1946. continue;
  1947. }
  1948. if (test_and_clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state)) {
  1949. section = "check lease";
  1950. status = nfs4_check_lease(clp);
  1951. if (status < 0)
  1952. goto out_error;
  1953. continue;
  1954. }
  1955. /* First recover reboot state... */
  1956. if (test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state)) {
  1957. section = "reclaim reboot";
  1958. status = nfs4_do_reclaim(clp,
  1959. clp->cl_mvops->reboot_recovery_ops);
  1960. if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) ||
  1961. test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state))
  1962. continue;
  1963. nfs4_state_end_reclaim_reboot(clp);
  1964. if (test_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state))
  1965. continue;
  1966. if (status < 0)
  1967. goto out_error;
  1968. }
  1969. /* Now recover expired state... */
  1970. if (test_and_clear_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state)) {
  1971. section = "reclaim nograce";
  1972. status = nfs4_do_reclaim(clp,
  1973. clp->cl_mvops->nograce_recovery_ops);
  1974. if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) ||
  1975. test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state) ||
  1976. test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
  1977. continue;
  1978. if (status < 0)
  1979. goto out_error;
  1980. }
  1981. nfs4_end_drain_session(clp);
  1982. if (test_and_clear_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state)) {
  1983. nfs_client_return_marked_delegations(clp);
  1984. continue;
  1985. }
  1986. nfs4_clear_state_manager_bit(clp);
  1987. /* Did we race with an attempt to give us more work? */
  1988. if (clp->cl_state == 0)
  1989. break;
  1990. if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
  1991. break;
  1992. } while (atomic_read(&clp->cl_count) > 1);
  1993. return;
  1994. out_error:
  1995. if (strlen(section))
  1996. section_sep = ": ";
  1997. pr_warn_ratelimited("NFS: state manager%s%s failed on NFSv4 server %s"
  1998. " with error %d\n", section_sep, section,
  1999. clp->cl_hostname, -status);
  2000. ssleep(1);
  2001. nfs4_end_drain_session(clp);
  2002. nfs4_clear_state_manager_bit(clp);
  2003. }
  2004. static int nfs4_run_state_manager(void *ptr)
  2005. {
  2006. struct nfs_client *clp = ptr;
  2007. allow_signal(SIGKILL);
  2008. nfs4_state_manager(clp);
  2009. nfs_put_client(clp);
  2010. module_put_and_exit(0);
  2011. return 0;
  2012. }
  2013. /*
  2014. * Local variables:
  2015. * c-basic-offset: 8
  2016. * End:
  2017. */