clnt.c 55 KB

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