clnt.c 58 KB

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