clnt.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551
  1. /*
  2. * linux/net/sunrpc/clnt.c
  3. *
  4. * This file contains the high-level RPC interface.
  5. * It is modeled as a finite state machine to support both synchronous
  6. * and asynchronous requests.
  7. *
  8. * - RPC header generation and argument serialization.
  9. * - Credential refresh.
  10. * - TCP connect handling.
  11. * - Retry of operation when it is suspected the operation failed because
  12. * of uid squashing on the server, or when the credentials were stale
  13. * and need to be refreshed, or when a packet was damaged in transit.
  14. * This may be have to be moved to the VFS layer.
  15. *
  16. * Copyright (C) 1992,1993 Rick Sladkey <jrs@world.std.com>
  17. * Copyright (C) 1995,1996 Olaf Kirch <okir@monad.swb.de>
  18. */
  19. #include <linux/module.h>
  20. #include <linux/types.h>
  21. #include <linux/kallsyms.h>
  22. #include <linux/mm.h>
  23. #include <linux/namei.h>
  24. #include <linux/mount.h>
  25. #include <linux/slab.h>
  26. #include <linux/rcupdate.h>
  27. #include <linux/utsname.h>
  28. #include <linux/workqueue.h>
  29. #include <linux/in.h>
  30. #include <linux/in6.h>
  31. #include <linux/un.h>
  32. #include <linux/sunrpc/clnt.h>
  33. #include <linux/sunrpc/addr.h>
  34. #include <linux/sunrpc/rpc_pipe_fs.h>
  35. #include <linux/sunrpc/metrics.h>
  36. #include <linux/sunrpc/bc_xprt.h>
  37. #include <trace/events/sunrpc.h>
  38. #include "sunrpc.h"
  39. #include "netns.h"
  40. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  41. # define RPCDBG_FACILITY RPCDBG_CALL
  42. #endif
  43. #define dprint_status(t) \
  44. dprintk("RPC: %5u %s (status %d)\n", t->tk_pid, \
  45. __func__, t->tk_status)
  46. /*
  47. * All RPC clients are linked into this list
  48. */
  49. static DECLARE_WAIT_QUEUE_HEAD(destroy_wait);
  50. static void call_start(struct rpc_task *task);
  51. static void call_reserve(struct rpc_task *task);
  52. static void call_reserveresult(struct rpc_task *task);
  53. static void call_allocate(struct rpc_task *task);
  54. static void call_decode(struct rpc_task *task);
  55. static void call_bind(struct rpc_task *task);
  56. static void call_bind_status(struct rpc_task *task);
  57. static void call_transmit(struct rpc_task *task);
  58. #if defined(CONFIG_SUNRPC_BACKCHANNEL)
  59. static void call_bc_transmit(struct rpc_task *task);
  60. #endif /* CONFIG_SUNRPC_BACKCHANNEL */
  61. static void call_status(struct rpc_task *task);
  62. static void call_transmit_status(struct rpc_task *task);
  63. static void call_refresh(struct rpc_task *task);
  64. static void call_refreshresult(struct rpc_task *task);
  65. static void call_timeout(struct rpc_task *task);
  66. static void call_connect(struct rpc_task *task);
  67. static void call_connect_status(struct rpc_task *task);
  68. static __be32 *rpc_encode_header(struct rpc_task *task);
  69. static __be32 *rpc_verify_header(struct rpc_task *task);
  70. static int rpc_ping(struct rpc_clnt *clnt);
  71. static void rpc_register_client(struct rpc_clnt *clnt)
  72. {
  73. struct net *net = rpc_net_ns(clnt);
  74. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  75. spin_lock(&sn->rpc_client_lock);
  76. list_add(&clnt->cl_clients, &sn->all_clients);
  77. spin_unlock(&sn->rpc_client_lock);
  78. }
  79. static void rpc_unregister_client(struct rpc_clnt *clnt)
  80. {
  81. struct net *net = rpc_net_ns(clnt);
  82. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  83. spin_lock(&sn->rpc_client_lock);
  84. list_del(&clnt->cl_clients);
  85. spin_unlock(&sn->rpc_client_lock);
  86. }
  87. static void __rpc_clnt_remove_pipedir(struct rpc_clnt *clnt)
  88. {
  89. rpc_remove_client_dir(clnt);
  90. }
  91. static void rpc_clnt_remove_pipedir(struct rpc_clnt *clnt)
  92. {
  93. struct net *net = rpc_net_ns(clnt);
  94. struct super_block *pipefs_sb;
  95. pipefs_sb = rpc_get_sb_net(net);
  96. if (pipefs_sb) {
  97. __rpc_clnt_remove_pipedir(clnt);
  98. rpc_put_sb_net(net);
  99. }
  100. }
  101. static struct dentry *rpc_setup_pipedir_sb(struct super_block *sb,
  102. struct rpc_clnt *clnt)
  103. {
  104. static uint32_t clntid;
  105. const char *dir_name = clnt->cl_program->pipe_dir_name;
  106. char name[15];
  107. struct dentry *dir, *dentry;
  108. dir = rpc_d_lookup_sb(sb, dir_name);
  109. if (dir == NULL) {
  110. pr_info("RPC: pipefs directory doesn't exist: %s\n", dir_name);
  111. return dir;
  112. }
  113. for (;;) {
  114. snprintf(name, sizeof(name), "clnt%x", (unsigned int)clntid++);
  115. name[sizeof(name) - 1] = '\0';
  116. dentry = rpc_create_client_dir(dir, name, clnt);
  117. if (!IS_ERR(dentry))
  118. break;
  119. if (dentry == ERR_PTR(-EEXIST))
  120. continue;
  121. printk(KERN_INFO "RPC: Couldn't create pipefs entry"
  122. " %s/%s, error %ld\n",
  123. dir_name, name, PTR_ERR(dentry));
  124. break;
  125. }
  126. dput(dir);
  127. return dentry;
  128. }
  129. static int
  130. rpc_setup_pipedir(struct super_block *pipefs_sb, struct rpc_clnt *clnt)
  131. {
  132. struct dentry *dentry;
  133. if (clnt->cl_program->pipe_dir_name != NULL) {
  134. dentry = rpc_setup_pipedir_sb(pipefs_sb, clnt);
  135. if (IS_ERR(dentry))
  136. return PTR_ERR(dentry);
  137. }
  138. return 0;
  139. }
  140. static int rpc_clnt_skip_event(struct rpc_clnt *clnt, unsigned long event)
  141. {
  142. if (clnt->cl_program->pipe_dir_name == NULL)
  143. return 1;
  144. switch (event) {
  145. case RPC_PIPEFS_MOUNT:
  146. if (clnt->cl_pipedir_objects.pdh_dentry != NULL)
  147. return 1;
  148. if (atomic_read(&clnt->cl_count) == 0)
  149. return 1;
  150. break;
  151. case RPC_PIPEFS_UMOUNT:
  152. if (clnt->cl_pipedir_objects.pdh_dentry == NULL)
  153. return 1;
  154. break;
  155. }
  156. return 0;
  157. }
  158. static int __rpc_clnt_handle_event(struct rpc_clnt *clnt, unsigned long event,
  159. struct super_block *sb)
  160. {
  161. struct dentry *dentry;
  162. int err = 0;
  163. switch (event) {
  164. case RPC_PIPEFS_MOUNT:
  165. dentry = rpc_setup_pipedir_sb(sb, clnt);
  166. if (!dentry)
  167. return -ENOENT;
  168. if (IS_ERR(dentry))
  169. return PTR_ERR(dentry);
  170. break;
  171. case RPC_PIPEFS_UMOUNT:
  172. __rpc_clnt_remove_pipedir(clnt);
  173. break;
  174. default:
  175. printk(KERN_ERR "%s: unknown event: %ld\n", __func__, event);
  176. return -ENOTSUPP;
  177. }
  178. return err;
  179. }
  180. static int __rpc_pipefs_event(struct rpc_clnt *clnt, unsigned long event,
  181. struct super_block *sb)
  182. {
  183. int error = 0;
  184. for (;; clnt = clnt->cl_parent) {
  185. if (!rpc_clnt_skip_event(clnt, event))
  186. error = __rpc_clnt_handle_event(clnt, event, sb);
  187. if (error || clnt == clnt->cl_parent)
  188. break;
  189. }
  190. return error;
  191. }
  192. static struct rpc_clnt *rpc_get_client_for_event(struct net *net, int event)
  193. {
  194. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  195. struct rpc_clnt *clnt;
  196. spin_lock(&sn->rpc_client_lock);
  197. list_for_each_entry(clnt, &sn->all_clients, cl_clients) {
  198. if (rpc_clnt_skip_event(clnt, event))
  199. continue;
  200. spin_unlock(&sn->rpc_client_lock);
  201. return clnt;
  202. }
  203. spin_unlock(&sn->rpc_client_lock);
  204. return NULL;
  205. }
  206. static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event,
  207. void *ptr)
  208. {
  209. struct super_block *sb = ptr;
  210. struct rpc_clnt *clnt;
  211. int error = 0;
  212. while ((clnt = rpc_get_client_for_event(sb->s_fs_info, event))) {
  213. error = __rpc_pipefs_event(clnt, event, sb);
  214. if (error)
  215. break;
  216. }
  217. return error;
  218. }
  219. static struct notifier_block rpc_clients_block = {
  220. .notifier_call = rpc_pipefs_event,
  221. .priority = SUNRPC_PIPEFS_RPC_PRIO,
  222. };
  223. int rpc_clients_notifier_register(void)
  224. {
  225. return rpc_pipefs_notifier_register(&rpc_clients_block);
  226. }
  227. void rpc_clients_notifier_unregister(void)
  228. {
  229. return rpc_pipefs_notifier_unregister(&rpc_clients_block);
  230. }
  231. static struct rpc_xprt *rpc_clnt_set_transport(struct rpc_clnt *clnt,
  232. struct rpc_xprt *xprt,
  233. const struct rpc_timeout *timeout)
  234. {
  235. struct rpc_xprt *old;
  236. spin_lock(&clnt->cl_lock);
  237. old = rcu_dereference_protected(clnt->cl_xprt,
  238. lockdep_is_held(&clnt->cl_lock));
  239. if (!xprt_bound(xprt))
  240. clnt->cl_autobind = 1;
  241. clnt->cl_timeout = timeout;
  242. rcu_assign_pointer(clnt->cl_xprt, xprt);
  243. spin_unlock(&clnt->cl_lock);
  244. return old;
  245. }
  246. static void rpc_clnt_set_nodename(struct rpc_clnt *clnt, const char *nodename)
  247. {
  248. clnt->cl_nodelen = strlcpy(clnt->cl_nodename,
  249. nodename, sizeof(clnt->cl_nodename));
  250. }
  251. static int rpc_client_register(struct rpc_clnt *clnt,
  252. rpc_authflavor_t pseudoflavor,
  253. const char *client_name)
  254. {
  255. struct rpc_auth_create_args auth_args = {
  256. .pseudoflavor = pseudoflavor,
  257. .target_name = client_name,
  258. };
  259. struct rpc_auth *auth;
  260. struct net *net = rpc_net_ns(clnt);
  261. struct super_block *pipefs_sb;
  262. int err;
  263. rpc_clnt_debugfs_register(clnt);
  264. pipefs_sb = rpc_get_sb_net(net);
  265. if (pipefs_sb) {
  266. err = rpc_setup_pipedir(pipefs_sb, clnt);
  267. if (err)
  268. goto out;
  269. }
  270. rpc_register_client(clnt);
  271. if (pipefs_sb)
  272. rpc_put_sb_net(net);
  273. auth = rpcauth_create(&auth_args, clnt);
  274. if (IS_ERR(auth)) {
  275. dprintk("RPC: Couldn't create auth handle (flavor %u)\n",
  276. pseudoflavor);
  277. err = PTR_ERR(auth);
  278. goto err_auth;
  279. }
  280. return 0;
  281. err_auth:
  282. pipefs_sb = rpc_get_sb_net(net);
  283. rpc_unregister_client(clnt);
  284. __rpc_clnt_remove_pipedir(clnt);
  285. out:
  286. if (pipefs_sb)
  287. rpc_put_sb_net(net);
  288. rpc_clnt_debugfs_unregister(clnt);
  289. return err;
  290. }
  291. static DEFINE_IDA(rpc_clids);
  292. void rpc_cleanup_clids(void)
  293. {
  294. ida_destroy(&rpc_clids);
  295. }
  296. static int rpc_alloc_clid(struct rpc_clnt *clnt)
  297. {
  298. int clid;
  299. clid = ida_simple_get(&rpc_clids, 0, 0, GFP_KERNEL);
  300. if (clid < 0)
  301. return clid;
  302. clnt->cl_clid = clid;
  303. return 0;
  304. }
  305. static void rpc_free_clid(struct rpc_clnt *clnt)
  306. {
  307. ida_simple_remove(&rpc_clids, clnt->cl_clid);
  308. }
  309. static struct rpc_clnt * rpc_new_client(const struct rpc_create_args *args,
  310. struct rpc_xprt *xprt,
  311. struct rpc_clnt *parent)
  312. {
  313. const struct rpc_program *program = args->program;
  314. const struct rpc_version *version;
  315. struct rpc_clnt *clnt = NULL;
  316. const struct rpc_timeout *timeout;
  317. const char *nodename = args->nodename;
  318. int err;
  319. /* sanity check the name before trying to print it */
  320. dprintk("RPC: creating %s client for %s (xprt %p)\n",
  321. program->name, args->servername, xprt);
  322. err = rpciod_up();
  323. if (err)
  324. goto out_no_rpciod;
  325. err = -EINVAL;
  326. if (args->version >= program->nrvers)
  327. goto out_err;
  328. version = program->version[args->version];
  329. if (version == NULL)
  330. goto out_err;
  331. err = -ENOMEM;
  332. clnt = kzalloc(sizeof(*clnt), GFP_KERNEL);
  333. if (!clnt)
  334. goto out_err;
  335. clnt->cl_parent = parent ? : clnt;
  336. err = rpc_alloc_clid(clnt);
  337. if (err)
  338. goto out_no_clid;
  339. clnt->cl_procinfo = version->procs;
  340. clnt->cl_maxproc = version->nrprocs;
  341. clnt->cl_prog = args->prognumber ? : program->number;
  342. clnt->cl_vers = version->number;
  343. clnt->cl_stats = program->stats;
  344. clnt->cl_metrics = rpc_alloc_iostats(clnt);
  345. rpc_init_pipe_dir_head(&clnt->cl_pipedir_objects);
  346. err = -ENOMEM;
  347. if (clnt->cl_metrics == NULL)
  348. goto out_no_stats;
  349. clnt->cl_program = program;
  350. INIT_LIST_HEAD(&clnt->cl_tasks);
  351. spin_lock_init(&clnt->cl_lock);
  352. timeout = xprt->timeout;
  353. if (args->timeout != NULL) {
  354. memcpy(&clnt->cl_timeout_default, args->timeout,
  355. sizeof(clnt->cl_timeout_default));
  356. timeout = &clnt->cl_timeout_default;
  357. }
  358. rpc_clnt_set_transport(clnt, xprt, timeout);
  359. clnt->cl_rtt = &clnt->cl_rtt_default;
  360. rpc_init_rtt(&clnt->cl_rtt_default, clnt->cl_timeout->to_initval);
  361. atomic_set(&clnt->cl_count, 1);
  362. if (nodename == NULL)
  363. nodename = utsname()->nodename;
  364. /* save the nodename */
  365. rpc_clnt_set_nodename(clnt, nodename);
  366. err = rpc_client_register(clnt, args->authflavor, args->client_name);
  367. if (err)
  368. goto out_no_path;
  369. if (parent)
  370. atomic_inc(&parent->cl_count);
  371. return clnt;
  372. out_no_path:
  373. rpc_free_iostats(clnt->cl_metrics);
  374. out_no_stats:
  375. rpc_free_clid(clnt);
  376. out_no_clid:
  377. kfree(clnt);
  378. out_err:
  379. rpciod_down();
  380. out_no_rpciod:
  381. xprt_put(xprt);
  382. return ERR_PTR(err);
  383. }
  384. static struct rpc_clnt *rpc_create_xprt(struct rpc_create_args *args,
  385. struct rpc_xprt *xprt)
  386. {
  387. struct rpc_clnt *clnt = NULL;
  388. clnt = rpc_new_client(args, xprt, NULL);
  389. if (IS_ERR(clnt))
  390. return clnt;
  391. if (!(args->flags & RPC_CLNT_CREATE_NOPING)) {
  392. int err = rpc_ping(clnt);
  393. if (err != 0) {
  394. rpc_shutdown_client(clnt);
  395. return ERR_PTR(err);
  396. }
  397. }
  398. clnt->cl_softrtry = 1;
  399. if (args->flags & RPC_CLNT_CREATE_HARDRTRY)
  400. clnt->cl_softrtry = 0;
  401. if (args->flags & RPC_CLNT_CREATE_AUTOBIND)
  402. clnt->cl_autobind = 1;
  403. if (args->flags & RPC_CLNT_CREATE_NO_RETRANS_TIMEOUT)
  404. clnt->cl_noretranstimeo = 1;
  405. if (args->flags & RPC_CLNT_CREATE_DISCRTRY)
  406. clnt->cl_discrtry = 1;
  407. if (!(args->flags & RPC_CLNT_CREATE_QUIET))
  408. clnt->cl_chatty = 1;
  409. return clnt;
  410. }
  411. /**
  412. * rpc_create - create an RPC client and transport with one call
  413. * @args: rpc_clnt create argument structure
  414. *
  415. * Creates and initializes an RPC transport and an RPC client.
  416. *
  417. * It can ping the server in order to determine if it is up, and to see if
  418. * it supports this program and version. RPC_CLNT_CREATE_NOPING disables
  419. * this behavior so asynchronous tasks can also use rpc_create.
  420. */
  421. struct rpc_clnt *rpc_create(struct rpc_create_args *args)
  422. {
  423. struct rpc_xprt *xprt;
  424. struct xprt_create xprtargs = {
  425. .net = args->net,
  426. .ident = args->protocol,
  427. .srcaddr = args->saddress,
  428. .dstaddr = args->address,
  429. .addrlen = args->addrsize,
  430. .servername = args->servername,
  431. .bc_xprt = args->bc_xprt,
  432. };
  433. char servername[48];
  434. if (args->bc_xprt) {
  435. WARN_ON(args->protocol != XPRT_TRANSPORT_BC_TCP);
  436. xprt = args->bc_xprt->xpt_bc_xprt;
  437. if (xprt) {
  438. xprt_get(xprt);
  439. return rpc_create_xprt(args, xprt);
  440. }
  441. }
  442. if (args->flags & RPC_CLNT_CREATE_INFINITE_SLOTS)
  443. xprtargs.flags |= XPRT_CREATE_INFINITE_SLOTS;
  444. if (args->flags & RPC_CLNT_CREATE_NO_IDLE_TIMEOUT)
  445. xprtargs.flags |= XPRT_CREATE_NO_IDLE_TIMEOUT;
  446. /*
  447. * If the caller chooses not to specify a hostname, whip
  448. * up a string representation of the passed-in address.
  449. */
  450. if (xprtargs.servername == NULL) {
  451. struct sockaddr_un *sun =
  452. (struct sockaddr_un *)args->address;
  453. struct sockaddr_in *sin =
  454. (struct sockaddr_in *)args->address;
  455. struct sockaddr_in6 *sin6 =
  456. (struct sockaddr_in6 *)args->address;
  457. servername[0] = '\0';
  458. switch (args->address->sa_family) {
  459. case AF_LOCAL:
  460. snprintf(servername, sizeof(servername), "%s",
  461. sun->sun_path);
  462. break;
  463. case AF_INET:
  464. snprintf(servername, sizeof(servername), "%pI4",
  465. &sin->sin_addr.s_addr);
  466. break;
  467. case AF_INET6:
  468. snprintf(servername, sizeof(servername), "%pI6",
  469. &sin6->sin6_addr);
  470. break;
  471. default:
  472. /* caller wants default server name, but
  473. * address family isn't recognized. */
  474. return ERR_PTR(-EINVAL);
  475. }
  476. xprtargs.servername = servername;
  477. }
  478. xprt = xprt_create_transport(&xprtargs);
  479. if (IS_ERR(xprt))
  480. return (struct rpc_clnt *)xprt;
  481. /*
  482. * By default, kernel RPC client connects from a reserved port.
  483. * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
  484. * but it is always enabled for rpciod, which handles the connect
  485. * operation.
  486. */
  487. xprt->resvport = 1;
  488. if (args->flags & RPC_CLNT_CREATE_NONPRIVPORT)
  489. xprt->resvport = 0;
  490. return rpc_create_xprt(args, xprt);
  491. }
  492. EXPORT_SYMBOL_GPL(rpc_create);
  493. /*
  494. * This function clones the RPC client structure. It allows us to share the
  495. * same transport while varying parameters such as the authentication
  496. * flavour.
  497. */
  498. static struct rpc_clnt *__rpc_clone_client(struct rpc_create_args *args,
  499. struct rpc_clnt *clnt)
  500. {
  501. struct rpc_xprt *xprt;
  502. struct rpc_clnt *new;
  503. int err;
  504. err = -ENOMEM;
  505. rcu_read_lock();
  506. xprt = xprt_get(rcu_dereference(clnt->cl_xprt));
  507. rcu_read_unlock();
  508. if (xprt == NULL)
  509. goto out_err;
  510. args->servername = xprt->servername;
  511. args->nodename = clnt->cl_nodename;
  512. new = rpc_new_client(args, xprt, clnt);
  513. if (IS_ERR(new)) {
  514. err = PTR_ERR(new);
  515. goto out_err;
  516. }
  517. /* Turn off autobind on clones */
  518. new->cl_autobind = 0;
  519. new->cl_softrtry = clnt->cl_softrtry;
  520. new->cl_noretranstimeo = clnt->cl_noretranstimeo;
  521. new->cl_discrtry = clnt->cl_discrtry;
  522. new->cl_chatty = clnt->cl_chatty;
  523. return new;
  524. out_err:
  525. dprintk("RPC: %s: returned error %d\n", __func__, err);
  526. return ERR_PTR(err);
  527. }
  528. /**
  529. * rpc_clone_client - Clone an RPC client structure
  530. *
  531. * @clnt: RPC client whose parameters are copied
  532. *
  533. * Returns a fresh RPC client or an ERR_PTR.
  534. */
  535. struct rpc_clnt *rpc_clone_client(struct rpc_clnt *clnt)
  536. {
  537. struct rpc_create_args args = {
  538. .program = clnt->cl_program,
  539. .prognumber = clnt->cl_prog,
  540. .version = clnt->cl_vers,
  541. .authflavor = clnt->cl_auth->au_flavor,
  542. };
  543. return __rpc_clone_client(&args, clnt);
  544. }
  545. EXPORT_SYMBOL_GPL(rpc_clone_client);
  546. /**
  547. * rpc_clone_client_set_auth - Clone an RPC client structure and set its auth
  548. *
  549. * @clnt: RPC client whose parameters are copied
  550. * @flavor: security flavor for new client
  551. *
  552. * Returns a fresh RPC client or an ERR_PTR.
  553. */
  554. struct rpc_clnt *
  555. rpc_clone_client_set_auth(struct rpc_clnt *clnt, rpc_authflavor_t flavor)
  556. {
  557. struct rpc_create_args args = {
  558. .program = clnt->cl_program,
  559. .prognumber = clnt->cl_prog,
  560. .version = clnt->cl_vers,
  561. .authflavor = flavor,
  562. };
  563. return __rpc_clone_client(&args, clnt);
  564. }
  565. EXPORT_SYMBOL_GPL(rpc_clone_client_set_auth);
  566. /**
  567. * rpc_switch_client_transport: switch the RPC transport on the fly
  568. * @clnt: pointer to a struct rpc_clnt
  569. * @args: pointer to the new transport arguments
  570. * @timeout: pointer to the new timeout parameters
  571. *
  572. * This function allows the caller to switch the RPC transport for the
  573. * rpc_clnt structure 'clnt' to allow it to connect to a mirrored NFS
  574. * server, for instance. It assumes that the caller has ensured that
  575. * there are no active RPC tasks by using some form of locking.
  576. *
  577. * Returns zero if "clnt" is now using the new xprt. Otherwise a
  578. * negative errno is returned, and "clnt" continues to use the old
  579. * xprt.
  580. */
  581. int rpc_switch_client_transport(struct rpc_clnt *clnt,
  582. struct xprt_create *args,
  583. const struct rpc_timeout *timeout)
  584. {
  585. const struct rpc_timeout *old_timeo;
  586. rpc_authflavor_t pseudoflavor;
  587. struct rpc_xprt *xprt, *old;
  588. struct rpc_clnt *parent;
  589. int err;
  590. xprt = xprt_create_transport(args);
  591. if (IS_ERR(xprt)) {
  592. dprintk("RPC: failed to create new xprt for clnt %p\n",
  593. clnt);
  594. return PTR_ERR(xprt);
  595. }
  596. pseudoflavor = clnt->cl_auth->au_flavor;
  597. old_timeo = clnt->cl_timeout;
  598. old = rpc_clnt_set_transport(clnt, xprt, timeout);
  599. rpc_unregister_client(clnt);
  600. __rpc_clnt_remove_pipedir(clnt);
  601. rpc_clnt_debugfs_unregister(clnt);
  602. /*
  603. * A new transport was created. "clnt" therefore
  604. * becomes the root of a new cl_parent tree. clnt's
  605. * children, if it has any, still point to the old xprt.
  606. */
  607. parent = clnt->cl_parent;
  608. clnt->cl_parent = clnt;
  609. /*
  610. * The old rpc_auth cache cannot be re-used. GSS
  611. * contexts in particular are between a single
  612. * client and server.
  613. */
  614. err = rpc_client_register(clnt, pseudoflavor, NULL);
  615. if (err)
  616. goto out_revert;
  617. synchronize_rcu();
  618. if (parent != clnt)
  619. rpc_release_client(parent);
  620. xprt_put(old);
  621. dprintk("RPC: replaced xprt for clnt %p\n", clnt);
  622. return 0;
  623. out_revert:
  624. rpc_clnt_set_transport(clnt, old, old_timeo);
  625. clnt->cl_parent = parent;
  626. rpc_client_register(clnt, pseudoflavor, NULL);
  627. xprt_put(xprt);
  628. dprintk("RPC: failed to switch xprt for clnt %p\n", clnt);
  629. return err;
  630. }
  631. EXPORT_SYMBOL_GPL(rpc_switch_client_transport);
  632. /*
  633. * Kill all tasks for the given client.
  634. * XXX: kill their descendants as well?
  635. */
  636. void rpc_killall_tasks(struct rpc_clnt *clnt)
  637. {
  638. struct rpc_task *rovr;
  639. if (list_empty(&clnt->cl_tasks))
  640. return;
  641. dprintk("RPC: killing all tasks for client %p\n", clnt);
  642. /*
  643. * Spin lock all_tasks to prevent changes...
  644. */
  645. spin_lock(&clnt->cl_lock);
  646. list_for_each_entry(rovr, &clnt->cl_tasks, tk_task) {
  647. if (!RPC_IS_ACTIVATED(rovr))
  648. continue;
  649. if (!(rovr->tk_flags & RPC_TASK_KILLED)) {
  650. rovr->tk_flags |= RPC_TASK_KILLED;
  651. rpc_exit(rovr, -EIO);
  652. if (RPC_IS_QUEUED(rovr))
  653. rpc_wake_up_queued_task(rovr->tk_waitqueue,
  654. rovr);
  655. }
  656. }
  657. spin_unlock(&clnt->cl_lock);
  658. }
  659. EXPORT_SYMBOL_GPL(rpc_killall_tasks);
  660. /*
  661. * Properly shut down an RPC client, terminating all outstanding
  662. * requests.
  663. */
  664. void rpc_shutdown_client(struct rpc_clnt *clnt)
  665. {
  666. might_sleep();
  667. dprintk_rcu("RPC: shutting down %s client for %s\n",
  668. clnt->cl_program->name,
  669. rcu_dereference(clnt->cl_xprt)->servername);
  670. while (!list_empty(&clnt->cl_tasks)) {
  671. rpc_killall_tasks(clnt);
  672. wait_event_timeout(destroy_wait,
  673. list_empty(&clnt->cl_tasks), 1*HZ);
  674. }
  675. rpc_release_client(clnt);
  676. }
  677. EXPORT_SYMBOL_GPL(rpc_shutdown_client);
  678. /*
  679. * Free an RPC client
  680. */
  681. static struct rpc_clnt *
  682. rpc_free_client(struct rpc_clnt *clnt)
  683. {
  684. struct rpc_clnt *parent = NULL;
  685. dprintk_rcu("RPC: destroying %s client for %s\n",
  686. clnt->cl_program->name,
  687. rcu_dereference(clnt->cl_xprt)->servername);
  688. if (clnt->cl_parent != clnt)
  689. parent = clnt->cl_parent;
  690. rpc_clnt_debugfs_unregister(clnt);
  691. rpc_clnt_remove_pipedir(clnt);
  692. rpc_unregister_client(clnt);
  693. rpc_free_iostats(clnt->cl_metrics);
  694. clnt->cl_metrics = NULL;
  695. xprt_put(rcu_dereference_raw(clnt->cl_xprt));
  696. rpciod_down();
  697. rpc_free_clid(clnt);
  698. kfree(clnt);
  699. return parent;
  700. }
  701. /*
  702. * Free an RPC client
  703. */
  704. static struct rpc_clnt *
  705. rpc_free_auth(struct rpc_clnt *clnt)
  706. {
  707. if (clnt->cl_auth == NULL)
  708. return rpc_free_client(clnt);
  709. /*
  710. * Note: RPCSEC_GSS may need to send NULL RPC calls in order to
  711. * release remaining GSS contexts. This mechanism ensures
  712. * that it can do so safely.
  713. */
  714. atomic_inc(&clnt->cl_count);
  715. rpcauth_release(clnt->cl_auth);
  716. clnt->cl_auth = NULL;
  717. if (atomic_dec_and_test(&clnt->cl_count))
  718. return rpc_free_client(clnt);
  719. return NULL;
  720. }
  721. /*
  722. * Release reference to the RPC client
  723. */
  724. void
  725. rpc_release_client(struct rpc_clnt *clnt)
  726. {
  727. dprintk("RPC: rpc_release_client(%p)\n", clnt);
  728. do {
  729. if (list_empty(&clnt->cl_tasks))
  730. wake_up(&destroy_wait);
  731. if (!atomic_dec_and_test(&clnt->cl_count))
  732. break;
  733. clnt = rpc_free_auth(clnt);
  734. } while (clnt != NULL);
  735. }
  736. EXPORT_SYMBOL_GPL(rpc_release_client);
  737. /**
  738. * rpc_bind_new_program - bind a new RPC program to an existing client
  739. * @old: old rpc_client
  740. * @program: rpc program to set
  741. * @vers: rpc program version
  742. *
  743. * Clones the rpc client and sets up a new RPC program. This is mainly
  744. * of use for enabling different RPC programs to share the same transport.
  745. * The Sun NFSv2/v3 ACL protocol can do this.
  746. */
  747. struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old,
  748. const struct rpc_program *program,
  749. u32 vers)
  750. {
  751. struct rpc_create_args args = {
  752. .program = program,
  753. .prognumber = program->number,
  754. .version = vers,
  755. .authflavor = old->cl_auth->au_flavor,
  756. };
  757. struct rpc_clnt *clnt;
  758. int err;
  759. clnt = __rpc_clone_client(&args, old);
  760. if (IS_ERR(clnt))
  761. goto out;
  762. err = rpc_ping(clnt);
  763. if (err != 0) {
  764. rpc_shutdown_client(clnt);
  765. clnt = ERR_PTR(err);
  766. }
  767. out:
  768. return clnt;
  769. }
  770. EXPORT_SYMBOL_GPL(rpc_bind_new_program);
  771. void rpc_task_release_client(struct rpc_task *task)
  772. {
  773. struct rpc_clnt *clnt = task->tk_client;
  774. if (clnt != NULL) {
  775. /* Remove from client task list */
  776. spin_lock(&clnt->cl_lock);
  777. list_del(&task->tk_task);
  778. spin_unlock(&clnt->cl_lock);
  779. task->tk_client = NULL;
  780. rpc_release_client(clnt);
  781. }
  782. }
  783. static
  784. void rpc_task_set_client(struct rpc_task *task, struct rpc_clnt *clnt)
  785. {
  786. if (clnt != NULL) {
  787. rpc_task_release_client(task);
  788. task->tk_client = clnt;
  789. atomic_inc(&clnt->cl_count);
  790. if (clnt->cl_softrtry)
  791. task->tk_flags |= RPC_TASK_SOFT;
  792. if (clnt->cl_noretranstimeo)
  793. task->tk_flags |= RPC_TASK_NO_RETRANS_TIMEOUT;
  794. if (atomic_read(&clnt->cl_swapper))
  795. task->tk_flags |= RPC_TASK_SWAPPER;
  796. /* Add to the client's list of all tasks */
  797. spin_lock(&clnt->cl_lock);
  798. list_add_tail(&task->tk_task, &clnt->cl_tasks);
  799. spin_unlock(&clnt->cl_lock);
  800. }
  801. }
  802. void rpc_task_reset_client(struct rpc_task *task, struct rpc_clnt *clnt)
  803. {
  804. rpc_task_release_client(task);
  805. rpc_task_set_client(task, clnt);
  806. }
  807. EXPORT_SYMBOL_GPL(rpc_task_reset_client);
  808. static void
  809. rpc_task_set_rpc_message(struct rpc_task *task, const struct rpc_message *msg)
  810. {
  811. if (msg != NULL) {
  812. task->tk_msg.rpc_proc = msg->rpc_proc;
  813. task->tk_msg.rpc_argp = msg->rpc_argp;
  814. task->tk_msg.rpc_resp = msg->rpc_resp;
  815. if (msg->rpc_cred != NULL)
  816. task->tk_msg.rpc_cred = get_rpccred(msg->rpc_cred);
  817. }
  818. }
  819. /*
  820. * Default callback for async RPC calls
  821. */
  822. static void
  823. rpc_default_callback(struct rpc_task *task, void *data)
  824. {
  825. }
  826. static const struct rpc_call_ops rpc_default_ops = {
  827. .rpc_call_done = rpc_default_callback,
  828. };
  829. /**
  830. * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
  831. * @task_setup_data: pointer to task initialisation data
  832. */
  833. struct rpc_task *rpc_run_task(const struct rpc_task_setup *task_setup_data)
  834. {
  835. struct rpc_task *task;
  836. task = rpc_new_task(task_setup_data);
  837. if (IS_ERR(task))
  838. goto out;
  839. rpc_task_set_client(task, task_setup_data->rpc_client);
  840. rpc_task_set_rpc_message(task, task_setup_data->rpc_message);
  841. if (task->tk_action == NULL)
  842. rpc_call_start(task);
  843. atomic_inc(&task->tk_count);
  844. rpc_execute(task);
  845. out:
  846. return task;
  847. }
  848. EXPORT_SYMBOL_GPL(rpc_run_task);
  849. /**
  850. * rpc_call_sync - Perform a synchronous RPC call
  851. * @clnt: pointer to RPC client
  852. * @msg: RPC call parameters
  853. * @flags: RPC call flags
  854. */
  855. int rpc_call_sync(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags)
  856. {
  857. struct rpc_task *task;
  858. struct rpc_task_setup task_setup_data = {
  859. .rpc_client = clnt,
  860. .rpc_message = msg,
  861. .callback_ops = &rpc_default_ops,
  862. .flags = flags,
  863. };
  864. int status;
  865. WARN_ON_ONCE(flags & RPC_TASK_ASYNC);
  866. if (flags & RPC_TASK_ASYNC) {
  867. rpc_release_calldata(task_setup_data.callback_ops,
  868. task_setup_data.callback_data);
  869. return -EINVAL;
  870. }
  871. task = rpc_run_task(&task_setup_data);
  872. if (IS_ERR(task))
  873. return PTR_ERR(task);
  874. status = task->tk_status;
  875. rpc_put_task(task);
  876. return status;
  877. }
  878. EXPORT_SYMBOL_GPL(rpc_call_sync);
  879. /**
  880. * rpc_call_async - Perform an asynchronous RPC call
  881. * @clnt: pointer to RPC client
  882. * @msg: RPC call parameters
  883. * @flags: RPC call flags
  884. * @tk_ops: RPC call ops
  885. * @data: user call data
  886. */
  887. int
  888. rpc_call_async(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags,
  889. const struct rpc_call_ops *tk_ops, void *data)
  890. {
  891. struct rpc_task *task;
  892. struct rpc_task_setup task_setup_data = {
  893. .rpc_client = clnt,
  894. .rpc_message = msg,
  895. .callback_ops = tk_ops,
  896. .callback_data = data,
  897. .flags = flags|RPC_TASK_ASYNC,
  898. };
  899. task = rpc_run_task(&task_setup_data);
  900. if (IS_ERR(task))
  901. return PTR_ERR(task);
  902. rpc_put_task(task);
  903. return 0;
  904. }
  905. EXPORT_SYMBOL_GPL(rpc_call_async);
  906. #if defined(CONFIG_SUNRPC_BACKCHANNEL)
  907. /**
  908. * rpc_run_bc_task - Allocate a new RPC task for backchannel use, then run
  909. * rpc_execute against it
  910. * @req: RPC request
  911. */
  912. struct rpc_task *rpc_run_bc_task(struct rpc_rqst *req)
  913. {
  914. struct rpc_task *task;
  915. struct xdr_buf *xbufp = &req->rq_snd_buf;
  916. struct rpc_task_setup task_setup_data = {
  917. .callback_ops = &rpc_default_ops,
  918. .flags = RPC_TASK_SOFTCONN,
  919. };
  920. dprintk("RPC: rpc_run_bc_task req= %p\n", req);
  921. /*
  922. * Create an rpc_task to send the data
  923. */
  924. task = rpc_new_task(&task_setup_data);
  925. if (IS_ERR(task)) {
  926. xprt_free_bc_request(req);
  927. goto out;
  928. }
  929. task->tk_rqstp = req;
  930. /*
  931. * Set up the xdr_buf length.
  932. * This also indicates that the buffer is XDR encoded already.
  933. */
  934. xbufp->len = xbufp->head[0].iov_len + xbufp->page_len +
  935. xbufp->tail[0].iov_len;
  936. task->tk_action = call_bc_transmit;
  937. atomic_inc(&task->tk_count);
  938. WARN_ON_ONCE(atomic_read(&task->tk_count) != 2);
  939. rpc_execute(task);
  940. out:
  941. dprintk("RPC: rpc_run_bc_task: task= %p\n", task);
  942. return task;
  943. }
  944. #endif /* CONFIG_SUNRPC_BACKCHANNEL */
  945. void
  946. rpc_call_start(struct rpc_task *task)
  947. {
  948. task->tk_action = call_start;
  949. }
  950. EXPORT_SYMBOL_GPL(rpc_call_start);
  951. /**
  952. * rpc_peeraddr - extract remote peer address from clnt's xprt
  953. * @clnt: RPC client structure
  954. * @buf: target buffer
  955. * @bufsize: length of target buffer
  956. *
  957. * Returns the number of bytes that are actually in the stored address.
  958. */
  959. size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize)
  960. {
  961. size_t bytes;
  962. struct rpc_xprt *xprt;
  963. rcu_read_lock();
  964. xprt = rcu_dereference(clnt->cl_xprt);
  965. bytes = xprt->addrlen;
  966. if (bytes > bufsize)
  967. bytes = bufsize;
  968. memcpy(buf, &xprt->addr, bytes);
  969. rcu_read_unlock();
  970. return bytes;
  971. }
  972. EXPORT_SYMBOL_GPL(rpc_peeraddr);
  973. /**
  974. * rpc_peeraddr2str - return remote peer address in printable format
  975. * @clnt: RPC client structure
  976. * @format: address format
  977. *
  978. * NB: the lifetime of the memory referenced by the returned pointer is
  979. * the same as the rpc_xprt itself. As long as the caller uses this
  980. * pointer, it must hold the RCU read lock.
  981. */
  982. const char *rpc_peeraddr2str(struct rpc_clnt *clnt,
  983. enum rpc_display_format_t format)
  984. {
  985. struct rpc_xprt *xprt;
  986. xprt = rcu_dereference(clnt->cl_xprt);
  987. if (xprt->address_strings[format] != NULL)
  988. return xprt->address_strings[format];
  989. else
  990. return "unprintable";
  991. }
  992. EXPORT_SYMBOL_GPL(rpc_peeraddr2str);
  993. static const struct sockaddr_in rpc_inaddr_loopback = {
  994. .sin_family = AF_INET,
  995. .sin_addr.s_addr = htonl(INADDR_ANY),
  996. };
  997. static const struct sockaddr_in6 rpc_in6addr_loopback = {
  998. .sin6_family = AF_INET6,
  999. .sin6_addr = IN6ADDR_ANY_INIT,
  1000. };
  1001. /*
  1002. * Try a getsockname() on a connected datagram socket. Using a
  1003. * connected datagram socket prevents leaving a socket in TIME_WAIT.
  1004. * This conserves the ephemeral port number space.
  1005. *
  1006. * Returns zero and fills in "buf" if successful; otherwise, a
  1007. * negative errno is returned.
  1008. */
  1009. static int rpc_sockname(struct net *net, struct sockaddr *sap, size_t salen,
  1010. struct sockaddr *buf, int buflen)
  1011. {
  1012. struct socket *sock;
  1013. int err;
  1014. err = __sock_create(net, sap->sa_family,
  1015. SOCK_DGRAM, IPPROTO_UDP, &sock, 1);
  1016. if (err < 0) {
  1017. dprintk("RPC: can't create UDP socket (%d)\n", err);
  1018. goto out;
  1019. }
  1020. switch (sap->sa_family) {
  1021. case AF_INET:
  1022. err = kernel_bind(sock,
  1023. (struct sockaddr *)&rpc_inaddr_loopback,
  1024. sizeof(rpc_inaddr_loopback));
  1025. break;
  1026. case AF_INET6:
  1027. err = kernel_bind(sock,
  1028. (struct sockaddr *)&rpc_in6addr_loopback,
  1029. sizeof(rpc_in6addr_loopback));
  1030. break;
  1031. default:
  1032. err = -EAFNOSUPPORT;
  1033. goto out;
  1034. }
  1035. if (err < 0) {
  1036. dprintk("RPC: can't bind UDP socket (%d)\n", err);
  1037. goto out_release;
  1038. }
  1039. err = kernel_connect(sock, sap, salen, 0);
  1040. if (err < 0) {
  1041. dprintk("RPC: can't connect UDP socket (%d)\n", err);
  1042. goto out_release;
  1043. }
  1044. err = kernel_getsockname(sock, buf, &buflen);
  1045. if (err < 0) {
  1046. dprintk("RPC: getsockname failed (%d)\n", err);
  1047. goto out_release;
  1048. }
  1049. err = 0;
  1050. if (buf->sa_family == AF_INET6) {
  1051. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)buf;
  1052. sin6->sin6_scope_id = 0;
  1053. }
  1054. dprintk("RPC: %s succeeded\n", __func__);
  1055. out_release:
  1056. sock_release(sock);
  1057. out:
  1058. return err;
  1059. }
  1060. /*
  1061. * Scraping a connected socket failed, so we don't have a useable
  1062. * local address. Fallback: generate an address that will prevent
  1063. * the server from calling us back.
  1064. *
  1065. * Returns zero and fills in "buf" if successful; otherwise, a
  1066. * negative errno is returned.
  1067. */
  1068. static int rpc_anyaddr(int family, struct sockaddr *buf, size_t buflen)
  1069. {
  1070. switch (family) {
  1071. case AF_INET:
  1072. if (buflen < sizeof(rpc_inaddr_loopback))
  1073. return -EINVAL;
  1074. memcpy(buf, &rpc_inaddr_loopback,
  1075. sizeof(rpc_inaddr_loopback));
  1076. break;
  1077. case AF_INET6:
  1078. if (buflen < sizeof(rpc_in6addr_loopback))
  1079. return -EINVAL;
  1080. memcpy(buf, &rpc_in6addr_loopback,
  1081. sizeof(rpc_in6addr_loopback));
  1082. default:
  1083. dprintk("RPC: %s: address family not supported\n",
  1084. __func__);
  1085. return -EAFNOSUPPORT;
  1086. }
  1087. dprintk("RPC: %s: succeeded\n", __func__);
  1088. return 0;
  1089. }
  1090. /**
  1091. * rpc_localaddr - discover local endpoint address for an RPC client
  1092. * @clnt: RPC client structure
  1093. * @buf: target buffer
  1094. * @buflen: size of target buffer, in bytes
  1095. *
  1096. * Returns zero and fills in "buf" and "buflen" if successful;
  1097. * otherwise, a negative errno is returned.
  1098. *
  1099. * This works even if the underlying transport is not currently connected,
  1100. * or if the upper layer never previously provided a source address.
  1101. *
  1102. * The result of this function call is transient: multiple calls in
  1103. * succession may give different results, depending on how local
  1104. * networking configuration changes over time.
  1105. */
  1106. int rpc_localaddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t buflen)
  1107. {
  1108. struct sockaddr_storage address;
  1109. struct sockaddr *sap = (struct sockaddr *)&address;
  1110. struct rpc_xprt *xprt;
  1111. struct net *net;
  1112. size_t salen;
  1113. int err;
  1114. rcu_read_lock();
  1115. xprt = rcu_dereference(clnt->cl_xprt);
  1116. salen = xprt->addrlen;
  1117. memcpy(sap, &xprt->addr, salen);
  1118. net = get_net(xprt->xprt_net);
  1119. rcu_read_unlock();
  1120. rpc_set_port(sap, 0);
  1121. err = rpc_sockname(net, sap, salen, buf, buflen);
  1122. put_net(net);
  1123. if (err != 0)
  1124. /* Couldn't discover local address, return ANYADDR */
  1125. return rpc_anyaddr(sap->sa_family, buf, buflen);
  1126. return 0;
  1127. }
  1128. EXPORT_SYMBOL_GPL(rpc_localaddr);
  1129. void
  1130. rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
  1131. {
  1132. struct rpc_xprt *xprt;
  1133. rcu_read_lock();
  1134. xprt = rcu_dereference(clnt->cl_xprt);
  1135. if (xprt->ops->set_buffer_size)
  1136. xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
  1137. rcu_read_unlock();
  1138. }
  1139. EXPORT_SYMBOL_GPL(rpc_setbufsize);
  1140. /**
  1141. * rpc_protocol - Get transport protocol number for an RPC client
  1142. * @clnt: RPC client to query
  1143. *
  1144. */
  1145. int rpc_protocol(struct rpc_clnt *clnt)
  1146. {
  1147. int protocol;
  1148. rcu_read_lock();
  1149. protocol = rcu_dereference(clnt->cl_xprt)->prot;
  1150. rcu_read_unlock();
  1151. return protocol;
  1152. }
  1153. EXPORT_SYMBOL_GPL(rpc_protocol);
  1154. /**
  1155. * rpc_net_ns - Get the network namespace for this RPC client
  1156. * @clnt: RPC client to query
  1157. *
  1158. */
  1159. struct net *rpc_net_ns(struct rpc_clnt *clnt)
  1160. {
  1161. struct net *ret;
  1162. rcu_read_lock();
  1163. ret = rcu_dereference(clnt->cl_xprt)->xprt_net;
  1164. rcu_read_unlock();
  1165. return ret;
  1166. }
  1167. EXPORT_SYMBOL_GPL(rpc_net_ns);
  1168. /**
  1169. * rpc_max_payload - Get maximum payload size for a transport, in bytes
  1170. * @clnt: RPC client to query
  1171. *
  1172. * For stream transports, this is one RPC record fragment (see RFC
  1173. * 1831), as we don't support multi-record requests yet. For datagram
  1174. * transports, this is the size of an IP packet minus the IP, UDP, and
  1175. * RPC header sizes.
  1176. */
  1177. size_t rpc_max_payload(struct rpc_clnt *clnt)
  1178. {
  1179. size_t ret;
  1180. rcu_read_lock();
  1181. ret = rcu_dereference(clnt->cl_xprt)->max_payload;
  1182. rcu_read_unlock();
  1183. return ret;
  1184. }
  1185. EXPORT_SYMBOL_GPL(rpc_max_payload);
  1186. /**
  1187. * rpc_get_timeout - Get timeout for transport in units of HZ
  1188. * @clnt: RPC client to query
  1189. */
  1190. unsigned long rpc_get_timeout(struct rpc_clnt *clnt)
  1191. {
  1192. unsigned long ret;
  1193. rcu_read_lock();
  1194. ret = rcu_dereference(clnt->cl_xprt)->timeout->to_initval;
  1195. rcu_read_unlock();
  1196. return ret;
  1197. }
  1198. EXPORT_SYMBOL_GPL(rpc_get_timeout);
  1199. /**
  1200. * rpc_force_rebind - force transport to check that remote port is unchanged
  1201. * @clnt: client to rebind
  1202. *
  1203. */
  1204. void rpc_force_rebind(struct rpc_clnt *clnt)
  1205. {
  1206. if (clnt->cl_autobind) {
  1207. rcu_read_lock();
  1208. xprt_clear_bound(rcu_dereference(clnt->cl_xprt));
  1209. rcu_read_unlock();
  1210. }
  1211. }
  1212. EXPORT_SYMBOL_GPL(rpc_force_rebind);
  1213. /*
  1214. * Restart an (async) RPC call from the call_prepare state.
  1215. * Usually called from within the exit handler.
  1216. */
  1217. int
  1218. rpc_restart_call_prepare(struct rpc_task *task)
  1219. {
  1220. if (RPC_ASSASSINATED(task))
  1221. return 0;
  1222. task->tk_action = call_start;
  1223. task->tk_status = 0;
  1224. if (task->tk_ops->rpc_call_prepare != NULL)
  1225. task->tk_action = rpc_prepare_task;
  1226. return 1;
  1227. }
  1228. EXPORT_SYMBOL_GPL(rpc_restart_call_prepare);
  1229. /*
  1230. * Restart an (async) RPC call. Usually called from within the
  1231. * exit handler.
  1232. */
  1233. int
  1234. rpc_restart_call(struct rpc_task *task)
  1235. {
  1236. if (RPC_ASSASSINATED(task))
  1237. return 0;
  1238. task->tk_action = call_start;
  1239. task->tk_status = 0;
  1240. return 1;
  1241. }
  1242. EXPORT_SYMBOL_GPL(rpc_restart_call);
  1243. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  1244. const char
  1245. *rpc_proc_name(const struct rpc_task *task)
  1246. {
  1247. const struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
  1248. if (proc) {
  1249. if (proc->p_name)
  1250. return proc->p_name;
  1251. else
  1252. return "NULL";
  1253. } else
  1254. return "no proc";
  1255. }
  1256. #endif
  1257. /*
  1258. * 0. Initial state
  1259. *
  1260. * Other FSM states can be visited zero or more times, but
  1261. * this state is visited exactly once for each RPC.
  1262. */
  1263. static void
  1264. call_start(struct rpc_task *task)
  1265. {
  1266. struct rpc_clnt *clnt = task->tk_client;
  1267. dprintk("RPC: %5u call_start %s%d proc %s (%s)\n", task->tk_pid,
  1268. clnt->cl_program->name, clnt->cl_vers,
  1269. rpc_proc_name(task),
  1270. (RPC_IS_ASYNC(task) ? "async" : "sync"));
  1271. /* Increment call count */
  1272. task->tk_msg.rpc_proc->p_count++;
  1273. clnt->cl_stats->rpccnt++;
  1274. task->tk_action = call_reserve;
  1275. }
  1276. /*
  1277. * 1. Reserve an RPC call slot
  1278. */
  1279. static void
  1280. call_reserve(struct rpc_task *task)
  1281. {
  1282. dprint_status(task);
  1283. task->tk_status = 0;
  1284. task->tk_action = call_reserveresult;
  1285. xprt_reserve(task);
  1286. }
  1287. static void call_retry_reserve(struct rpc_task *task);
  1288. /*
  1289. * 1b. Grok the result of xprt_reserve()
  1290. */
  1291. static void
  1292. call_reserveresult(struct rpc_task *task)
  1293. {
  1294. int status = task->tk_status;
  1295. dprint_status(task);
  1296. /*
  1297. * After a call to xprt_reserve(), we must have either
  1298. * a request slot or else an error status.
  1299. */
  1300. task->tk_status = 0;
  1301. if (status >= 0) {
  1302. if (task->tk_rqstp) {
  1303. task->tk_action = call_refresh;
  1304. return;
  1305. }
  1306. printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n",
  1307. __func__, status);
  1308. rpc_exit(task, -EIO);
  1309. return;
  1310. }
  1311. /*
  1312. * Even though there was an error, we may have acquired
  1313. * a request slot somehow. Make sure not to leak it.
  1314. */
  1315. if (task->tk_rqstp) {
  1316. printk(KERN_ERR "%s: status=%d, request allocated anyway\n",
  1317. __func__, status);
  1318. xprt_release(task);
  1319. }
  1320. switch (status) {
  1321. case -ENOMEM:
  1322. rpc_delay(task, HZ >> 2);
  1323. case -EAGAIN: /* woken up; retry */
  1324. task->tk_action = call_retry_reserve;
  1325. return;
  1326. case -EIO: /* probably a shutdown */
  1327. break;
  1328. default:
  1329. printk(KERN_ERR "%s: unrecognized error %d, exiting\n",
  1330. __func__, status);
  1331. break;
  1332. }
  1333. rpc_exit(task, status);
  1334. }
  1335. /*
  1336. * 1c. Retry reserving an RPC call slot
  1337. */
  1338. static void
  1339. call_retry_reserve(struct rpc_task *task)
  1340. {
  1341. dprint_status(task);
  1342. task->tk_status = 0;
  1343. task->tk_action = call_reserveresult;
  1344. xprt_retry_reserve(task);
  1345. }
  1346. /*
  1347. * 2. Bind and/or refresh the credentials
  1348. */
  1349. static void
  1350. call_refresh(struct rpc_task *task)
  1351. {
  1352. dprint_status(task);
  1353. task->tk_action = call_refreshresult;
  1354. task->tk_status = 0;
  1355. task->tk_client->cl_stats->rpcauthrefresh++;
  1356. rpcauth_refreshcred(task);
  1357. }
  1358. /*
  1359. * 2a. Process the results of a credential refresh
  1360. */
  1361. static void
  1362. call_refreshresult(struct rpc_task *task)
  1363. {
  1364. int status = task->tk_status;
  1365. dprint_status(task);
  1366. task->tk_status = 0;
  1367. task->tk_action = call_refresh;
  1368. switch (status) {
  1369. case 0:
  1370. if (rpcauth_uptodatecred(task)) {
  1371. task->tk_action = call_allocate;
  1372. return;
  1373. }
  1374. /* Use rate-limiting and a max number of retries if refresh
  1375. * had status 0 but failed to update the cred.
  1376. */
  1377. case -ETIMEDOUT:
  1378. rpc_delay(task, 3*HZ);
  1379. case -EAGAIN:
  1380. status = -EACCES;
  1381. case -EKEYEXPIRED:
  1382. if (!task->tk_cred_retry)
  1383. break;
  1384. task->tk_cred_retry--;
  1385. dprintk("RPC: %5u %s: retry refresh creds\n",
  1386. task->tk_pid, __func__);
  1387. return;
  1388. }
  1389. dprintk("RPC: %5u %s: refresh creds failed with error %d\n",
  1390. task->tk_pid, __func__, status);
  1391. rpc_exit(task, status);
  1392. }
  1393. /*
  1394. * 2b. Allocate the buffer. For details, see sched.c:rpc_malloc.
  1395. * (Note: buffer memory is freed in xprt_release).
  1396. */
  1397. static void
  1398. call_allocate(struct rpc_task *task)
  1399. {
  1400. unsigned int slack = task->tk_rqstp->rq_cred->cr_auth->au_cslack;
  1401. struct rpc_rqst *req = task->tk_rqstp;
  1402. struct rpc_xprt *xprt = req->rq_xprt;
  1403. struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
  1404. dprint_status(task);
  1405. task->tk_status = 0;
  1406. task->tk_action = call_bind;
  1407. if (req->rq_buffer)
  1408. return;
  1409. if (proc->p_proc != 0) {
  1410. BUG_ON(proc->p_arglen == 0);
  1411. if (proc->p_decode != NULL)
  1412. BUG_ON(proc->p_replen == 0);
  1413. }
  1414. /*
  1415. * Calculate the size (in quads) of the RPC call
  1416. * and reply headers, and convert both values
  1417. * to byte sizes.
  1418. */
  1419. req->rq_callsize = RPC_CALLHDRSIZE + (slack << 1) + proc->p_arglen;
  1420. req->rq_callsize <<= 2;
  1421. req->rq_rcvsize = RPC_REPHDRSIZE + slack + proc->p_replen;
  1422. req->rq_rcvsize <<= 2;
  1423. req->rq_buffer = xprt->ops->buf_alloc(task,
  1424. req->rq_callsize + req->rq_rcvsize);
  1425. if (req->rq_buffer != NULL)
  1426. return;
  1427. xprt_inject_disconnect(xprt);
  1428. dprintk("RPC: %5u rpc_buffer allocation failed\n", task->tk_pid);
  1429. if (RPC_IS_ASYNC(task) || !fatal_signal_pending(current)) {
  1430. task->tk_action = call_allocate;
  1431. rpc_delay(task, HZ>>4);
  1432. return;
  1433. }
  1434. rpc_exit(task, -ERESTARTSYS);
  1435. }
  1436. static inline int
  1437. rpc_task_need_encode(struct rpc_task *task)
  1438. {
  1439. return task->tk_rqstp->rq_snd_buf.len == 0;
  1440. }
  1441. static inline void
  1442. rpc_task_force_reencode(struct rpc_task *task)
  1443. {
  1444. task->tk_rqstp->rq_snd_buf.len = 0;
  1445. task->tk_rqstp->rq_bytes_sent = 0;
  1446. }
  1447. static inline void
  1448. rpc_xdr_buf_init(struct xdr_buf *buf, void *start, size_t len)
  1449. {
  1450. buf->head[0].iov_base = start;
  1451. buf->head[0].iov_len = len;
  1452. buf->tail[0].iov_len = 0;
  1453. buf->page_len = 0;
  1454. buf->flags = 0;
  1455. buf->len = 0;
  1456. buf->buflen = len;
  1457. }
  1458. /*
  1459. * 3. Encode arguments of an RPC call
  1460. */
  1461. static void
  1462. rpc_xdr_encode(struct rpc_task *task)
  1463. {
  1464. struct rpc_rqst *req = task->tk_rqstp;
  1465. kxdreproc_t encode;
  1466. __be32 *p;
  1467. dprint_status(task);
  1468. rpc_xdr_buf_init(&req->rq_snd_buf,
  1469. req->rq_buffer,
  1470. req->rq_callsize);
  1471. rpc_xdr_buf_init(&req->rq_rcv_buf,
  1472. (char *)req->rq_buffer + req->rq_callsize,
  1473. req->rq_rcvsize);
  1474. p = rpc_encode_header(task);
  1475. if (p == NULL) {
  1476. printk(KERN_INFO "RPC: couldn't encode RPC header, exit EIO\n");
  1477. rpc_exit(task, -EIO);
  1478. return;
  1479. }
  1480. encode = task->tk_msg.rpc_proc->p_encode;
  1481. if (encode == NULL)
  1482. return;
  1483. task->tk_status = rpcauth_wrap_req(task, encode, req, p,
  1484. task->tk_msg.rpc_argp);
  1485. }
  1486. /*
  1487. * 4. Get the server port number if not yet set
  1488. */
  1489. static void
  1490. call_bind(struct rpc_task *task)
  1491. {
  1492. struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
  1493. dprint_status(task);
  1494. task->tk_action = call_connect;
  1495. if (!xprt_bound(xprt)) {
  1496. task->tk_action = call_bind_status;
  1497. task->tk_timeout = xprt->bind_timeout;
  1498. xprt->ops->rpcbind(task);
  1499. }
  1500. }
  1501. /*
  1502. * 4a. Sort out bind result
  1503. */
  1504. static void
  1505. call_bind_status(struct rpc_task *task)
  1506. {
  1507. int status = -EIO;
  1508. if (task->tk_status >= 0) {
  1509. dprint_status(task);
  1510. task->tk_status = 0;
  1511. task->tk_action = call_connect;
  1512. return;
  1513. }
  1514. trace_rpc_bind_status(task);
  1515. switch (task->tk_status) {
  1516. case -ENOMEM:
  1517. dprintk("RPC: %5u rpcbind out of memory\n", task->tk_pid);
  1518. rpc_delay(task, HZ >> 2);
  1519. goto retry_timeout;
  1520. case -EACCES:
  1521. dprintk("RPC: %5u remote rpcbind: RPC program/version "
  1522. "unavailable\n", task->tk_pid);
  1523. /* fail immediately if this is an RPC ping */
  1524. if (task->tk_msg.rpc_proc->p_proc == 0) {
  1525. status = -EOPNOTSUPP;
  1526. break;
  1527. }
  1528. if (task->tk_rebind_retry == 0)
  1529. break;
  1530. task->tk_rebind_retry--;
  1531. rpc_delay(task, 3*HZ);
  1532. goto retry_timeout;
  1533. case -ETIMEDOUT:
  1534. dprintk("RPC: %5u rpcbind request timed out\n",
  1535. task->tk_pid);
  1536. goto retry_timeout;
  1537. case -EPFNOSUPPORT:
  1538. /* server doesn't support any rpcbind version we know of */
  1539. dprintk("RPC: %5u unrecognized remote rpcbind service\n",
  1540. task->tk_pid);
  1541. break;
  1542. case -EPROTONOSUPPORT:
  1543. dprintk("RPC: %5u remote rpcbind version unavailable, retrying\n",
  1544. task->tk_pid);
  1545. goto retry_timeout;
  1546. case -ECONNREFUSED: /* connection problems */
  1547. case -ECONNRESET:
  1548. case -ECONNABORTED:
  1549. case -ENOTCONN:
  1550. case -EHOSTDOWN:
  1551. case -EHOSTUNREACH:
  1552. case -ENETUNREACH:
  1553. case -ENOBUFS:
  1554. case -EPIPE:
  1555. dprintk("RPC: %5u remote rpcbind unreachable: %d\n",
  1556. task->tk_pid, task->tk_status);
  1557. if (!RPC_IS_SOFTCONN(task)) {
  1558. rpc_delay(task, 5*HZ);
  1559. goto retry_timeout;
  1560. }
  1561. status = task->tk_status;
  1562. break;
  1563. default:
  1564. dprintk("RPC: %5u unrecognized rpcbind error (%d)\n",
  1565. task->tk_pid, -task->tk_status);
  1566. }
  1567. rpc_exit(task, status);
  1568. return;
  1569. retry_timeout:
  1570. task->tk_status = 0;
  1571. task->tk_action = call_timeout;
  1572. }
  1573. /*
  1574. * 4b. Connect to the RPC server
  1575. */
  1576. static void
  1577. call_connect(struct rpc_task *task)
  1578. {
  1579. struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
  1580. dprintk("RPC: %5u call_connect xprt %p %s connected\n",
  1581. task->tk_pid, xprt,
  1582. (xprt_connected(xprt) ? "is" : "is not"));
  1583. task->tk_action = call_transmit;
  1584. if (!xprt_connected(xprt)) {
  1585. task->tk_action = call_connect_status;
  1586. if (task->tk_status < 0)
  1587. return;
  1588. if (task->tk_flags & RPC_TASK_NOCONNECT) {
  1589. rpc_exit(task, -ENOTCONN);
  1590. return;
  1591. }
  1592. xprt_connect(task);
  1593. }
  1594. }
  1595. /*
  1596. * 4c. Sort out connect result
  1597. */
  1598. static void
  1599. call_connect_status(struct rpc_task *task)
  1600. {
  1601. struct rpc_clnt *clnt = task->tk_client;
  1602. int status = task->tk_status;
  1603. dprint_status(task);
  1604. trace_rpc_connect_status(task, status);
  1605. task->tk_status = 0;
  1606. switch (status) {
  1607. case -ECONNREFUSED:
  1608. case -ECONNRESET:
  1609. case -ECONNABORTED:
  1610. case -ENETUNREACH:
  1611. case -EHOSTUNREACH:
  1612. case -EADDRINUSE:
  1613. case -ENOBUFS:
  1614. case -EPIPE:
  1615. if (RPC_IS_SOFTCONN(task))
  1616. break;
  1617. /* retry with existing socket, after a delay */
  1618. rpc_delay(task, 3*HZ);
  1619. case -EAGAIN:
  1620. /* Check for timeouts before looping back to call_bind */
  1621. case -ETIMEDOUT:
  1622. task->tk_action = call_timeout;
  1623. return;
  1624. case 0:
  1625. clnt->cl_stats->netreconn++;
  1626. task->tk_action = call_transmit;
  1627. return;
  1628. }
  1629. rpc_exit(task, status);
  1630. }
  1631. /*
  1632. * 5. Transmit the RPC request, and wait for reply
  1633. */
  1634. static void
  1635. call_transmit(struct rpc_task *task)
  1636. {
  1637. int is_retrans = RPC_WAS_SENT(task);
  1638. dprint_status(task);
  1639. task->tk_action = call_status;
  1640. if (task->tk_status < 0)
  1641. return;
  1642. if (!xprt_prepare_transmit(task))
  1643. return;
  1644. task->tk_action = call_transmit_status;
  1645. /* Encode here so that rpcsec_gss can use correct sequence number. */
  1646. if (rpc_task_need_encode(task)) {
  1647. rpc_xdr_encode(task);
  1648. /* Did the encode result in an error condition? */
  1649. if (task->tk_status != 0) {
  1650. /* Was the error nonfatal? */
  1651. if (task->tk_status == -EAGAIN)
  1652. rpc_delay(task, HZ >> 4);
  1653. else
  1654. rpc_exit(task, task->tk_status);
  1655. return;
  1656. }
  1657. }
  1658. xprt_transmit(task);
  1659. if (task->tk_status < 0)
  1660. return;
  1661. if (is_retrans)
  1662. task->tk_client->cl_stats->rpcretrans++;
  1663. /*
  1664. * On success, ensure that we call xprt_end_transmit() before sleeping
  1665. * in order to allow access to the socket to other RPC requests.
  1666. */
  1667. call_transmit_status(task);
  1668. if (rpc_reply_expected(task))
  1669. return;
  1670. task->tk_action = rpc_exit_task;
  1671. rpc_wake_up_queued_task(&task->tk_rqstp->rq_xprt->pending, task);
  1672. }
  1673. /*
  1674. * 5a. Handle cleanup after a transmission
  1675. */
  1676. static void
  1677. call_transmit_status(struct rpc_task *task)
  1678. {
  1679. task->tk_action = call_status;
  1680. /*
  1681. * Common case: success. Force the compiler to put this
  1682. * test first.
  1683. */
  1684. if (task->tk_status == 0) {
  1685. xprt_end_transmit(task);
  1686. rpc_task_force_reencode(task);
  1687. return;
  1688. }
  1689. switch (task->tk_status) {
  1690. case -EAGAIN:
  1691. case -ENOBUFS:
  1692. break;
  1693. default:
  1694. dprint_status(task);
  1695. xprt_end_transmit(task);
  1696. rpc_task_force_reencode(task);
  1697. break;
  1698. /*
  1699. * Special cases: if we've been waiting on the
  1700. * socket's write_space() callback, or if the
  1701. * socket just returned a connection error,
  1702. * then hold onto the transport lock.
  1703. */
  1704. case -ECONNREFUSED:
  1705. case -EHOSTDOWN:
  1706. case -EHOSTUNREACH:
  1707. case -ENETUNREACH:
  1708. case -EPERM:
  1709. if (RPC_IS_SOFTCONN(task)) {
  1710. xprt_end_transmit(task);
  1711. rpc_exit(task, task->tk_status);
  1712. break;
  1713. }
  1714. case -ECONNRESET:
  1715. case -ECONNABORTED:
  1716. case -EADDRINUSE:
  1717. case -ENOTCONN:
  1718. case -EPIPE:
  1719. rpc_task_force_reencode(task);
  1720. }
  1721. }
  1722. #if defined(CONFIG_SUNRPC_BACKCHANNEL)
  1723. /*
  1724. * 5b. Send the backchannel RPC reply. On error, drop the reply. In
  1725. * addition, disconnect on connectivity errors.
  1726. */
  1727. static void
  1728. call_bc_transmit(struct rpc_task *task)
  1729. {
  1730. struct rpc_rqst *req = task->tk_rqstp;
  1731. if (!xprt_prepare_transmit(task))
  1732. goto out_retry;
  1733. if (task->tk_status < 0) {
  1734. printk(KERN_NOTICE "RPC: Could not send backchannel reply "
  1735. "error: %d\n", task->tk_status);
  1736. goto out_done;
  1737. }
  1738. if (req->rq_connect_cookie != req->rq_xprt->connect_cookie)
  1739. req->rq_bytes_sent = 0;
  1740. xprt_transmit(task);
  1741. if (task->tk_status == -EAGAIN)
  1742. goto out_nospace;
  1743. xprt_end_transmit(task);
  1744. dprint_status(task);
  1745. switch (task->tk_status) {
  1746. case 0:
  1747. /* Success */
  1748. case -EHOSTDOWN:
  1749. case -EHOSTUNREACH:
  1750. case -ENETUNREACH:
  1751. case -ECONNRESET:
  1752. case -ECONNREFUSED:
  1753. case -EADDRINUSE:
  1754. case -ENOTCONN:
  1755. case -EPIPE:
  1756. break;
  1757. case -ETIMEDOUT:
  1758. /*
  1759. * Problem reaching the server. Disconnect and let the
  1760. * forechannel reestablish the connection. The server will
  1761. * have to retransmit the backchannel request and we'll
  1762. * reprocess it. Since these ops are idempotent, there's no
  1763. * need to cache our reply at this time.
  1764. */
  1765. printk(KERN_NOTICE "RPC: Could not send backchannel reply "
  1766. "error: %d\n", task->tk_status);
  1767. xprt_conditional_disconnect(req->rq_xprt,
  1768. req->rq_connect_cookie);
  1769. break;
  1770. default:
  1771. /*
  1772. * We were unable to reply and will have to drop the
  1773. * request. The server should reconnect and retransmit.
  1774. */
  1775. WARN_ON_ONCE(task->tk_status == -EAGAIN);
  1776. printk(KERN_NOTICE "RPC: Could not send backchannel reply "
  1777. "error: %d\n", task->tk_status);
  1778. break;
  1779. }
  1780. rpc_wake_up_queued_task(&req->rq_xprt->pending, task);
  1781. out_done:
  1782. task->tk_action = rpc_exit_task;
  1783. return;
  1784. out_nospace:
  1785. req->rq_connect_cookie = req->rq_xprt->connect_cookie;
  1786. out_retry:
  1787. task->tk_status = 0;
  1788. }
  1789. #endif /* CONFIG_SUNRPC_BACKCHANNEL */
  1790. /*
  1791. * 6. Sort out the RPC call status
  1792. */
  1793. static void
  1794. call_status(struct rpc_task *task)
  1795. {
  1796. struct rpc_clnt *clnt = task->tk_client;
  1797. struct rpc_rqst *req = task->tk_rqstp;
  1798. int status;
  1799. if (req->rq_reply_bytes_recvd > 0 && !req->rq_bytes_sent)
  1800. task->tk_status = req->rq_reply_bytes_recvd;
  1801. dprint_status(task);
  1802. status = task->tk_status;
  1803. if (status >= 0) {
  1804. task->tk_action = call_decode;
  1805. return;
  1806. }
  1807. trace_rpc_call_status(task);
  1808. task->tk_status = 0;
  1809. switch(status) {
  1810. case -EHOSTDOWN:
  1811. case -EHOSTUNREACH:
  1812. case -ENETUNREACH:
  1813. case -EPERM:
  1814. if (RPC_IS_SOFTCONN(task)) {
  1815. rpc_exit(task, status);
  1816. break;
  1817. }
  1818. /*
  1819. * Delay any retries for 3 seconds, then handle as if it
  1820. * were a timeout.
  1821. */
  1822. rpc_delay(task, 3*HZ);
  1823. case -ETIMEDOUT:
  1824. task->tk_action = call_timeout;
  1825. if (!(task->tk_flags & RPC_TASK_NO_RETRANS_TIMEOUT)
  1826. && task->tk_client->cl_discrtry)
  1827. xprt_conditional_disconnect(req->rq_xprt,
  1828. req->rq_connect_cookie);
  1829. break;
  1830. case -ECONNREFUSED:
  1831. case -ECONNRESET:
  1832. case -ECONNABORTED:
  1833. rpc_force_rebind(clnt);
  1834. case -EADDRINUSE:
  1835. rpc_delay(task, 3*HZ);
  1836. case -EPIPE:
  1837. case -ENOTCONN:
  1838. task->tk_action = call_bind;
  1839. break;
  1840. case -ENOBUFS:
  1841. rpc_delay(task, HZ>>2);
  1842. case -EAGAIN:
  1843. task->tk_action = call_transmit;
  1844. break;
  1845. case -EIO:
  1846. /* shutdown or soft timeout */
  1847. rpc_exit(task, status);
  1848. break;
  1849. default:
  1850. if (clnt->cl_chatty)
  1851. printk("%s: RPC call returned error %d\n",
  1852. clnt->cl_program->name, -status);
  1853. rpc_exit(task, status);
  1854. }
  1855. }
  1856. /*
  1857. * 6a. Handle RPC timeout
  1858. * We do not release the request slot, so we keep using the
  1859. * same XID for all retransmits.
  1860. */
  1861. static void
  1862. call_timeout(struct rpc_task *task)
  1863. {
  1864. struct rpc_clnt *clnt = task->tk_client;
  1865. if (xprt_adjust_timeout(task->tk_rqstp) == 0) {
  1866. dprintk("RPC: %5u call_timeout (minor)\n", task->tk_pid);
  1867. goto retry;
  1868. }
  1869. dprintk("RPC: %5u call_timeout (major)\n", task->tk_pid);
  1870. task->tk_timeouts++;
  1871. if (RPC_IS_SOFTCONN(task)) {
  1872. rpc_exit(task, -ETIMEDOUT);
  1873. return;
  1874. }
  1875. if (RPC_IS_SOFT(task)) {
  1876. if (clnt->cl_chatty) {
  1877. rcu_read_lock();
  1878. printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
  1879. clnt->cl_program->name,
  1880. rcu_dereference(clnt->cl_xprt)->servername);
  1881. rcu_read_unlock();
  1882. }
  1883. if (task->tk_flags & RPC_TASK_TIMEOUT)
  1884. rpc_exit(task, -ETIMEDOUT);
  1885. else
  1886. rpc_exit(task, -EIO);
  1887. return;
  1888. }
  1889. if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
  1890. task->tk_flags |= RPC_CALL_MAJORSEEN;
  1891. if (clnt->cl_chatty) {
  1892. rcu_read_lock();
  1893. printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
  1894. clnt->cl_program->name,
  1895. rcu_dereference(clnt->cl_xprt)->servername);
  1896. rcu_read_unlock();
  1897. }
  1898. }
  1899. rpc_force_rebind(clnt);
  1900. /*
  1901. * Did our request time out due to an RPCSEC_GSS out-of-sequence
  1902. * event? RFC2203 requires the server to drop all such requests.
  1903. */
  1904. rpcauth_invalcred(task);
  1905. retry:
  1906. task->tk_action = call_bind;
  1907. task->tk_status = 0;
  1908. }
  1909. /*
  1910. * 7. Decode the RPC reply
  1911. */
  1912. static void
  1913. call_decode(struct rpc_task *task)
  1914. {
  1915. struct rpc_clnt *clnt = task->tk_client;
  1916. struct rpc_rqst *req = task->tk_rqstp;
  1917. kxdrdproc_t decode = task->tk_msg.rpc_proc->p_decode;
  1918. __be32 *p;
  1919. dprint_status(task);
  1920. if (task->tk_flags & RPC_CALL_MAJORSEEN) {
  1921. if (clnt->cl_chatty) {
  1922. rcu_read_lock();
  1923. printk(KERN_NOTICE "%s: server %s OK\n",
  1924. clnt->cl_program->name,
  1925. rcu_dereference(clnt->cl_xprt)->servername);
  1926. rcu_read_unlock();
  1927. }
  1928. task->tk_flags &= ~RPC_CALL_MAJORSEEN;
  1929. }
  1930. /*
  1931. * Ensure that we see all writes made by xprt_complete_rqst()
  1932. * before it changed req->rq_reply_bytes_recvd.
  1933. */
  1934. smp_rmb();
  1935. req->rq_rcv_buf.len = req->rq_private_buf.len;
  1936. /* Check that the softirq receive buffer is valid */
  1937. WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
  1938. sizeof(req->rq_rcv_buf)) != 0);
  1939. if (req->rq_rcv_buf.len < 12) {
  1940. if (!RPC_IS_SOFT(task)) {
  1941. task->tk_action = call_bind;
  1942. goto out_retry;
  1943. }
  1944. dprintk("RPC: %s: too small RPC reply size (%d bytes)\n",
  1945. clnt->cl_program->name, task->tk_status);
  1946. task->tk_action = call_timeout;
  1947. goto out_retry;
  1948. }
  1949. p = rpc_verify_header(task);
  1950. if (IS_ERR(p)) {
  1951. if (p == ERR_PTR(-EAGAIN))
  1952. goto out_retry;
  1953. return;
  1954. }
  1955. task->tk_action = rpc_exit_task;
  1956. if (decode) {
  1957. task->tk_status = rpcauth_unwrap_resp(task, decode, req, p,
  1958. task->tk_msg.rpc_resp);
  1959. }
  1960. dprintk("RPC: %5u call_decode result %d\n", task->tk_pid,
  1961. task->tk_status);
  1962. return;
  1963. out_retry:
  1964. task->tk_status = 0;
  1965. /* Note: rpc_verify_header() may have freed the RPC slot */
  1966. if (task->tk_rqstp == req) {
  1967. req->rq_reply_bytes_recvd = req->rq_rcv_buf.len = 0;
  1968. if (task->tk_client->cl_discrtry)
  1969. xprt_conditional_disconnect(req->rq_xprt,
  1970. req->rq_connect_cookie);
  1971. }
  1972. }
  1973. static __be32 *
  1974. rpc_encode_header(struct rpc_task *task)
  1975. {
  1976. struct rpc_clnt *clnt = task->tk_client;
  1977. struct rpc_rqst *req = task->tk_rqstp;
  1978. __be32 *p = req->rq_svec[0].iov_base;
  1979. /* FIXME: check buffer size? */
  1980. p = xprt_skip_transport_header(req->rq_xprt, p);
  1981. *p++ = req->rq_xid; /* XID */
  1982. *p++ = htonl(RPC_CALL); /* CALL */
  1983. *p++ = htonl(RPC_VERSION); /* RPC version */
  1984. *p++ = htonl(clnt->cl_prog); /* program number */
  1985. *p++ = htonl(clnt->cl_vers); /* program version */
  1986. *p++ = htonl(task->tk_msg.rpc_proc->p_proc); /* procedure */
  1987. p = rpcauth_marshcred(task, p);
  1988. req->rq_slen = xdr_adjust_iovec(&req->rq_svec[0], p);
  1989. return p;
  1990. }
  1991. static __be32 *
  1992. rpc_verify_header(struct rpc_task *task)
  1993. {
  1994. struct rpc_clnt *clnt = task->tk_client;
  1995. struct kvec *iov = &task->tk_rqstp->rq_rcv_buf.head[0];
  1996. int len = task->tk_rqstp->rq_rcv_buf.len >> 2;
  1997. __be32 *p = iov->iov_base;
  1998. u32 n;
  1999. int error = -EACCES;
  2000. if ((task->tk_rqstp->rq_rcv_buf.len & 3) != 0) {
  2001. /* RFC-1014 says that the representation of XDR data must be a
  2002. * multiple of four bytes
  2003. * - if it isn't pointer subtraction in the NFS client may give
  2004. * undefined results
  2005. */
  2006. dprintk("RPC: %5u %s: XDR representation not a multiple of"
  2007. " 4 bytes: 0x%x\n", task->tk_pid, __func__,
  2008. task->tk_rqstp->rq_rcv_buf.len);
  2009. error = -EIO;
  2010. goto out_err;
  2011. }
  2012. if ((len -= 3) < 0)
  2013. goto out_overflow;
  2014. p += 1; /* skip XID */
  2015. if ((n = ntohl(*p++)) != RPC_REPLY) {
  2016. dprintk("RPC: %5u %s: not an RPC reply: %x\n",
  2017. task->tk_pid, __func__, n);
  2018. error = -EIO;
  2019. goto out_garbage;
  2020. }
  2021. if ((n = ntohl(*p++)) != RPC_MSG_ACCEPTED) {
  2022. if (--len < 0)
  2023. goto out_overflow;
  2024. switch ((n = ntohl(*p++))) {
  2025. case RPC_AUTH_ERROR:
  2026. break;
  2027. case RPC_MISMATCH:
  2028. dprintk("RPC: %5u %s: RPC call version mismatch!\n",
  2029. task->tk_pid, __func__);
  2030. error = -EPROTONOSUPPORT;
  2031. goto out_err;
  2032. default:
  2033. dprintk("RPC: %5u %s: RPC call rejected, "
  2034. "unknown error: %x\n",
  2035. task->tk_pid, __func__, n);
  2036. error = -EIO;
  2037. goto out_err;
  2038. }
  2039. if (--len < 0)
  2040. goto out_overflow;
  2041. switch ((n = ntohl(*p++))) {
  2042. case RPC_AUTH_REJECTEDCRED:
  2043. case RPC_AUTH_REJECTEDVERF:
  2044. case RPCSEC_GSS_CREDPROBLEM:
  2045. case RPCSEC_GSS_CTXPROBLEM:
  2046. if (!task->tk_cred_retry)
  2047. break;
  2048. task->tk_cred_retry--;
  2049. dprintk("RPC: %5u %s: retry stale creds\n",
  2050. task->tk_pid, __func__);
  2051. rpcauth_invalcred(task);
  2052. /* Ensure we obtain a new XID! */
  2053. xprt_release(task);
  2054. task->tk_action = call_reserve;
  2055. goto out_retry;
  2056. case RPC_AUTH_BADCRED:
  2057. case RPC_AUTH_BADVERF:
  2058. /* possibly garbled cred/verf? */
  2059. if (!task->tk_garb_retry)
  2060. break;
  2061. task->tk_garb_retry--;
  2062. dprintk("RPC: %5u %s: retry garbled creds\n",
  2063. task->tk_pid, __func__);
  2064. task->tk_action = call_bind;
  2065. goto out_retry;
  2066. case RPC_AUTH_TOOWEAK:
  2067. rcu_read_lock();
  2068. printk(KERN_NOTICE "RPC: server %s requires stronger "
  2069. "authentication.\n",
  2070. rcu_dereference(clnt->cl_xprt)->servername);
  2071. rcu_read_unlock();
  2072. break;
  2073. default:
  2074. dprintk("RPC: %5u %s: unknown auth error: %x\n",
  2075. task->tk_pid, __func__, n);
  2076. error = -EIO;
  2077. }
  2078. dprintk("RPC: %5u %s: call rejected %d\n",
  2079. task->tk_pid, __func__, n);
  2080. goto out_err;
  2081. }
  2082. p = rpcauth_checkverf(task, p);
  2083. if (IS_ERR(p)) {
  2084. error = PTR_ERR(p);
  2085. dprintk("RPC: %5u %s: auth check failed with %d\n",
  2086. task->tk_pid, __func__, error);
  2087. goto out_garbage; /* bad verifier, retry */
  2088. }
  2089. len = p - (__be32 *)iov->iov_base - 1;
  2090. if (len < 0)
  2091. goto out_overflow;
  2092. switch ((n = ntohl(*p++))) {
  2093. case RPC_SUCCESS:
  2094. return p;
  2095. case RPC_PROG_UNAVAIL:
  2096. dprintk_rcu("RPC: %5u %s: program %u is unsupported "
  2097. "by server %s\n", task->tk_pid, __func__,
  2098. (unsigned int)clnt->cl_prog,
  2099. rcu_dereference(clnt->cl_xprt)->servername);
  2100. error = -EPFNOSUPPORT;
  2101. goto out_err;
  2102. case RPC_PROG_MISMATCH:
  2103. dprintk_rcu("RPC: %5u %s: program %u, version %u unsupported "
  2104. "by server %s\n", task->tk_pid, __func__,
  2105. (unsigned int)clnt->cl_prog,
  2106. (unsigned int)clnt->cl_vers,
  2107. rcu_dereference(clnt->cl_xprt)->servername);
  2108. error = -EPROTONOSUPPORT;
  2109. goto out_err;
  2110. case RPC_PROC_UNAVAIL:
  2111. dprintk_rcu("RPC: %5u %s: proc %s unsupported by program %u, "
  2112. "version %u on server %s\n",
  2113. task->tk_pid, __func__,
  2114. rpc_proc_name(task),
  2115. clnt->cl_prog, clnt->cl_vers,
  2116. rcu_dereference(clnt->cl_xprt)->servername);
  2117. error = -EOPNOTSUPP;
  2118. goto out_err;
  2119. case RPC_GARBAGE_ARGS:
  2120. dprintk("RPC: %5u %s: server saw garbage\n",
  2121. task->tk_pid, __func__);
  2122. break; /* retry */
  2123. default:
  2124. dprintk("RPC: %5u %s: server accept status: %x\n",
  2125. task->tk_pid, __func__, n);
  2126. /* Also retry */
  2127. }
  2128. out_garbage:
  2129. clnt->cl_stats->rpcgarbage++;
  2130. if (task->tk_garb_retry) {
  2131. task->tk_garb_retry--;
  2132. dprintk("RPC: %5u %s: retrying\n",
  2133. task->tk_pid, __func__);
  2134. task->tk_action = call_bind;
  2135. out_retry:
  2136. return ERR_PTR(-EAGAIN);
  2137. }
  2138. out_err:
  2139. rpc_exit(task, error);
  2140. dprintk("RPC: %5u %s: call failed with error %d\n", task->tk_pid,
  2141. __func__, error);
  2142. return ERR_PTR(error);
  2143. out_overflow:
  2144. dprintk("RPC: %5u %s: server reply was truncated.\n", task->tk_pid,
  2145. __func__);
  2146. goto out_garbage;
  2147. }
  2148. static void rpcproc_encode_null(void *rqstp, struct xdr_stream *xdr, void *obj)
  2149. {
  2150. }
  2151. static int rpcproc_decode_null(void *rqstp, struct xdr_stream *xdr, void *obj)
  2152. {
  2153. return 0;
  2154. }
  2155. static struct rpc_procinfo rpcproc_null = {
  2156. .p_encode = rpcproc_encode_null,
  2157. .p_decode = rpcproc_decode_null,
  2158. };
  2159. static int rpc_ping(struct rpc_clnt *clnt)
  2160. {
  2161. struct rpc_message msg = {
  2162. .rpc_proc = &rpcproc_null,
  2163. };
  2164. int err;
  2165. msg.rpc_cred = authnull_ops.lookup_cred(NULL, NULL, 0);
  2166. err = rpc_call_sync(clnt, &msg, RPC_TASK_SOFT | RPC_TASK_SOFTCONN);
  2167. put_rpccred(msg.rpc_cred);
  2168. return err;
  2169. }
  2170. struct rpc_task *rpc_call_null(struct rpc_clnt *clnt, struct rpc_cred *cred, int flags)
  2171. {
  2172. struct rpc_message msg = {
  2173. .rpc_proc = &rpcproc_null,
  2174. .rpc_cred = cred,
  2175. };
  2176. struct rpc_task_setup task_setup_data = {
  2177. .rpc_client = clnt,
  2178. .rpc_message = &msg,
  2179. .callback_ops = &rpc_default_ops,
  2180. .flags = flags,
  2181. };
  2182. return rpc_run_task(&task_setup_data);
  2183. }
  2184. EXPORT_SYMBOL_GPL(rpc_call_null);
  2185. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  2186. static void rpc_show_header(void)
  2187. {
  2188. printk(KERN_INFO "-pid- flgs status -client- --rqstp- "
  2189. "-timeout ---ops--\n");
  2190. }
  2191. static void rpc_show_task(const struct rpc_clnt *clnt,
  2192. const struct rpc_task *task)
  2193. {
  2194. const char *rpc_waitq = "none";
  2195. if (RPC_IS_QUEUED(task))
  2196. rpc_waitq = rpc_qname(task->tk_waitqueue);
  2197. printk(KERN_INFO "%5u %04x %6d %8p %8p %8ld %8p %sv%u %s a:%ps q:%s\n",
  2198. task->tk_pid, task->tk_flags, task->tk_status,
  2199. clnt, task->tk_rqstp, task->tk_timeout, task->tk_ops,
  2200. clnt->cl_program->name, clnt->cl_vers, rpc_proc_name(task),
  2201. task->tk_action, rpc_waitq);
  2202. }
  2203. void rpc_show_tasks(struct net *net)
  2204. {
  2205. struct rpc_clnt *clnt;
  2206. struct rpc_task *task;
  2207. int header = 0;
  2208. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  2209. spin_lock(&sn->rpc_client_lock);
  2210. list_for_each_entry(clnt, &sn->all_clients, cl_clients) {
  2211. spin_lock(&clnt->cl_lock);
  2212. list_for_each_entry(task, &clnt->cl_tasks, tk_task) {
  2213. if (!header) {
  2214. rpc_show_header();
  2215. header++;
  2216. }
  2217. rpc_show_task(clnt, task);
  2218. }
  2219. spin_unlock(&clnt->cl_lock);
  2220. }
  2221. spin_unlock(&sn->rpc_client_lock);
  2222. }
  2223. #endif
  2224. #if IS_ENABLED(CONFIG_SUNRPC_SWAP)
  2225. int
  2226. rpc_clnt_swap_activate(struct rpc_clnt *clnt)
  2227. {
  2228. int ret = 0;
  2229. struct rpc_xprt *xprt;
  2230. if (atomic_inc_return(&clnt->cl_swapper) == 1) {
  2231. retry:
  2232. rcu_read_lock();
  2233. xprt = xprt_get(rcu_dereference(clnt->cl_xprt));
  2234. rcu_read_unlock();
  2235. if (!xprt) {
  2236. /*
  2237. * If we didn't get a reference, then we likely are
  2238. * racing with a migration event. Wait for a grace
  2239. * period and try again.
  2240. */
  2241. synchronize_rcu();
  2242. goto retry;
  2243. }
  2244. ret = xprt_enable_swap(xprt);
  2245. xprt_put(xprt);
  2246. }
  2247. return ret;
  2248. }
  2249. EXPORT_SYMBOL_GPL(rpc_clnt_swap_activate);
  2250. void
  2251. rpc_clnt_swap_deactivate(struct rpc_clnt *clnt)
  2252. {
  2253. struct rpc_xprt *xprt;
  2254. if (atomic_dec_if_positive(&clnt->cl_swapper) == 0) {
  2255. retry:
  2256. rcu_read_lock();
  2257. xprt = xprt_get(rcu_dereference(clnt->cl_xprt));
  2258. rcu_read_unlock();
  2259. if (!xprt) {
  2260. /*
  2261. * If we didn't get a reference, then we likely are
  2262. * racing with a migration event. Wait for a grace
  2263. * period and try again.
  2264. */
  2265. synchronize_rcu();
  2266. goto retry;
  2267. }
  2268. xprt_disable_swap(xprt);
  2269. xprt_put(xprt);
  2270. }
  2271. }
  2272. EXPORT_SYMBOL_GPL(rpc_clnt_swap_deactivate);
  2273. #endif /* CONFIG_SUNRPC_SWAP */