nfs4state.c 59 KB

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