clnt.c 58 KB

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