clnt.c 54 KB

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