xprtsock.c 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954
  1. /*
  2. * linux/net/sunrpc/xprtsock.c
  3. *
  4. * Client-side transport implementation for sockets.
  5. *
  6. * TCP callback races fixes (C) 1998 Red Hat
  7. * TCP send fixes (C) 1998 Red Hat
  8. * TCP NFS related read + write fixes
  9. * (C) 1999 Dave Airlie, University of Limerick, Ireland <airlied@linux.ie>
  10. *
  11. * Rewrite of larges part of the code in order to stabilize TCP stuff.
  12. * Fix behaviour when socket buffer is full.
  13. * (C) 1999 Trond Myklebust <trond.myklebust@fys.uio.no>
  14. *
  15. * IP socket transport implementation, (C) 2005 Chuck Lever <cel@netapp.com>
  16. *
  17. * IPv6 support contributed by Gilles Quillard, Bull Open Source, 2005.
  18. * <gilles.quillard@bull.net>
  19. */
  20. #include <linux/types.h>
  21. #include <linux/string.h>
  22. #include <linux/slab.h>
  23. #include <linux/module.h>
  24. #include <linux/capability.h>
  25. #include <linux/pagemap.h>
  26. #include <linux/errno.h>
  27. #include <linux/socket.h>
  28. #include <linux/in.h>
  29. #include <linux/net.h>
  30. #include <linux/mm.h>
  31. #include <linux/un.h>
  32. #include <linux/udp.h>
  33. #include <linux/tcp.h>
  34. #include <linux/sunrpc/clnt.h>
  35. #include <linux/sunrpc/sched.h>
  36. #include <linux/sunrpc/svcsock.h>
  37. #include <linux/sunrpc/xprtsock.h>
  38. #include <linux/file.h>
  39. #ifdef CONFIG_NFS_V4_1
  40. #include <linux/sunrpc/bc_xprt.h>
  41. #endif
  42. #include <net/sock.h>
  43. #include <net/checksum.h>
  44. #include <net/udp.h>
  45. #include <net/tcp.h>
  46. #include "sunrpc.h"
  47. static void xs_close(struct rpc_xprt *xprt);
  48. /*
  49. * xprtsock tunables
  50. */
  51. unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
  52. unsigned int xprt_tcp_slot_table_entries = RPC_DEF_SLOT_TABLE;
  53. unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
  54. unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
  55. #define XS_TCP_LINGER_TO (15U * HZ)
  56. static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO;
  57. /*
  58. * We can register our own files under /proc/sys/sunrpc by
  59. * calling register_sysctl_table() again. The files in that
  60. * directory become the union of all files registered there.
  61. *
  62. * We simply need to make sure that we don't collide with
  63. * someone else's file names!
  64. */
  65. #ifdef RPC_DEBUG
  66. static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE;
  67. static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE;
  68. static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT;
  69. static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT;
  70. static struct ctl_table_header *sunrpc_table_header;
  71. /*
  72. * FIXME: changing the UDP slot table size should also resize the UDP
  73. * socket buffers for existing UDP transports
  74. */
  75. static ctl_table xs_tunables_table[] = {
  76. {
  77. .procname = "udp_slot_table_entries",
  78. .data = &xprt_udp_slot_table_entries,
  79. .maxlen = sizeof(unsigned int),
  80. .mode = 0644,
  81. .proc_handler = proc_dointvec_minmax,
  82. .extra1 = &min_slot_table_size,
  83. .extra2 = &max_slot_table_size
  84. },
  85. {
  86. .procname = "tcp_slot_table_entries",
  87. .data = &xprt_tcp_slot_table_entries,
  88. .maxlen = sizeof(unsigned int),
  89. .mode = 0644,
  90. .proc_handler = proc_dointvec_minmax,
  91. .extra1 = &min_slot_table_size,
  92. .extra2 = &max_slot_table_size
  93. },
  94. {
  95. .procname = "min_resvport",
  96. .data = &xprt_min_resvport,
  97. .maxlen = sizeof(unsigned int),
  98. .mode = 0644,
  99. .proc_handler = proc_dointvec_minmax,
  100. .extra1 = &xprt_min_resvport_limit,
  101. .extra2 = &xprt_max_resvport_limit
  102. },
  103. {
  104. .procname = "max_resvport",
  105. .data = &xprt_max_resvport,
  106. .maxlen = sizeof(unsigned int),
  107. .mode = 0644,
  108. .proc_handler = proc_dointvec_minmax,
  109. .extra1 = &xprt_min_resvport_limit,
  110. .extra2 = &xprt_max_resvport_limit
  111. },
  112. {
  113. .procname = "tcp_fin_timeout",
  114. .data = &xs_tcp_fin_timeout,
  115. .maxlen = sizeof(xs_tcp_fin_timeout),
  116. .mode = 0644,
  117. .proc_handler = proc_dointvec_jiffies,
  118. },
  119. { },
  120. };
  121. static ctl_table sunrpc_table[] = {
  122. {
  123. .procname = "sunrpc",
  124. .mode = 0555,
  125. .child = xs_tunables_table
  126. },
  127. { },
  128. };
  129. #endif
  130. /*
  131. * Wait duration for a reply from the RPC portmapper.
  132. */
  133. #define XS_BIND_TO (60U * HZ)
  134. /*
  135. * Delay if a UDP socket connect error occurs. This is most likely some
  136. * kind of resource problem on the local host.
  137. */
  138. #define XS_UDP_REEST_TO (2U * HZ)
  139. /*
  140. * The reestablish timeout allows clients to delay for a bit before attempting
  141. * to reconnect to a server that just dropped our connection.
  142. *
  143. * We implement an exponential backoff when trying to reestablish a TCP
  144. * transport connection with the server. Some servers like to drop a TCP
  145. * connection when they are overworked, so we start with a short timeout and
  146. * increase over time if the server is down or not responding.
  147. */
  148. #define XS_TCP_INIT_REEST_TO (3U * HZ)
  149. #define XS_TCP_MAX_REEST_TO (5U * 60 * HZ)
  150. /*
  151. * TCP idle timeout; client drops the transport socket if it is idle
  152. * for this long. Note that we also timeout UDP sockets to prevent
  153. * holding port numbers when there is no RPC traffic.
  154. */
  155. #define XS_IDLE_DISC_TO (5U * 60 * HZ)
  156. #ifdef RPC_DEBUG
  157. # undef RPC_DEBUG_DATA
  158. # define RPCDBG_FACILITY RPCDBG_TRANS
  159. #endif
  160. #ifdef RPC_DEBUG_DATA
  161. static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
  162. {
  163. u8 *buf = (u8 *) packet;
  164. int j;
  165. dprintk("RPC: %s\n", msg);
  166. for (j = 0; j < count && j < 128; j += 4) {
  167. if (!(j & 31)) {
  168. if (j)
  169. dprintk("\n");
  170. dprintk("0x%04x ", j);
  171. }
  172. dprintk("%02x%02x%02x%02x ",
  173. buf[j], buf[j+1], buf[j+2], buf[j+3]);
  174. }
  175. dprintk("\n");
  176. }
  177. #else
  178. static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
  179. {
  180. /* NOP */
  181. }
  182. #endif
  183. struct sock_xprt {
  184. struct rpc_xprt xprt;
  185. /*
  186. * Network layer
  187. */
  188. struct socket * sock;
  189. struct sock * inet;
  190. /*
  191. * State of TCP reply receive
  192. */
  193. __be32 tcp_fraghdr,
  194. tcp_xid,
  195. tcp_calldir;
  196. u32 tcp_offset,
  197. tcp_reclen;
  198. unsigned long tcp_copied,
  199. tcp_flags;
  200. /*
  201. * Connection of transports
  202. */
  203. struct delayed_work connect_worker;
  204. struct sockaddr_storage srcaddr;
  205. unsigned short srcport;
  206. /*
  207. * UDP socket buffer size parameters
  208. */
  209. size_t rcvsize,
  210. sndsize;
  211. /*
  212. * Saved socket callback addresses
  213. */
  214. void (*old_data_ready)(struct sock *, int);
  215. void (*old_state_change)(struct sock *);
  216. void (*old_write_space)(struct sock *);
  217. void (*old_error_report)(struct sock *);
  218. };
  219. /*
  220. * TCP receive state flags
  221. */
  222. #define TCP_RCV_LAST_FRAG (1UL << 0)
  223. #define TCP_RCV_COPY_FRAGHDR (1UL << 1)
  224. #define TCP_RCV_COPY_XID (1UL << 2)
  225. #define TCP_RCV_COPY_DATA (1UL << 3)
  226. #define TCP_RCV_READ_CALLDIR (1UL << 4)
  227. #define TCP_RCV_COPY_CALLDIR (1UL << 5)
  228. /*
  229. * TCP RPC flags
  230. */
  231. #define TCP_RPC_REPLY (1UL << 6)
  232. static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
  233. {
  234. return (struct sockaddr *) &xprt->addr;
  235. }
  236. static inline struct sockaddr_un *xs_addr_un(struct rpc_xprt *xprt)
  237. {
  238. return (struct sockaddr_un *) &xprt->addr;
  239. }
  240. static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
  241. {
  242. return (struct sockaddr_in *) &xprt->addr;
  243. }
  244. static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
  245. {
  246. return (struct sockaddr_in6 *) &xprt->addr;
  247. }
  248. static void xs_format_common_peer_addresses(struct rpc_xprt *xprt)
  249. {
  250. struct sockaddr *sap = xs_addr(xprt);
  251. struct sockaddr_in6 *sin6;
  252. struct sockaddr_in *sin;
  253. struct sockaddr_un *sun;
  254. char buf[128];
  255. switch (sap->sa_family) {
  256. case AF_LOCAL:
  257. sun = xs_addr_un(xprt);
  258. strlcpy(buf, sun->sun_path, sizeof(buf));
  259. xprt->address_strings[RPC_DISPLAY_ADDR] =
  260. kstrdup(buf, GFP_KERNEL);
  261. break;
  262. case AF_INET:
  263. (void)rpc_ntop(sap, buf, sizeof(buf));
  264. xprt->address_strings[RPC_DISPLAY_ADDR] =
  265. kstrdup(buf, GFP_KERNEL);
  266. sin = xs_addr_in(xprt);
  267. snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr));
  268. break;
  269. case AF_INET6:
  270. (void)rpc_ntop(sap, buf, sizeof(buf));
  271. xprt->address_strings[RPC_DISPLAY_ADDR] =
  272. kstrdup(buf, GFP_KERNEL);
  273. sin6 = xs_addr_in6(xprt);
  274. snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr);
  275. break;
  276. default:
  277. BUG();
  278. }
  279. xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
  280. }
  281. static void xs_format_common_peer_ports(struct rpc_xprt *xprt)
  282. {
  283. struct sockaddr *sap = xs_addr(xprt);
  284. char buf[128];
  285. snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
  286. xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
  287. snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
  288. xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
  289. }
  290. static void xs_format_peer_addresses(struct rpc_xprt *xprt,
  291. const char *protocol,
  292. const char *netid)
  293. {
  294. xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
  295. xprt->address_strings[RPC_DISPLAY_NETID] = netid;
  296. xs_format_common_peer_addresses(xprt);
  297. xs_format_common_peer_ports(xprt);
  298. }
  299. static void xs_update_peer_port(struct rpc_xprt *xprt)
  300. {
  301. kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]);
  302. kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
  303. xs_format_common_peer_ports(xprt);
  304. }
  305. static void xs_free_peer_addresses(struct rpc_xprt *xprt)
  306. {
  307. unsigned int i;
  308. for (i = 0; i < RPC_DISPLAY_MAX; i++)
  309. switch (i) {
  310. case RPC_DISPLAY_PROTO:
  311. case RPC_DISPLAY_NETID:
  312. continue;
  313. default:
  314. kfree(xprt->address_strings[i]);
  315. }
  316. }
  317. #define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
  318. static int xs_send_kvec(struct socket *sock, struct sockaddr *addr, int addrlen, struct kvec *vec, unsigned int base, int more)
  319. {
  320. struct msghdr msg = {
  321. .msg_name = addr,
  322. .msg_namelen = addrlen,
  323. .msg_flags = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0),
  324. };
  325. struct kvec iov = {
  326. .iov_base = vec->iov_base + base,
  327. .iov_len = vec->iov_len - base,
  328. };
  329. if (iov.iov_len != 0)
  330. return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
  331. return kernel_sendmsg(sock, &msg, NULL, 0, 0);
  332. }
  333. static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more)
  334. {
  335. struct page **ppage;
  336. unsigned int remainder;
  337. int err, sent = 0;
  338. remainder = xdr->page_len - base;
  339. base += xdr->page_base;
  340. ppage = xdr->pages + (base >> PAGE_SHIFT);
  341. base &= ~PAGE_MASK;
  342. for(;;) {
  343. unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
  344. int flags = XS_SENDMSG_FLAGS;
  345. remainder -= len;
  346. if (remainder != 0 || more)
  347. flags |= MSG_MORE;
  348. err = sock->ops->sendpage(sock, *ppage, base, len, flags);
  349. if (remainder == 0 || err != len)
  350. break;
  351. sent += err;
  352. ppage++;
  353. base = 0;
  354. }
  355. if (sent == 0)
  356. return err;
  357. if (err > 0)
  358. sent += err;
  359. return sent;
  360. }
  361. /**
  362. * xs_sendpages - write pages directly to a socket
  363. * @sock: socket to send on
  364. * @addr: UDP only -- address of destination
  365. * @addrlen: UDP only -- length of destination address
  366. * @xdr: buffer containing this request
  367. * @base: starting position in the buffer
  368. *
  369. */
  370. static int xs_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base)
  371. {
  372. unsigned int remainder = xdr->len - base;
  373. int err, sent = 0;
  374. if (unlikely(!sock))
  375. return -ENOTSOCK;
  376. clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
  377. if (base != 0) {
  378. addr = NULL;
  379. addrlen = 0;
  380. }
  381. if (base < xdr->head[0].iov_len || addr != NULL) {
  382. unsigned int len = xdr->head[0].iov_len - base;
  383. remainder -= len;
  384. err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0);
  385. if (remainder == 0 || err != len)
  386. goto out;
  387. sent += err;
  388. base = 0;
  389. } else
  390. base -= xdr->head[0].iov_len;
  391. if (base < xdr->page_len) {
  392. unsigned int len = xdr->page_len - base;
  393. remainder -= len;
  394. err = xs_send_pagedata(sock, xdr, base, remainder != 0);
  395. if (remainder == 0 || err != len)
  396. goto out;
  397. sent += err;
  398. base = 0;
  399. } else
  400. base -= xdr->page_len;
  401. if (base >= xdr->tail[0].iov_len)
  402. return sent;
  403. err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0);
  404. out:
  405. if (sent == 0)
  406. return err;
  407. if (err > 0)
  408. sent += err;
  409. return sent;
  410. }
  411. static void xs_nospace_callback(struct rpc_task *task)
  412. {
  413. struct sock_xprt *transport = container_of(task->tk_rqstp->rq_xprt, struct sock_xprt, xprt);
  414. transport->inet->sk_write_pending--;
  415. clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
  416. }
  417. /**
  418. * xs_nospace - place task on wait queue if transmit was incomplete
  419. * @task: task to put to sleep
  420. *
  421. */
  422. static int xs_nospace(struct rpc_task *task)
  423. {
  424. struct rpc_rqst *req = task->tk_rqstp;
  425. struct rpc_xprt *xprt = req->rq_xprt;
  426. struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
  427. int ret = -EAGAIN;
  428. dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
  429. task->tk_pid, req->rq_slen - req->rq_bytes_sent,
  430. req->rq_slen);
  431. /* Protect against races with write_space */
  432. spin_lock_bh(&xprt->transport_lock);
  433. /* Don't race with disconnect */
  434. if (xprt_connected(xprt)) {
  435. if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) {
  436. /*
  437. * Notify TCP that we're limited by the application
  438. * window size
  439. */
  440. set_bit(SOCK_NOSPACE, &transport->sock->flags);
  441. transport->inet->sk_write_pending++;
  442. /* ...and wait for more buffer space */
  443. xprt_wait_for_buffer_space(task, xs_nospace_callback);
  444. }
  445. } else {
  446. clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
  447. ret = -ENOTCONN;
  448. }
  449. spin_unlock_bh(&xprt->transport_lock);
  450. return ret;
  451. }
  452. /*
  453. * Construct a stream transport record marker in @buf.
  454. */
  455. static inline void xs_encode_stream_record_marker(struct xdr_buf *buf)
  456. {
  457. u32 reclen = buf->len - sizeof(rpc_fraghdr);
  458. rpc_fraghdr *base = buf->head[0].iov_base;
  459. *base = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | reclen);
  460. }
  461. /**
  462. * xs_local_send_request - write an RPC request to an AF_LOCAL socket
  463. * @task: RPC task that manages the state of an RPC request
  464. *
  465. * Return values:
  466. * 0: The request has been sent
  467. * EAGAIN: The socket was blocked, please call again later to
  468. * complete the request
  469. * ENOTCONN: Caller needs to invoke connect logic then call again
  470. * other: Some other error occured, the request was not sent
  471. */
  472. static int xs_local_send_request(struct rpc_task *task)
  473. {
  474. struct rpc_rqst *req = task->tk_rqstp;
  475. struct rpc_xprt *xprt = req->rq_xprt;
  476. struct sock_xprt *transport =
  477. container_of(xprt, struct sock_xprt, xprt);
  478. struct xdr_buf *xdr = &req->rq_snd_buf;
  479. int status;
  480. xs_encode_stream_record_marker(&req->rq_snd_buf);
  481. xs_pktdump("packet data:",
  482. req->rq_svec->iov_base, req->rq_svec->iov_len);
  483. status = xs_sendpages(transport->sock, NULL, 0,
  484. xdr, req->rq_bytes_sent);
  485. dprintk("RPC: %s(%u) = %d\n",
  486. __func__, xdr->len - req->rq_bytes_sent, status);
  487. if (likely(status >= 0)) {
  488. req->rq_bytes_sent += status;
  489. req->rq_xmit_bytes_sent += status;
  490. if (likely(req->rq_bytes_sent >= req->rq_slen)) {
  491. req->rq_bytes_sent = 0;
  492. return 0;
  493. }
  494. status = -EAGAIN;
  495. }
  496. switch (status) {
  497. case -EAGAIN:
  498. status = xs_nospace(task);
  499. break;
  500. default:
  501. dprintk("RPC: sendmsg returned unrecognized error %d\n",
  502. -status);
  503. case -EPIPE:
  504. xs_close(xprt);
  505. status = -ENOTCONN;
  506. }
  507. return status;
  508. }
  509. /**
  510. * xs_udp_send_request - write an RPC request to a UDP socket
  511. * @task: address of RPC task that manages the state of an RPC request
  512. *
  513. * Return values:
  514. * 0: The request has been sent
  515. * EAGAIN: The socket was blocked, please call again later to
  516. * complete the request
  517. * ENOTCONN: Caller needs to invoke connect logic then call again
  518. * other: Some other error occurred, the request was not sent
  519. */
  520. static int xs_udp_send_request(struct rpc_task *task)
  521. {
  522. struct rpc_rqst *req = task->tk_rqstp;
  523. struct rpc_xprt *xprt = req->rq_xprt;
  524. struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
  525. struct xdr_buf *xdr = &req->rq_snd_buf;
  526. int status;
  527. xs_pktdump("packet data:",
  528. req->rq_svec->iov_base,
  529. req->rq_svec->iov_len);
  530. if (!xprt_bound(xprt))
  531. return -ENOTCONN;
  532. status = xs_sendpages(transport->sock,
  533. xs_addr(xprt),
  534. xprt->addrlen, xdr,
  535. req->rq_bytes_sent);
  536. dprintk("RPC: xs_udp_send_request(%u) = %d\n",
  537. xdr->len - req->rq_bytes_sent, status);
  538. if (status >= 0) {
  539. req->rq_xmit_bytes_sent += status;
  540. if (status >= req->rq_slen)
  541. return 0;
  542. /* Still some bytes left; set up for a retry later. */
  543. status = -EAGAIN;
  544. }
  545. switch (status) {
  546. case -ENOTSOCK:
  547. status = -ENOTCONN;
  548. /* Should we call xs_close() here? */
  549. break;
  550. case -EAGAIN:
  551. status = xs_nospace(task);
  552. break;
  553. default:
  554. dprintk("RPC: sendmsg returned unrecognized error %d\n",
  555. -status);
  556. case -ENETUNREACH:
  557. case -EPIPE:
  558. case -ECONNREFUSED:
  559. /* When the server has died, an ICMP port unreachable message
  560. * prompts ECONNREFUSED. */
  561. clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
  562. }
  563. return status;
  564. }
  565. /**
  566. * xs_tcp_shutdown - gracefully shut down a TCP socket
  567. * @xprt: transport
  568. *
  569. * Initiates a graceful shutdown of the TCP socket by calling the
  570. * equivalent of shutdown(SHUT_WR);
  571. */
  572. static void xs_tcp_shutdown(struct rpc_xprt *xprt)
  573. {
  574. struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
  575. struct socket *sock = transport->sock;
  576. if (sock != NULL)
  577. kernel_sock_shutdown(sock, SHUT_WR);
  578. }
  579. /**
  580. * xs_tcp_send_request - write an RPC request to a TCP socket
  581. * @task: address of RPC task that manages the state of an RPC request
  582. *
  583. * Return values:
  584. * 0: The request has been sent
  585. * EAGAIN: The socket was blocked, please call again later to
  586. * complete the request
  587. * ENOTCONN: Caller needs to invoke connect logic then call again
  588. * other: Some other error occurred, the request was not sent
  589. *
  590. * XXX: In the case of soft timeouts, should we eventually give up
  591. * if sendmsg is not able to make progress?
  592. */
  593. static int xs_tcp_send_request(struct rpc_task *task)
  594. {
  595. struct rpc_rqst *req = task->tk_rqstp;
  596. struct rpc_xprt *xprt = req->rq_xprt;
  597. struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
  598. struct xdr_buf *xdr = &req->rq_snd_buf;
  599. int status;
  600. xs_encode_stream_record_marker(&req->rq_snd_buf);
  601. xs_pktdump("packet data:",
  602. req->rq_svec->iov_base,
  603. req->rq_svec->iov_len);
  604. /* Continue transmitting the packet/record. We must be careful
  605. * to cope with writespace callbacks arriving _after_ we have
  606. * called sendmsg(). */
  607. while (1) {
  608. status = xs_sendpages(transport->sock,
  609. NULL, 0, xdr, req->rq_bytes_sent);
  610. dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
  611. xdr->len - req->rq_bytes_sent, status);
  612. if (unlikely(status < 0))
  613. break;
  614. /* If we've sent the entire packet, immediately
  615. * reset the count of bytes sent. */
  616. req->rq_bytes_sent += status;
  617. req->rq_xmit_bytes_sent += status;
  618. if (likely(req->rq_bytes_sent >= req->rq_slen)) {
  619. req->rq_bytes_sent = 0;
  620. return 0;
  621. }
  622. if (status != 0)
  623. continue;
  624. status = -EAGAIN;
  625. break;
  626. }
  627. switch (status) {
  628. case -ENOTSOCK:
  629. status = -ENOTCONN;
  630. /* Should we call xs_close() here? */
  631. break;
  632. case -EAGAIN:
  633. status = xs_nospace(task);
  634. break;
  635. default:
  636. dprintk("RPC: sendmsg returned unrecognized error %d\n",
  637. -status);
  638. case -ECONNRESET:
  639. case -EPIPE:
  640. xs_tcp_shutdown(xprt);
  641. case -ECONNREFUSED:
  642. case -ENOTCONN:
  643. clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
  644. }
  645. return status;
  646. }
  647. /**
  648. * xs_tcp_release_xprt - clean up after a tcp transmission
  649. * @xprt: transport
  650. * @task: rpc task
  651. *
  652. * This cleans up if an error causes us to abort the transmission of a request.
  653. * In this case, the socket may need to be reset in order to avoid confusing
  654. * the server.
  655. */
  656. static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
  657. {
  658. struct rpc_rqst *req;
  659. if (task != xprt->snd_task)
  660. return;
  661. if (task == NULL)
  662. goto out_release;
  663. req = task->tk_rqstp;
  664. if (req->rq_bytes_sent == 0)
  665. goto out_release;
  666. if (req->rq_bytes_sent == req->rq_snd_buf.len)
  667. goto out_release;
  668. set_bit(XPRT_CLOSE_WAIT, &task->tk_xprt->state);
  669. out_release:
  670. xprt_release_xprt(xprt, task);
  671. }
  672. static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
  673. {
  674. transport->old_data_ready = sk->sk_data_ready;
  675. transport->old_state_change = sk->sk_state_change;
  676. transport->old_write_space = sk->sk_write_space;
  677. transport->old_error_report = sk->sk_error_report;
  678. }
  679. static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
  680. {
  681. sk->sk_data_ready = transport->old_data_ready;
  682. sk->sk_state_change = transport->old_state_change;
  683. sk->sk_write_space = transport->old_write_space;
  684. sk->sk_error_report = transport->old_error_report;
  685. }
  686. static void xs_reset_transport(struct sock_xprt *transport)
  687. {
  688. struct socket *sock = transport->sock;
  689. struct sock *sk = transport->inet;
  690. if (sk == NULL)
  691. return;
  692. transport->srcport = 0;
  693. write_lock_bh(&sk->sk_callback_lock);
  694. transport->inet = NULL;
  695. transport->sock = NULL;
  696. sk->sk_user_data = NULL;
  697. xs_restore_old_callbacks(transport, sk);
  698. write_unlock_bh(&sk->sk_callback_lock);
  699. sk->sk_no_check = 0;
  700. sock_release(sock);
  701. }
  702. /**
  703. * xs_close - close a socket
  704. * @xprt: transport
  705. *
  706. * This is used when all requests are complete; ie, no DRC state remains
  707. * on the server we want to save.
  708. *
  709. * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
  710. * xs_reset_transport() zeroing the socket from underneath a writer.
  711. */
  712. static void xs_close(struct rpc_xprt *xprt)
  713. {
  714. struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
  715. dprintk("RPC: xs_close xprt %p\n", xprt);
  716. xs_reset_transport(transport);
  717. xprt->reestablish_timeout = 0;
  718. smp_mb__before_clear_bit();
  719. clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
  720. clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
  721. clear_bit(XPRT_CLOSING, &xprt->state);
  722. smp_mb__after_clear_bit();
  723. xprt_disconnect_done(xprt);
  724. }
  725. static void xs_tcp_close(struct rpc_xprt *xprt)
  726. {
  727. if (test_and_clear_bit(XPRT_CONNECTION_CLOSE, &xprt->state))
  728. xs_close(xprt);
  729. else
  730. xs_tcp_shutdown(xprt);
  731. }
  732. /**
  733. * xs_destroy - prepare to shutdown a transport
  734. * @xprt: doomed transport
  735. *
  736. */
  737. static void xs_destroy(struct rpc_xprt *xprt)
  738. {
  739. struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
  740. dprintk("RPC: xs_destroy xprt %p\n", xprt);
  741. cancel_delayed_work_sync(&transport->connect_worker);
  742. xs_close(xprt);
  743. xs_free_peer_addresses(xprt);
  744. xprt_free(xprt);
  745. module_put(THIS_MODULE);
  746. }
  747. static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
  748. {
  749. return (struct rpc_xprt *) sk->sk_user_data;
  750. }
  751. static int xs_local_copy_to_xdr(struct xdr_buf *xdr, struct sk_buff *skb)
  752. {
  753. struct xdr_skb_reader desc = {
  754. .skb = skb,
  755. .offset = sizeof(rpc_fraghdr),
  756. .count = skb->len - sizeof(rpc_fraghdr),
  757. };
  758. if (xdr_partial_copy_from_skb(xdr, 0, &desc, xdr_skb_read_bits) < 0)
  759. return -1;
  760. if (desc.count)
  761. return -1;
  762. return 0;
  763. }
  764. /**
  765. * xs_local_data_ready - "data ready" callback for AF_LOCAL sockets
  766. * @sk: socket with data to read
  767. * @len: how much data to read
  768. *
  769. * Currently this assumes we can read the whole reply in a single gulp.
  770. */
  771. static void xs_local_data_ready(struct sock *sk, int len)
  772. {
  773. struct rpc_task *task;
  774. struct rpc_xprt *xprt;
  775. struct rpc_rqst *rovr;
  776. struct sk_buff *skb;
  777. int err, repsize, copied;
  778. u32 _xid;
  779. __be32 *xp;
  780. read_lock_bh(&sk->sk_callback_lock);
  781. dprintk("RPC: %s...\n", __func__);
  782. xprt = xprt_from_sock(sk);
  783. if (xprt == NULL)
  784. goto out;
  785. skb = skb_recv_datagram(sk, 0, 1, &err);
  786. if (skb == NULL)
  787. goto out;
  788. if (xprt->shutdown)
  789. goto dropit;
  790. repsize = skb->len - sizeof(rpc_fraghdr);
  791. if (repsize < 4) {
  792. dprintk("RPC: impossible RPC reply size %d\n", repsize);
  793. goto dropit;
  794. }
  795. /* Copy the XID from the skb... */
  796. xp = skb_header_pointer(skb, sizeof(rpc_fraghdr), sizeof(_xid), &_xid);
  797. if (xp == NULL)
  798. goto dropit;
  799. /* Look up and lock the request corresponding to the given XID */
  800. spin_lock(&xprt->transport_lock);
  801. rovr = xprt_lookup_rqst(xprt, *xp);
  802. if (!rovr)
  803. goto out_unlock;
  804. task = rovr->rq_task;
  805. copied = rovr->rq_private_buf.buflen;
  806. if (copied > repsize)
  807. copied = repsize;
  808. if (xs_local_copy_to_xdr(&rovr->rq_private_buf, skb)) {
  809. dprintk("RPC: sk_buff copy failed\n");
  810. goto out_unlock;
  811. }
  812. xprt_complete_rqst(task, copied);
  813. out_unlock:
  814. spin_unlock(&xprt->transport_lock);
  815. dropit:
  816. skb_free_datagram(sk, skb);
  817. out:
  818. read_unlock_bh(&sk->sk_callback_lock);
  819. }
  820. /**
  821. * xs_udp_data_ready - "data ready" callback for UDP sockets
  822. * @sk: socket with data to read
  823. * @len: how much data to read
  824. *
  825. */
  826. static void xs_udp_data_ready(struct sock *sk, int len)
  827. {
  828. struct rpc_task *task;
  829. struct rpc_xprt *xprt;
  830. struct rpc_rqst *rovr;
  831. struct sk_buff *skb;
  832. int err, repsize, copied;
  833. u32 _xid;
  834. __be32 *xp;
  835. read_lock_bh(&sk->sk_callback_lock);
  836. dprintk("RPC: xs_udp_data_ready...\n");
  837. if (!(xprt = xprt_from_sock(sk)))
  838. goto out;
  839. if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
  840. goto out;
  841. if (xprt->shutdown)
  842. goto dropit;
  843. repsize = skb->len - sizeof(struct udphdr);
  844. if (repsize < 4) {
  845. dprintk("RPC: impossible RPC reply size %d!\n", repsize);
  846. goto dropit;
  847. }
  848. /* Copy the XID from the skb... */
  849. xp = skb_header_pointer(skb, sizeof(struct udphdr),
  850. sizeof(_xid), &_xid);
  851. if (xp == NULL)
  852. goto dropit;
  853. /* Look up and lock the request corresponding to the given XID */
  854. spin_lock(&xprt->transport_lock);
  855. rovr = xprt_lookup_rqst(xprt, *xp);
  856. if (!rovr)
  857. goto out_unlock;
  858. task = rovr->rq_task;
  859. if ((copied = rovr->rq_private_buf.buflen) > repsize)
  860. copied = repsize;
  861. /* Suck it into the iovec, verify checksum if not done by hw. */
  862. if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
  863. UDPX_INC_STATS_BH(sk, UDP_MIB_INERRORS);
  864. goto out_unlock;
  865. }
  866. UDPX_INC_STATS_BH(sk, UDP_MIB_INDATAGRAMS);
  867. /* Something worked... */
  868. dst_confirm(skb_dst(skb));
  869. xprt_adjust_cwnd(task, copied);
  870. xprt_complete_rqst(task, copied);
  871. out_unlock:
  872. spin_unlock(&xprt->transport_lock);
  873. dropit:
  874. skb_free_datagram(sk, skb);
  875. out:
  876. read_unlock_bh(&sk->sk_callback_lock);
  877. }
  878. static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
  879. {
  880. struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
  881. size_t len, used;
  882. char *p;
  883. p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset;
  884. len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset;
  885. used = xdr_skb_read_bits(desc, p, len);
  886. transport->tcp_offset += used;
  887. if (used != len)
  888. return;
  889. transport->tcp_reclen = ntohl(transport->tcp_fraghdr);
  890. if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
  891. transport->tcp_flags |= TCP_RCV_LAST_FRAG;
  892. else
  893. transport->tcp_flags &= ~TCP_RCV_LAST_FRAG;
  894. transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
  895. transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR;
  896. transport->tcp_offset = 0;
  897. /* Sanity check of the record length */
  898. if (unlikely(transport->tcp_reclen < 8)) {
  899. dprintk("RPC: invalid TCP record fragment length\n");
  900. xprt_force_disconnect(xprt);
  901. return;
  902. }
  903. dprintk("RPC: reading TCP record fragment of length %d\n",
  904. transport->tcp_reclen);
  905. }
  906. static void xs_tcp_check_fraghdr(struct sock_xprt *transport)
  907. {
  908. if (transport->tcp_offset == transport->tcp_reclen) {
  909. transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR;
  910. transport->tcp_offset = 0;
  911. if (transport->tcp_flags & TCP_RCV_LAST_FRAG) {
  912. transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
  913. transport->tcp_flags |= TCP_RCV_COPY_XID;
  914. transport->tcp_copied = 0;
  915. }
  916. }
  917. }
  918. static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc)
  919. {
  920. size_t len, used;
  921. char *p;
  922. len = sizeof(transport->tcp_xid) - transport->tcp_offset;
  923. dprintk("RPC: reading XID (%Zu bytes)\n", len);
  924. p = ((char *) &transport->tcp_xid) + transport->tcp_offset;
  925. used = xdr_skb_read_bits(desc, p, len);
  926. transport->tcp_offset += used;
  927. if (used != len)
  928. return;
  929. transport->tcp_flags &= ~TCP_RCV_COPY_XID;
  930. transport->tcp_flags |= TCP_RCV_READ_CALLDIR;
  931. transport->tcp_copied = 4;
  932. dprintk("RPC: reading %s XID %08x\n",
  933. (transport->tcp_flags & TCP_RPC_REPLY) ? "reply for"
  934. : "request with",
  935. ntohl(transport->tcp_xid));
  936. xs_tcp_check_fraghdr(transport);
  937. }
  938. static inline void xs_tcp_read_calldir(struct sock_xprt *transport,
  939. struct xdr_skb_reader *desc)
  940. {
  941. size_t len, used;
  942. u32 offset;
  943. char *p;
  944. /*
  945. * We want transport->tcp_offset to be 8 at the end of this routine
  946. * (4 bytes for the xid and 4 bytes for the call/reply flag).
  947. * When this function is called for the first time,
  948. * transport->tcp_offset is 4 (after having already read the xid).
  949. */
  950. offset = transport->tcp_offset - sizeof(transport->tcp_xid);
  951. len = sizeof(transport->tcp_calldir) - offset;
  952. dprintk("RPC: reading CALL/REPLY flag (%Zu bytes)\n", len);
  953. p = ((char *) &transport->tcp_calldir) + offset;
  954. used = xdr_skb_read_bits(desc, p, len);
  955. transport->tcp_offset += used;
  956. if (used != len)
  957. return;
  958. transport->tcp_flags &= ~TCP_RCV_READ_CALLDIR;
  959. /*
  960. * We don't yet have the XDR buffer, so we will write the calldir
  961. * out after we get the buffer from the 'struct rpc_rqst'
  962. */
  963. switch (ntohl(transport->tcp_calldir)) {
  964. case RPC_REPLY:
  965. transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
  966. transport->tcp_flags |= TCP_RCV_COPY_DATA;
  967. transport->tcp_flags |= TCP_RPC_REPLY;
  968. break;
  969. case RPC_CALL:
  970. transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
  971. transport->tcp_flags |= TCP_RCV_COPY_DATA;
  972. transport->tcp_flags &= ~TCP_RPC_REPLY;
  973. break;
  974. default:
  975. dprintk("RPC: invalid request message type\n");
  976. xprt_force_disconnect(&transport->xprt);
  977. }
  978. xs_tcp_check_fraghdr(transport);
  979. }
  980. static inline void xs_tcp_read_common(struct rpc_xprt *xprt,
  981. struct xdr_skb_reader *desc,
  982. struct rpc_rqst *req)
  983. {
  984. struct sock_xprt *transport =
  985. container_of(xprt, struct sock_xprt, xprt);
  986. struct xdr_buf *rcvbuf;
  987. size_t len;
  988. ssize_t r;
  989. rcvbuf = &req->rq_private_buf;
  990. if (transport->tcp_flags & TCP_RCV_COPY_CALLDIR) {
  991. /*
  992. * Save the RPC direction in the XDR buffer
  993. */
  994. memcpy(rcvbuf->head[0].iov_base + transport->tcp_copied,
  995. &transport->tcp_calldir,
  996. sizeof(transport->tcp_calldir));
  997. transport->tcp_copied += sizeof(transport->tcp_calldir);
  998. transport->tcp_flags &= ~TCP_RCV_COPY_CALLDIR;
  999. }
  1000. len = desc->count;
  1001. if (len > transport->tcp_reclen - transport->tcp_offset) {
  1002. struct xdr_skb_reader my_desc;
  1003. len = transport->tcp_reclen - transport->tcp_offset;
  1004. memcpy(&my_desc, desc, sizeof(my_desc));
  1005. my_desc.count = len;
  1006. r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
  1007. &my_desc, xdr_skb_read_bits);
  1008. desc->count -= r;
  1009. desc->offset += r;
  1010. } else
  1011. r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
  1012. desc, xdr_skb_read_bits);
  1013. if (r > 0) {
  1014. transport->tcp_copied += r;
  1015. transport->tcp_offset += r;
  1016. }
  1017. if (r != len) {
  1018. /* Error when copying to the receive buffer,
  1019. * usually because we weren't able to allocate
  1020. * additional buffer pages. All we can do now
  1021. * is turn off TCP_RCV_COPY_DATA, so the request
  1022. * will not receive any additional updates,
  1023. * and time out.
  1024. * Any remaining data from this record will
  1025. * be discarded.
  1026. */
  1027. transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
  1028. dprintk("RPC: XID %08x truncated request\n",
  1029. ntohl(transport->tcp_xid));
  1030. dprintk("RPC: xprt = %p, tcp_copied = %lu, "
  1031. "tcp_offset = %u, tcp_reclen = %u\n",
  1032. xprt, transport->tcp_copied,
  1033. transport->tcp_offset, transport->tcp_reclen);
  1034. return;
  1035. }
  1036. dprintk("RPC: XID %08x read %Zd bytes\n",
  1037. ntohl(transport->tcp_xid), r);
  1038. dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
  1039. "tcp_reclen = %u\n", xprt, transport->tcp_copied,
  1040. transport->tcp_offset, transport->tcp_reclen);
  1041. if (transport->tcp_copied == req->rq_private_buf.buflen)
  1042. transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
  1043. else if (transport->tcp_offset == transport->tcp_reclen) {
  1044. if (transport->tcp_flags & TCP_RCV_LAST_FRAG)
  1045. transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
  1046. }
  1047. }
  1048. /*
  1049. * Finds the request corresponding to the RPC xid and invokes the common
  1050. * tcp read code to read the data.
  1051. */
  1052. static inline int xs_tcp_read_reply(struct rpc_xprt *xprt,
  1053. struct xdr_skb_reader *desc)
  1054. {
  1055. struct sock_xprt *transport =
  1056. container_of(xprt, struct sock_xprt, xprt);
  1057. struct rpc_rqst *req;
  1058. dprintk("RPC: read reply XID %08x\n", ntohl(transport->tcp_xid));
  1059. /* Find and lock the request corresponding to this xid */
  1060. spin_lock(&xprt->transport_lock);
  1061. req = xprt_lookup_rqst(xprt, transport->tcp_xid);
  1062. if (!req) {
  1063. dprintk("RPC: XID %08x request not found!\n",
  1064. ntohl(transport->tcp_xid));
  1065. spin_unlock(&xprt->transport_lock);
  1066. return -1;
  1067. }
  1068. xs_tcp_read_common(xprt, desc, req);
  1069. if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
  1070. xprt_complete_rqst(req->rq_task, transport->tcp_copied);
  1071. spin_unlock(&xprt->transport_lock);
  1072. return 0;
  1073. }
  1074. #if defined(CONFIG_NFS_V4_1)
  1075. /*
  1076. * Obtains an rpc_rqst previously allocated and invokes the common
  1077. * tcp read code to read the data. The result is placed in the callback
  1078. * queue.
  1079. * If we're unable to obtain the rpc_rqst we schedule the closing of the
  1080. * connection and return -1.
  1081. */
  1082. static inline int xs_tcp_read_callback(struct rpc_xprt *xprt,
  1083. struct xdr_skb_reader *desc)
  1084. {
  1085. struct sock_xprt *transport =
  1086. container_of(xprt, struct sock_xprt, xprt);
  1087. struct rpc_rqst *req;
  1088. req = xprt_alloc_bc_request(xprt);
  1089. if (req == NULL) {
  1090. printk(KERN_WARNING "Callback slot table overflowed\n");
  1091. xprt_force_disconnect(xprt);
  1092. return -1;
  1093. }
  1094. req->rq_xid = transport->tcp_xid;
  1095. dprintk("RPC: read callback XID %08x\n", ntohl(req->rq_xid));
  1096. xs_tcp_read_common(xprt, desc, req);
  1097. if (!(transport->tcp_flags & TCP_RCV_COPY_DATA)) {
  1098. struct svc_serv *bc_serv = xprt->bc_serv;
  1099. /*
  1100. * Add callback request to callback list. The callback
  1101. * service sleeps on the sv_cb_waitq waiting for new
  1102. * requests. Wake it up after adding enqueing the
  1103. * request.
  1104. */
  1105. dprintk("RPC: add callback request to list\n");
  1106. spin_lock(&bc_serv->sv_cb_lock);
  1107. list_add(&req->rq_bc_list, &bc_serv->sv_cb_list);
  1108. spin_unlock(&bc_serv->sv_cb_lock);
  1109. wake_up(&bc_serv->sv_cb_waitq);
  1110. }
  1111. req->rq_private_buf.len = transport->tcp_copied;
  1112. return 0;
  1113. }
  1114. static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
  1115. struct xdr_skb_reader *desc)
  1116. {
  1117. struct sock_xprt *transport =
  1118. container_of(xprt, struct sock_xprt, xprt);
  1119. return (transport->tcp_flags & TCP_RPC_REPLY) ?
  1120. xs_tcp_read_reply(xprt, desc) :
  1121. xs_tcp_read_callback(xprt, desc);
  1122. }
  1123. #else
  1124. static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
  1125. struct xdr_skb_reader *desc)
  1126. {
  1127. return xs_tcp_read_reply(xprt, desc);
  1128. }
  1129. #endif /* CONFIG_NFS_V4_1 */
  1130. /*
  1131. * Read data off the transport. This can be either an RPC_CALL or an
  1132. * RPC_REPLY. Relay the processing to helper functions.
  1133. */
  1134. static void xs_tcp_read_data(struct rpc_xprt *xprt,
  1135. struct xdr_skb_reader *desc)
  1136. {
  1137. struct sock_xprt *transport =
  1138. container_of(xprt, struct sock_xprt, xprt);
  1139. if (_xs_tcp_read_data(xprt, desc) == 0)
  1140. xs_tcp_check_fraghdr(transport);
  1141. else {
  1142. /*
  1143. * The transport_lock protects the request handling.
  1144. * There's no need to hold it to update the tcp_flags.
  1145. */
  1146. transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
  1147. }
  1148. }
  1149. static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc)
  1150. {
  1151. size_t len;
  1152. len = transport->tcp_reclen - transport->tcp_offset;
  1153. if (len > desc->count)
  1154. len = desc->count;
  1155. desc->count -= len;
  1156. desc->offset += len;
  1157. transport->tcp_offset += len;
  1158. dprintk("RPC: discarded %Zu bytes\n", len);
  1159. xs_tcp_check_fraghdr(transport);
  1160. }
  1161. static int xs_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb, unsigned int offset, size_t len)
  1162. {
  1163. struct rpc_xprt *xprt = rd_desc->arg.data;
  1164. struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
  1165. struct xdr_skb_reader desc = {
  1166. .skb = skb,
  1167. .offset = offset,
  1168. .count = len,
  1169. };
  1170. dprintk("RPC: xs_tcp_data_recv started\n");
  1171. do {
  1172. /* Read in a new fragment marker if necessary */
  1173. /* Can we ever really expect to get completely empty fragments? */
  1174. if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) {
  1175. xs_tcp_read_fraghdr(xprt, &desc);
  1176. continue;
  1177. }
  1178. /* Read in the xid if necessary */
  1179. if (transport->tcp_flags & TCP_RCV_COPY_XID) {
  1180. xs_tcp_read_xid(transport, &desc);
  1181. continue;
  1182. }
  1183. /* Read in the call/reply flag */
  1184. if (transport->tcp_flags & TCP_RCV_READ_CALLDIR) {
  1185. xs_tcp_read_calldir(transport, &desc);
  1186. continue;
  1187. }
  1188. /* Read in the request data */
  1189. if (transport->tcp_flags & TCP_RCV_COPY_DATA) {
  1190. xs_tcp_read_data(xprt, &desc);
  1191. continue;
  1192. }
  1193. /* Skip over any trailing bytes on short reads */
  1194. xs_tcp_read_discard(transport, &desc);
  1195. } while (desc.count);
  1196. dprintk("RPC: xs_tcp_data_recv done\n");
  1197. return len - desc.count;
  1198. }
  1199. /**
  1200. * xs_tcp_data_ready - "data ready" callback for TCP sockets
  1201. * @sk: socket with data to read
  1202. * @bytes: how much data to read
  1203. *
  1204. */
  1205. static void xs_tcp_data_ready(struct sock *sk, int bytes)
  1206. {
  1207. struct rpc_xprt *xprt;
  1208. read_descriptor_t rd_desc;
  1209. int read;
  1210. dprintk("RPC: xs_tcp_data_ready...\n");
  1211. read_lock_bh(&sk->sk_callback_lock);
  1212. if (!(xprt = xprt_from_sock(sk)))
  1213. goto out;
  1214. if (xprt->shutdown)
  1215. goto out;
  1216. /* Any data means we had a useful conversation, so
  1217. * the we don't need to delay the next reconnect
  1218. */
  1219. if (xprt->reestablish_timeout)
  1220. xprt->reestablish_timeout = 0;
  1221. /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
  1222. rd_desc.arg.data = xprt;
  1223. do {
  1224. rd_desc.count = 65536;
  1225. read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
  1226. } while (read > 0);
  1227. out:
  1228. read_unlock_bh(&sk->sk_callback_lock);
  1229. }
  1230. /*
  1231. * Do the equivalent of linger/linger2 handling for dealing with
  1232. * broken servers that don't close the socket in a timely
  1233. * fashion
  1234. */
  1235. static void xs_tcp_schedule_linger_timeout(struct rpc_xprt *xprt,
  1236. unsigned long timeout)
  1237. {
  1238. struct sock_xprt *transport;
  1239. if (xprt_test_and_set_connecting(xprt))
  1240. return;
  1241. set_bit(XPRT_CONNECTION_ABORT, &xprt->state);
  1242. transport = container_of(xprt, struct sock_xprt, xprt);
  1243. queue_delayed_work(rpciod_workqueue, &transport->connect_worker,
  1244. timeout);
  1245. }
  1246. static void xs_tcp_cancel_linger_timeout(struct rpc_xprt *xprt)
  1247. {
  1248. struct sock_xprt *transport;
  1249. transport = container_of(xprt, struct sock_xprt, xprt);
  1250. if (!test_bit(XPRT_CONNECTION_ABORT, &xprt->state) ||
  1251. !cancel_delayed_work(&transport->connect_worker))
  1252. return;
  1253. clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
  1254. xprt_clear_connecting(xprt);
  1255. }
  1256. static void xs_sock_mark_closed(struct rpc_xprt *xprt)
  1257. {
  1258. smp_mb__before_clear_bit();
  1259. clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
  1260. clear_bit(XPRT_CLOSING, &xprt->state);
  1261. smp_mb__after_clear_bit();
  1262. /* Mark transport as closed and wake up all pending tasks */
  1263. xprt_disconnect_done(xprt);
  1264. }
  1265. /**
  1266. * xs_tcp_state_change - callback to handle TCP socket state changes
  1267. * @sk: socket whose state has changed
  1268. *
  1269. */
  1270. static void xs_tcp_state_change(struct sock *sk)
  1271. {
  1272. struct rpc_xprt *xprt;
  1273. read_lock_bh(&sk->sk_callback_lock);
  1274. if (!(xprt = xprt_from_sock(sk)))
  1275. goto out;
  1276. dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
  1277. dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n",
  1278. sk->sk_state, xprt_connected(xprt),
  1279. sock_flag(sk, SOCK_DEAD),
  1280. sock_flag(sk, SOCK_ZAPPED),
  1281. sk->sk_shutdown);
  1282. switch (sk->sk_state) {
  1283. case TCP_ESTABLISHED:
  1284. spin_lock(&xprt->transport_lock);
  1285. if (!xprt_test_and_set_connected(xprt)) {
  1286. struct sock_xprt *transport = container_of(xprt,
  1287. struct sock_xprt, xprt);
  1288. /* Reset TCP record info */
  1289. transport->tcp_offset = 0;
  1290. transport->tcp_reclen = 0;
  1291. transport->tcp_copied = 0;
  1292. transport->tcp_flags =
  1293. TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
  1294. xprt_wake_pending_tasks(xprt, -EAGAIN);
  1295. }
  1296. spin_unlock(&xprt->transport_lock);
  1297. break;
  1298. case TCP_FIN_WAIT1:
  1299. /* The client initiated a shutdown of the socket */
  1300. xprt->connect_cookie++;
  1301. xprt->reestablish_timeout = 0;
  1302. set_bit(XPRT_CLOSING, &xprt->state);
  1303. smp_mb__before_clear_bit();
  1304. clear_bit(XPRT_CONNECTED, &xprt->state);
  1305. clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
  1306. smp_mb__after_clear_bit();
  1307. xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
  1308. break;
  1309. case TCP_CLOSE_WAIT:
  1310. /* The server initiated a shutdown of the socket */
  1311. xprt_force_disconnect(xprt);
  1312. xprt->connect_cookie++;
  1313. case TCP_CLOSING:
  1314. /*
  1315. * If the server closed down the connection, make sure that
  1316. * we back off before reconnecting
  1317. */
  1318. if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
  1319. xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
  1320. break;
  1321. case TCP_LAST_ACK:
  1322. set_bit(XPRT_CLOSING, &xprt->state);
  1323. xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
  1324. smp_mb__before_clear_bit();
  1325. clear_bit(XPRT_CONNECTED, &xprt->state);
  1326. smp_mb__after_clear_bit();
  1327. break;
  1328. case TCP_CLOSE:
  1329. xs_tcp_cancel_linger_timeout(xprt);
  1330. xs_sock_mark_closed(xprt);
  1331. }
  1332. out:
  1333. read_unlock_bh(&sk->sk_callback_lock);
  1334. }
  1335. /**
  1336. * xs_error_report - callback mainly for catching socket errors
  1337. * @sk: socket
  1338. */
  1339. static void xs_error_report(struct sock *sk)
  1340. {
  1341. struct rpc_xprt *xprt;
  1342. read_lock_bh(&sk->sk_callback_lock);
  1343. if (!(xprt = xprt_from_sock(sk)))
  1344. goto out;
  1345. dprintk("RPC: %s client %p...\n"
  1346. "RPC: error %d\n",
  1347. __func__, xprt, sk->sk_err);
  1348. xprt_wake_pending_tasks(xprt, -EAGAIN);
  1349. out:
  1350. read_unlock_bh(&sk->sk_callback_lock);
  1351. }
  1352. static void xs_write_space(struct sock *sk)
  1353. {
  1354. struct socket *sock;
  1355. struct rpc_xprt *xprt;
  1356. if (unlikely(!(sock = sk->sk_socket)))
  1357. return;
  1358. clear_bit(SOCK_NOSPACE, &sock->flags);
  1359. if (unlikely(!(xprt = xprt_from_sock(sk))))
  1360. return;
  1361. if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0)
  1362. return;
  1363. xprt_write_space(xprt);
  1364. }
  1365. /**
  1366. * xs_udp_write_space - callback invoked when socket buffer space
  1367. * becomes available
  1368. * @sk: socket whose state has changed
  1369. *
  1370. * Called when more output buffer space is available for this socket.
  1371. * We try not to wake our writers until they can make "significant"
  1372. * progress, otherwise we'll waste resources thrashing kernel_sendmsg
  1373. * with a bunch of small requests.
  1374. */
  1375. static void xs_udp_write_space(struct sock *sk)
  1376. {
  1377. read_lock_bh(&sk->sk_callback_lock);
  1378. /* from net/core/sock.c:sock_def_write_space */
  1379. if (sock_writeable(sk))
  1380. xs_write_space(sk);
  1381. read_unlock_bh(&sk->sk_callback_lock);
  1382. }
  1383. /**
  1384. * xs_tcp_write_space - callback invoked when socket buffer space
  1385. * becomes available
  1386. * @sk: socket whose state has changed
  1387. *
  1388. * Called when more output buffer space is available for this socket.
  1389. * We try not to wake our writers until they can make "significant"
  1390. * progress, otherwise we'll waste resources thrashing kernel_sendmsg
  1391. * with a bunch of small requests.
  1392. */
  1393. static void xs_tcp_write_space(struct sock *sk)
  1394. {
  1395. read_lock_bh(&sk->sk_callback_lock);
  1396. /* from net/core/stream.c:sk_stream_write_space */
  1397. if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk))
  1398. xs_write_space(sk);
  1399. read_unlock_bh(&sk->sk_callback_lock);
  1400. }
  1401. static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
  1402. {
  1403. struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
  1404. struct sock *sk = transport->inet;
  1405. if (transport->rcvsize) {
  1406. sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
  1407. sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
  1408. }
  1409. if (transport->sndsize) {
  1410. sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
  1411. sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
  1412. sk->sk_write_space(sk);
  1413. }
  1414. }
  1415. /**
  1416. * xs_udp_set_buffer_size - set send and receive limits
  1417. * @xprt: generic transport
  1418. * @sndsize: requested size of send buffer, in bytes
  1419. * @rcvsize: requested size of receive buffer, in bytes
  1420. *
  1421. * Set socket send and receive buffer size limits.
  1422. */
  1423. static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
  1424. {
  1425. struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
  1426. transport->sndsize = 0;
  1427. if (sndsize)
  1428. transport->sndsize = sndsize + 1024;
  1429. transport->rcvsize = 0;
  1430. if (rcvsize)
  1431. transport->rcvsize = rcvsize + 1024;
  1432. xs_udp_do_set_buffer_size(xprt);
  1433. }
  1434. /**
  1435. * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
  1436. * @task: task that timed out
  1437. *
  1438. * Adjust the congestion window after a retransmit timeout has occurred.
  1439. */
  1440. static void xs_udp_timer(struct rpc_task *task)
  1441. {
  1442. xprt_adjust_cwnd(task, -ETIMEDOUT);
  1443. }
  1444. static unsigned short xs_get_random_port(void)
  1445. {
  1446. unsigned short range = xprt_max_resvport - xprt_min_resvport;
  1447. unsigned short rand = (unsigned short) net_random() % range;
  1448. return rand + xprt_min_resvport;
  1449. }
  1450. /**
  1451. * xs_set_port - reset the port number in the remote endpoint address
  1452. * @xprt: generic transport
  1453. * @port: new port number
  1454. *
  1455. */
  1456. static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
  1457. {
  1458. dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
  1459. rpc_set_port(xs_addr(xprt), port);
  1460. xs_update_peer_port(xprt);
  1461. }
  1462. static unsigned short xs_get_srcport(struct sock_xprt *transport)
  1463. {
  1464. unsigned short port = transport->srcport;
  1465. if (port == 0 && transport->xprt.resvport)
  1466. port = xs_get_random_port();
  1467. return port;
  1468. }
  1469. static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port)
  1470. {
  1471. if (transport->srcport != 0)
  1472. transport->srcport = 0;
  1473. if (!transport->xprt.resvport)
  1474. return 0;
  1475. if (port <= xprt_min_resvport || port > xprt_max_resvport)
  1476. return xprt_max_resvport;
  1477. return --port;
  1478. }
  1479. static int xs_bind(struct sock_xprt *transport, struct socket *sock)
  1480. {
  1481. struct sockaddr_storage myaddr;
  1482. int err, nloop = 0;
  1483. unsigned short port = xs_get_srcport(transport);
  1484. unsigned short last;
  1485. memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen);
  1486. do {
  1487. rpc_set_port((struct sockaddr *)&myaddr, port);
  1488. err = kernel_bind(sock, (struct sockaddr *)&myaddr,
  1489. transport->xprt.addrlen);
  1490. if (port == 0)
  1491. break;
  1492. if (err == 0) {
  1493. transport->srcport = port;
  1494. break;
  1495. }
  1496. last = port;
  1497. port = xs_next_srcport(transport, port);
  1498. if (port > last)
  1499. nloop++;
  1500. } while (err == -EADDRINUSE && nloop != 2);
  1501. if (myaddr.ss_family == AF_INET)
  1502. dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__,
  1503. &((struct sockaddr_in *)&myaddr)->sin_addr,
  1504. port, err ? "failed" : "ok", err);
  1505. else
  1506. dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__,
  1507. &((struct sockaddr_in6 *)&myaddr)->sin6_addr,
  1508. port, err ? "failed" : "ok", err);
  1509. return err;
  1510. }
  1511. /*
  1512. * We don't support autobind on AF_LOCAL sockets
  1513. */
  1514. static void xs_local_rpcbind(struct rpc_task *task)
  1515. {
  1516. xprt_set_bound(task->tk_xprt);
  1517. }
  1518. static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port)
  1519. {
  1520. }
  1521. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  1522. static struct lock_class_key xs_key[2];
  1523. static struct lock_class_key xs_slock_key[2];
  1524. static inline void xs_reclassify_socketu(struct socket *sock)
  1525. {
  1526. struct sock *sk = sock->sk;
  1527. BUG_ON(sock_owned_by_user(sk));
  1528. sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC",
  1529. &xs_slock_key[1], "sk_lock-AF_LOCAL-RPC", &xs_key[1]);
  1530. }
  1531. static inline void xs_reclassify_socket4(struct socket *sock)
  1532. {
  1533. struct sock *sk = sock->sk;
  1534. BUG_ON(sock_owned_by_user(sk));
  1535. sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
  1536. &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
  1537. }
  1538. static inline void xs_reclassify_socket6(struct socket *sock)
  1539. {
  1540. struct sock *sk = sock->sk;
  1541. BUG_ON(sock_owned_by_user(sk));
  1542. sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
  1543. &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
  1544. }
  1545. static inline void xs_reclassify_socket(int family, struct socket *sock)
  1546. {
  1547. switch (family) {
  1548. case AF_LOCAL:
  1549. xs_reclassify_socketu(sock);
  1550. break;
  1551. case AF_INET:
  1552. xs_reclassify_socket4(sock);
  1553. break;
  1554. case AF_INET6:
  1555. xs_reclassify_socket6(sock);
  1556. break;
  1557. }
  1558. }
  1559. #else
  1560. static inline void xs_reclassify_socketu(struct socket *sock)
  1561. {
  1562. }
  1563. static inline void xs_reclassify_socket4(struct socket *sock)
  1564. {
  1565. }
  1566. static inline void xs_reclassify_socket6(struct socket *sock)
  1567. {
  1568. }
  1569. static inline void xs_reclassify_socket(int family, struct socket *sock)
  1570. {
  1571. }
  1572. #endif
  1573. static struct socket *xs_create_sock(struct rpc_xprt *xprt,
  1574. struct sock_xprt *transport, int family, int type, int protocol)
  1575. {
  1576. struct socket *sock;
  1577. int err;
  1578. err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1);
  1579. if (err < 0) {
  1580. dprintk("RPC: can't create %d transport socket (%d).\n",
  1581. protocol, -err);
  1582. goto out;
  1583. }
  1584. xs_reclassify_socket(family, sock);
  1585. err = xs_bind(transport, sock);
  1586. if (err) {
  1587. sock_release(sock);
  1588. goto out;
  1589. }
  1590. return sock;
  1591. out:
  1592. return ERR_PTR(err);
  1593. }
  1594. static int xs_local_finish_connecting(struct rpc_xprt *xprt,
  1595. struct socket *sock)
  1596. {
  1597. struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
  1598. xprt);
  1599. if (!transport->inet) {
  1600. struct sock *sk = sock->sk;
  1601. write_lock_bh(&sk->sk_callback_lock);
  1602. xs_save_old_callbacks(transport, sk);
  1603. sk->sk_user_data = xprt;
  1604. sk->sk_data_ready = xs_local_data_ready;
  1605. sk->sk_write_space = xs_udp_write_space;
  1606. sk->sk_error_report = xs_error_report;
  1607. sk->sk_allocation = GFP_ATOMIC;
  1608. xprt_clear_connected(xprt);
  1609. /* Reset to new socket */
  1610. transport->sock = sock;
  1611. transport->inet = sk;
  1612. write_unlock_bh(&sk->sk_callback_lock);
  1613. }
  1614. /* Tell the socket layer to start connecting... */
  1615. xprt->stat.connect_count++;
  1616. xprt->stat.connect_start = jiffies;
  1617. return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0);
  1618. }
  1619. /**
  1620. * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint
  1621. * @xprt: RPC transport to connect
  1622. * @transport: socket transport to connect
  1623. * @create_sock: function to create a socket of the correct type
  1624. *
  1625. * Invoked by a work queue tasklet.
  1626. */
  1627. static void xs_local_setup_socket(struct work_struct *work)
  1628. {
  1629. struct sock_xprt *transport =
  1630. container_of(work, struct sock_xprt, connect_worker.work);
  1631. struct rpc_xprt *xprt = &transport->xprt;
  1632. struct socket *sock;
  1633. int status = -EIO;
  1634. if (xprt->shutdown)
  1635. goto out;
  1636. clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
  1637. status = __sock_create(xprt->xprt_net, AF_LOCAL,
  1638. SOCK_STREAM, 0, &sock, 1);
  1639. if (status < 0) {
  1640. dprintk("RPC: can't create AF_LOCAL "
  1641. "transport socket (%d).\n", -status);
  1642. goto out;
  1643. }
  1644. xs_reclassify_socketu(sock);
  1645. dprintk("RPC: worker connecting xprt %p via AF_LOCAL to %s\n",
  1646. xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
  1647. status = xs_local_finish_connecting(xprt, sock);
  1648. switch (status) {
  1649. case 0:
  1650. dprintk("RPC: xprt %p connected to %s\n",
  1651. xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
  1652. xprt_set_connected(xprt);
  1653. break;
  1654. case -ENOENT:
  1655. dprintk("RPC: xprt %p: socket %s does not exist\n",
  1656. xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
  1657. break;
  1658. default:
  1659. printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n",
  1660. __func__, -status,
  1661. xprt->address_strings[RPC_DISPLAY_ADDR]);
  1662. }
  1663. out:
  1664. xprt_clear_connecting(xprt);
  1665. xprt_wake_pending_tasks(xprt, status);
  1666. }
  1667. static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
  1668. {
  1669. struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
  1670. if (!transport->inet) {
  1671. struct sock *sk = sock->sk;
  1672. write_lock_bh(&sk->sk_callback_lock);
  1673. xs_save_old_callbacks(transport, sk);
  1674. sk->sk_user_data = xprt;
  1675. sk->sk_data_ready = xs_udp_data_ready;
  1676. sk->sk_write_space = xs_udp_write_space;
  1677. sk->sk_error_report = xs_error_report;
  1678. sk->sk_no_check = UDP_CSUM_NORCV;
  1679. sk->sk_allocation = GFP_ATOMIC;
  1680. xprt_set_connected(xprt);
  1681. /* Reset to new socket */
  1682. transport->sock = sock;
  1683. transport->inet = sk;
  1684. write_unlock_bh(&sk->sk_callback_lock);
  1685. }
  1686. xs_udp_do_set_buffer_size(xprt);
  1687. }
  1688. static void xs_udp_setup_socket(struct work_struct *work)
  1689. {
  1690. struct sock_xprt *transport =
  1691. container_of(work, struct sock_xprt, connect_worker.work);
  1692. struct rpc_xprt *xprt = &transport->xprt;
  1693. struct socket *sock = transport->sock;
  1694. int status = -EIO;
  1695. if (xprt->shutdown)
  1696. goto out;
  1697. /* Start by resetting any existing state */
  1698. xs_reset_transport(transport);
  1699. sock = xs_create_sock(xprt, transport,
  1700. xs_addr(xprt)->sa_family, SOCK_DGRAM, IPPROTO_UDP);
  1701. if (IS_ERR(sock))
  1702. goto out;
  1703. dprintk("RPC: worker connecting xprt %p via %s to "
  1704. "%s (port %s)\n", xprt,
  1705. xprt->address_strings[RPC_DISPLAY_PROTO],
  1706. xprt->address_strings[RPC_DISPLAY_ADDR],
  1707. xprt->address_strings[RPC_DISPLAY_PORT]);
  1708. xs_udp_finish_connecting(xprt, sock);
  1709. status = 0;
  1710. out:
  1711. xprt_clear_connecting(xprt);
  1712. xprt_wake_pending_tasks(xprt, status);
  1713. }
  1714. /*
  1715. * We need to preserve the port number so the reply cache on the server can
  1716. * find our cached RPC replies when we get around to reconnecting.
  1717. */
  1718. static void xs_abort_connection(struct sock_xprt *transport)
  1719. {
  1720. int result;
  1721. struct sockaddr any;
  1722. dprintk("RPC: disconnecting xprt %p to reuse port\n", transport);
  1723. /*
  1724. * Disconnect the transport socket by doing a connect operation
  1725. * with AF_UNSPEC. This should return immediately...
  1726. */
  1727. memset(&any, 0, sizeof(any));
  1728. any.sa_family = AF_UNSPEC;
  1729. result = kernel_connect(transport->sock, &any, sizeof(any), 0);
  1730. if (!result)
  1731. xs_sock_mark_closed(&transport->xprt);
  1732. else
  1733. dprintk("RPC: AF_UNSPEC connect return code %d\n",
  1734. result);
  1735. }
  1736. static void xs_tcp_reuse_connection(struct sock_xprt *transport)
  1737. {
  1738. unsigned int state = transport->inet->sk_state;
  1739. if (state == TCP_CLOSE && transport->sock->state == SS_UNCONNECTED) {
  1740. /* we don't need to abort the connection if the socket
  1741. * hasn't undergone a shutdown
  1742. */
  1743. if (transport->inet->sk_shutdown == 0)
  1744. return;
  1745. dprintk("RPC: %s: TCP_CLOSEd and sk_shutdown set to %d\n",
  1746. __func__, transport->inet->sk_shutdown);
  1747. }
  1748. if ((1 << state) & (TCPF_ESTABLISHED|TCPF_SYN_SENT)) {
  1749. /* we don't need to abort the connection if the socket
  1750. * hasn't undergone a shutdown
  1751. */
  1752. if (transport->inet->sk_shutdown == 0)
  1753. return;
  1754. dprintk("RPC: %s: ESTABLISHED/SYN_SENT "
  1755. "sk_shutdown set to %d\n",
  1756. __func__, transport->inet->sk_shutdown);
  1757. }
  1758. xs_abort_connection(transport);
  1759. }
  1760. static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
  1761. {
  1762. struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
  1763. int ret = -ENOTCONN;
  1764. if (!transport->inet) {
  1765. struct sock *sk = sock->sk;
  1766. write_lock_bh(&sk->sk_callback_lock);
  1767. xs_save_old_callbacks(transport, sk);
  1768. sk->sk_user_data = xprt;
  1769. sk->sk_data_ready = xs_tcp_data_ready;
  1770. sk->sk_state_change = xs_tcp_state_change;
  1771. sk->sk_write_space = xs_tcp_write_space;
  1772. sk->sk_error_report = xs_error_report;
  1773. sk->sk_allocation = GFP_ATOMIC;
  1774. /* socket options */
  1775. sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
  1776. sock_reset_flag(sk, SOCK_LINGER);
  1777. tcp_sk(sk)->linger2 = 0;
  1778. tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
  1779. xprt_clear_connected(xprt);
  1780. /* Reset to new socket */
  1781. transport->sock = sock;
  1782. transport->inet = sk;
  1783. write_unlock_bh(&sk->sk_callback_lock);
  1784. }
  1785. if (!xprt_bound(xprt))
  1786. goto out;
  1787. /* Tell the socket layer to start connecting... */
  1788. xprt->stat.connect_count++;
  1789. xprt->stat.connect_start = jiffies;
  1790. ret = kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
  1791. switch (ret) {
  1792. case 0:
  1793. case -EINPROGRESS:
  1794. /* SYN_SENT! */
  1795. xprt->connect_cookie++;
  1796. if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
  1797. xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
  1798. }
  1799. out:
  1800. return ret;
  1801. }
  1802. /**
  1803. * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
  1804. * @xprt: RPC transport to connect
  1805. * @transport: socket transport to connect
  1806. * @create_sock: function to create a socket of the correct type
  1807. *
  1808. * Invoked by a work queue tasklet.
  1809. */
  1810. static void xs_tcp_setup_socket(struct work_struct *work)
  1811. {
  1812. struct sock_xprt *transport =
  1813. container_of(work, struct sock_xprt, connect_worker.work);
  1814. struct socket *sock = transport->sock;
  1815. struct rpc_xprt *xprt = &transport->xprt;
  1816. int status = -EIO;
  1817. if (xprt->shutdown)
  1818. goto out;
  1819. if (!sock) {
  1820. clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
  1821. sock = xs_create_sock(xprt, transport,
  1822. xs_addr(xprt)->sa_family, SOCK_STREAM, IPPROTO_TCP);
  1823. if (IS_ERR(sock)) {
  1824. status = PTR_ERR(sock);
  1825. goto out;
  1826. }
  1827. } else {
  1828. int abort_and_exit;
  1829. abort_and_exit = test_and_clear_bit(XPRT_CONNECTION_ABORT,
  1830. &xprt->state);
  1831. /* "close" the socket, preserving the local port */
  1832. xs_tcp_reuse_connection(transport);
  1833. if (abort_and_exit)
  1834. goto out_eagain;
  1835. }
  1836. dprintk("RPC: worker connecting xprt %p via %s to "
  1837. "%s (port %s)\n", xprt,
  1838. xprt->address_strings[RPC_DISPLAY_PROTO],
  1839. xprt->address_strings[RPC_DISPLAY_ADDR],
  1840. xprt->address_strings[RPC_DISPLAY_PORT]);
  1841. status = xs_tcp_finish_connecting(xprt, sock);
  1842. dprintk("RPC: %p connect status %d connected %d sock state %d\n",
  1843. xprt, -status, xprt_connected(xprt),
  1844. sock->sk->sk_state);
  1845. switch (status) {
  1846. default:
  1847. printk("%s: connect returned unhandled error %d\n",
  1848. __func__, status);
  1849. case -EADDRNOTAVAIL:
  1850. /* We're probably in TIME_WAIT. Get rid of existing socket,
  1851. * and retry
  1852. */
  1853. set_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
  1854. xprt_force_disconnect(xprt);
  1855. break;
  1856. case -ECONNREFUSED:
  1857. case -ECONNRESET:
  1858. case -ENETUNREACH:
  1859. /* retry with existing socket, after a delay */
  1860. case 0:
  1861. case -EINPROGRESS:
  1862. case -EALREADY:
  1863. xprt_clear_connecting(xprt);
  1864. return;
  1865. case -EINVAL:
  1866. /* Happens, for instance, if the user specified a link
  1867. * local IPv6 address without a scope-id.
  1868. */
  1869. goto out;
  1870. }
  1871. out_eagain:
  1872. status = -EAGAIN;
  1873. out:
  1874. xprt_clear_connecting(xprt);
  1875. xprt_wake_pending_tasks(xprt, status);
  1876. }
  1877. /**
  1878. * xs_connect - connect a socket to a remote endpoint
  1879. * @task: address of RPC task that manages state of connect request
  1880. *
  1881. * TCP: If the remote end dropped the connection, delay reconnecting.
  1882. *
  1883. * UDP socket connects are synchronous, but we use a work queue anyway
  1884. * to guarantee that even unprivileged user processes can set up a
  1885. * socket on a privileged port.
  1886. *
  1887. * If a UDP socket connect fails, the delay behavior here prevents
  1888. * retry floods (hard mounts).
  1889. */
  1890. static void xs_connect(struct rpc_task *task)
  1891. {
  1892. struct rpc_xprt *xprt = task->tk_xprt;
  1893. struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
  1894. if (transport->sock != NULL && !RPC_IS_SOFTCONN(task)) {
  1895. dprintk("RPC: xs_connect delayed xprt %p for %lu "
  1896. "seconds\n",
  1897. xprt, xprt->reestablish_timeout / HZ);
  1898. queue_delayed_work(rpciod_workqueue,
  1899. &transport->connect_worker,
  1900. xprt->reestablish_timeout);
  1901. xprt->reestablish_timeout <<= 1;
  1902. if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
  1903. xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
  1904. if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO)
  1905. xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO;
  1906. } else {
  1907. dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
  1908. queue_delayed_work(rpciod_workqueue,
  1909. &transport->connect_worker, 0);
  1910. }
  1911. }
  1912. /**
  1913. * xs_local_print_stats - display AF_LOCAL socket-specifc stats
  1914. * @xprt: rpc_xprt struct containing statistics
  1915. * @seq: output file
  1916. *
  1917. */
  1918. static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
  1919. {
  1920. long idle_time = 0;
  1921. if (xprt_connected(xprt))
  1922. idle_time = (long)(jiffies - xprt->last_used) / HZ;
  1923. seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu "
  1924. "%llu %llu\n",
  1925. xprt->stat.bind_count,
  1926. xprt->stat.connect_count,
  1927. xprt->stat.connect_time,
  1928. idle_time,
  1929. xprt->stat.sends,
  1930. xprt->stat.recvs,
  1931. xprt->stat.bad_xids,
  1932. xprt->stat.req_u,
  1933. xprt->stat.bklog_u);
  1934. }
  1935. /**
  1936. * xs_udp_print_stats - display UDP socket-specifc stats
  1937. * @xprt: rpc_xprt struct containing statistics
  1938. * @seq: output file
  1939. *
  1940. */
  1941. static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
  1942. {
  1943. struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
  1944. seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n",
  1945. transport->srcport,
  1946. xprt->stat.bind_count,
  1947. xprt->stat.sends,
  1948. xprt->stat.recvs,
  1949. xprt->stat.bad_xids,
  1950. xprt->stat.req_u,
  1951. xprt->stat.bklog_u);
  1952. }
  1953. /**
  1954. * xs_tcp_print_stats - display TCP socket-specifc stats
  1955. * @xprt: rpc_xprt struct containing statistics
  1956. * @seq: output file
  1957. *
  1958. */
  1959. static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
  1960. {
  1961. struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
  1962. long idle_time = 0;
  1963. if (xprt_connected(xprt))
  1964. idle_time = (long)(jiffies - xprt->last_used) / HZ;
  1965. seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n",
  1966. transport->srcport,
  1967. xprt->stat.bind_count,
  1968. xprt->stat.connect_count,
  1969. xprt->stat.connect_time,
  1970. idle_time,
  1971. xprt->stat.sends,
  1972. xprt->stat.recvs,
  1973. xprt->stat.bad_xids,
  1974. xprt->stat.req_u,
  1975. xprt->stat.bklog_u);
  1976. }
  1977. /*
  1978. * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
  1979. * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
  1980. * to use the server side send routines.
  1981. */
  1982. static void *bc_malloc(struct rpc_task *task, size_t size)
  1983. {
  1984. struct page *page;
  1985. struct rpc_buffer *buf;
  1986. BUG_ON(size > PAGE_SIZE - sizeof(struct rpc_buffer));
  1987. page = alloc_page(GFP_KERNEL);
  1988. if (!page)
  1989. return NULL;
  1990. buf = page_address(page);
  1991. buf->len = PAGE_SIZE;
  1992. return buf->data;
  1993. }
  1994. /*
  1995. * Free the space allocated in the bc_alloc routine
  1996. */
  1997. static void bc_free(void *buffer)
  1998. {
  1999. struct rpc_buffer *buf;
  2000. if (!buffer)
  2001. return;
  2002. buf = container_of(buffer, struct rpc_buffer, data);
  2003. free_page((unsigned long)buf);
  2004. }
  2005. /*
  2006. * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex
  2007. * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request.
  2008. */
  2009. static int bc_sendto(struct rpc_rqst *req)
  2010. {
  2011. int len;
  2012. struct xdr_buf *xbufp = &req->rq_snd_buf;
  2013. struct rpc_xprt *xprt = req->rq_xprt;
  2014. struct sock_xprt *transport =
  2015. container_of(xprt, struct sock_xprt, xprt);
  2016. struct socket *sock = transport->sock;
  2017. unsigned long headoff;
  2018. unsigned long tailoff;
  2019. xs_encode_stream_record_marker(xbufp);
  2020. tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK;
  2021. headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK;
  2022. len = svc_send_common(sock, xbufp,
  2023. virt_to_page(xbufp->head[0].iov_base), headoff,
  2024. xbufp->tail[0].iov_base, tailoff);
  2025. if (len != xbufp->len) {
  2026. printk(KERN_NOTICE "Error sending entire callback!\n");
  2027. len = -EAGAIN;
  2028. }
  2029. return len;
  2030. }
  2031. /*
  2032. * The send routine. Borrows from svc_send
  2033. */
  2034. static int bc_send_request(struct rpc_task *task)
  2035. {
  2036. struct rpc_rqst *req = task->tk_rqstp;
  2037. struct svc_xprt *xprt;
  2038. struct svc_sock *svsk;
  2039. u32 len;
  2040. dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid));
  2041. /*
  2042. * Get the server socket associated with this callback xprt
  2043. */
  2044. xprt = req->rq_xprt->bc_xprt;
  2045. svsk = container_of(xprt, struct svc_sock, sk_xprt);
  2046. /*
  2047. * Grab the mutex to serialize data as the connection is shared
  2048. * with the fore channel
  2049. */
  2050. if (!mutex_trylock(&xprt->xpt_mutex)) {
  2051. rpc_sleep_on(&xprt->xpt_bc_pending, task, NULL);
  2052. if (!mutex_trylock(&xprt->xpt_mutex))
  2053. return -EAGAIN;
  2054. rpc_wake_up_queued_task(&xprt->xpt_bc_pending, task);
  2055. }
  2056. if (test_bit(XPT_DEAD, &xprt->xpt_flags))
  2057. len = -ENOTCONN;
  2058. else
  2059. len = bc_sendto(req);
  2060. mutex_unlock(&xprt->xpt_mutex);
  2061. if (len > 0)
  2062. len = 0;
  2063. return len;
  2064. }
  2065. /*
  2066. * The close routine. Since this is client initiated, we do nothing
  2067. */
  2068. static void bc_close(struct rpc_xprt *xprt)
  2069. {
  2070. }
  2071. /*
  2072. * The xprt destroy routine. Again, because this connection is client
  2073. * initiated, we do nothing
  2074. */
  2075. static void bc_destroy(struct rpc_xprt *xprt)
  2076. {
  2077. }
  2078. static struct rpc_xprt_ops xs_local_ops = {
  2079. .reserve_xprt = xprt_reserve_xprt,
  2080. .release_xprt = xs_tcp_release_xprt,
  2081. .rpcbind = xs_local_rpcbind,
  2082. .set_port = xs_local_set_port,
  2083. .connect = xs_connect,
  2084. .buf_alloc = rpc_malloc,
  2085. .buf_free = rpc_free,
  2086. .send_request = xs_local_send_request,
  2087. .set_retrans_timeout = xprt_set_retrans_timeout_def,
  2088. .close = xs_close,
  2089. .destroy = xs_destroy,
  2090. .print_stats = xs_local_print_stats,
  2091. };
  2092. static struct rpc_xprt_ops xs_udp_ops = {
  2093. .set_buffer_size = xs_udp_set_buffer_size,
  2094. .reserve_xprt = xprt_reserve_xprt_cong,
  2095. .release_xprt = xprt_release_xprt_cong,
  2096. .rpcbind = rpcb_getport_async,
  2097. .set_port = xs_set_port,
  2098. .connect = xs_connect,
  2099. .buf_alloc = rpc_malloc,
  2100. .buf_free = rpc_free,
  2101. .send_request = xs_udp_send_request,
  2102. .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
  2103. .timer = xs_udp_timer,
  2104. .release_request = xprt_release_rqst_cong,
  2105. .close = xs_close,
  2106. .destroy = xs_destroy,
  2107. .print_stats = xs_udp_print_stats,
  2108. };
  2109. static struct rpc_xprt_ops xs_tcp_ops = {
  2110. .reserve_xprt = xprt_reserve_xprt,
  2111. .release_xprt = xs_tcp_release_xprt,
  2112. .rpcbind = rpcb_getport_async,
  2113. .set_port = xs_set_port,
  2114. .connect = xs_connect,
  2115. .buf_alloc = rpc_malloc,
  2116. .buf_free = rpc_free,
  2117. .send_request = xs_tcp_send_request,
  2118. .set_retrans_timeout = xprt_set_retrans_timeout_def,
  2119. .close = xs_tcp_close,
  2120. .destroy = xs_destroy,
  2121. .print_stats = xs_tcp_print_stats,
  2122. };
  2123. /*
  2124. * The rpc_xprt_ops for the server backchannel
  2125. */
  2126. static struct rpc_xprt_ops bc_tcp_ops = {
  2127. .reserve_xprt = xprt_reserve_xprt,
  2128. .release_xprt = xprt_release_xprt,
  2129. .buf_alloc = bc_malloc,
  2130. .buf_free = bc_free,
  2131. .send_request = bc_send_request,
  2132. .set_retrans_timeout = xprt_set_retrans_timeout_def,
  2133. .close = bc_close,
  2134. .destroy = bc_destroy,
  2135. .print_stats = xs_tcp_print_stats,
  2136. };
  2137. static int xs_init_anyaddr(const int family, struct sockaddr *sap)
  2138. {
  2139. static const struct sockaddr_in sin = {
  2140. .sin_family = AF_INET,
  2141. .sin_addr.s_addr = htonl(INADDR_ANY),
  2142. };
  2143. static const struct sockaddr_in6 sin6 = {
  2144. .sin6_family = AF_INET6,
  2145. .sin6_addr = IN6ADDR_ANY_INIT,
  2146. };
  2147. switch (family) {
  2148. case AF_LOCAL:
  2149. break;
  2150. case AF_INET:
  2151. memcpy(sap, &sin, sizeof(sin));
  2152. break;
  2153. case AF_INET6:
  2154. memcpy(sap, &sin6, sizeof(sin6));
  2155. break;
  2156. default:
  2157. dprintk("RPC: %s: Bad address family\n", __func__);
  2158. return -EAFNOSUPPORT;
  2159. }
  2160. return 0;
  2161. }
  2162. static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
  2163. unsigned int slot_table_size)
  2164. {
  2165. struct rpc_xprt *xprt;
  2166. struct sock_xprt *new;
  2167. if (args->addrlen > sizeof(xprt->addr)) {
  2168. dprintk("RPC: xs_setup_xprt: address too large\n");
  2169. return ERR_PTR(-EBADF);
  2170. }
  2171. xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size);
  2172. if (xprt == NULL) {
  2173. dprintk("RPC: xs_setup_xprt: couldn't allocate "
  2174. "rpc_xprt\n");
  2175. return ERR_PTR(-ENOMEM);
  2176. }
  2177. new = container_of(xprt, struct sock_xprt, xprt);
  2178. memcpy(&xprt->addr, args->dstaddr, args->addrlen);
  2179. xprt->addrlen = args->addrlen;
  2180. if (args->srcaddr)
  2181. memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
  2182. else {
  2183. int err;
  2184. err = xs_init_anyaddr(args->dstaddr->sa_family,
  2185. (struct sockaddr *)&new->srcaddr);
  2186. if (err != 0)
  2187. return ERR_PTR(err);
  2188. }
  2189. return xprt;
  2190. }
  2191. static const struct rpc_timeout xs_local_default_timeout = {
  2192. .to_initval = 10 * HZ,
  2193. .to_maxval = 10 * HZ,
  2194. .to_retries = 2,
  2195. };
  2196. /**
  2197. * xs_setup_local - Set up transport to use an AF_LOCAL socket
  2198. * @args: rpc transport creation arguments
  2199. *
  2200. * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP
  2201. */
  2202. static struct rpc_xprt *xs_setup_local(struct xprt_create *args)
  2203. {
  2204. struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr;
  2205. struct sock_xprt *transport;
  2206. struct rpc_xprt *xprt;
  2207. struct rpc_xprt *ret;
  2208. xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries);
  2209. if (IS_ERR(xprt))
  2210. return xprt;
  2211. transport = container_of(xprt, struct sock_xprt, xprt);
  2212. xprt->prot = 0;
  2213. xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
  2214. xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
  2215. xprt->bind_timeout = XS_BIND_TO;
  2216. xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
  2217. xprt->idle_timeout = XS_IDLE_DISC_TO;
  2218. xprt->ops = &xs_local_ops;
  2219. xprt->timeout = &xs_local_default_timeout;
  2220. switch (sun->sun_family) {
  2221. case AF_LOCAL:
  2222. if (sun->sun_path[0] != '/') {
  2223. dprintk("RPC: bad AF_LOCAL address: %s\n",
  2224. sun->sun_path);
  2225. ret = ERR_PTR(-EINVAL);
  2226. goto out_err;
  2227. }
  2228. xprt_set_bound(xprt);
  2229. INIT_DELAYED_WORK(&transport->connect_worker,
  2230. xs_local_setup_socket);
  2231. xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL);
  2232. break;
  2233. default:
  2234. ret = ERR_PTR(-EAFNOSUPPORT);
  2235. goto out_err;
  2236. }
  2237. dprintk("RPC: set up xprt to %s via AF_LOCAL\n",
  2238. xprt->address_strings[RPC_DISPLAY_ADDR]);
  2239. if (try_module_get(THIS_MODULE))
  2240. return xprt;
  2241. ret = ERR_PTR(-EINVAL);
  2242. out_err:
  2243. xprt_free(xprt);
  2244. return ret;
  2245. }
  2246. static const struct rpc_timeout xs_udp_default_timeout = {
  2247. .to_initval = 5 * HZ,
  2248. .to_maxval = 30 * HZ,
  2249. .to_increment = 5 * HZ,
  2250. .to_retries = 5,
  2251. };
  2252. /**
  2253. * xs_setup_udp - Set up transport to use a UDP socket
  2254. * @args: rpc transport creation arguments
  2255. *
  2256. */
  2257. static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
  2258. {
  2259. struct sockaddr *addr = args->dstaddr;
  2260. struct rpc_xprt *xprt;
  2261. struct sock_xprt *transport;
  2262. struct rpc_xprt *ret;
  2263. xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries);
  2264. if (IS_ERR(xprt))
  2265. return xprt;
  2266. transport = container_of(xprt, struct sock_xprt, xprt);
  2267. xprt->prot = IPPROTO_UDP;
  2268. xprt->tsh_size = 0;
  2269. /* XXX: header size can vary due to auth type, IPv6, etc. */
  2270. xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
  2271. xprt->bind_timeout = XS_BIND_TO;
  2272. xprt->reestablish_timeout = XS_UDP_REEST_TO;
  2273. xprt->idle_timeout = XS_IDLE_DISC_TO;
  2274. xprt->ops = &xs_udp_ops;
  2275. xprt->timeout = &xs_udp_default_timeout;
  2276. switch (addr->sa_family) {
  2277. case AF_INET:
  2278. if (((struct sockaddr_in *)addr)->sin_port != htons(0))
  2279. xprt_set_bound(xprt);
  2280. INIT_DELAYED_WORK(&transport->connect_worker,
  2281. xs_udp_setup_socket);
  2282. xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
  2283. break;
  2284. case AF_INET6:
  2285. if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
  2286. xprt_set_bound(xprt);
  2287. INIT_DELAYED_WORK(&transport->connect_worker,
  2288. xs_udp_setup_socket);
  2289. xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
  2290. break;
  2291. default:
  2292. ret = ERR_PTR(-EAFNOSUPPORT);
  2293. goto out_err;
  2294. }
  2295. if (xprt_bound(xprt))
  2296. dprintk("RPC: set up xprt to %s (port %s) via %s\n",
  2297. xprt->address_strings[RPC_DISPLAY_ADDR],
  2298. xprt->address_strings[RPC_DISPLAY_PORT],
  2299. xprt->address_strings[RPC_DISPLAY_PROTO]);
  2300. else
  2301. dprintk("RPC: set up xprt to %s (autobind) via %s\n",
  2302. xprt->address_strings[RPC_DISPLAY_ADDR],
  2303. xprt->address_strings[RPC_DISPLAY_PROTO]);
  2304. if (try_module_get(THIS_MODULE))
  2305. return xprt;
  2306. ret = ERR_PTR(-EINVAL);
  2307. out_err:
  2308. xprt_free(xprt);
  2309. return ret;
  2310. }
  2311. static const struct rpc_timeout xs_tcp_default_timeout = {
  2312. .to_initval = 60 * HZ,
  2313. .to_maxval = 60 * HZ,
  2314. .to_retries = 2,
  2315. };
  2316. /**
  2317. * xs_setup_tcp - Set up transport to use a TCP socket
  2318. * @args: rpc transport creation arguments
  2319. *
  2320. */
  2321. static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
  2322. {
  2323. struct sockaddr *addr = args->dstaddr;
  2324. struct rpc_xprt *xprt;
  2325. struct sock_xprt *transport;
  2326. struct rpc_xprt *ret;
  2327. xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries);
  2328. if (IS_ERR(xprt))
  2329. return xprt;
  2330. transport = container_of(xprt, struct sock_xprt, xprt);
  2331. xprt->prot = IPPROTO_TCP;
  2332. xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
  2333. xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
  2334. xprt->bind_timeout = XS_BIND_TO;
  2335. xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
  2336. xprt->idle_timeout = XS_IDLE_DISC_TO;
  2337. xprt->ops = &xs_tcp_ops;
  2338. xprt->timeout = &xs_tcp_default_timeout;
  2339. switch (addr->sa_family) {
  2340. case AF_INET:
  2341. if (((struct sockaddr_in *)addr)->sin_port != htons(0))
  2342. xprt_set_bound(xprt);
  2343. INIT_DELAYED_WORK(&transport->connect_worker,
  2344. xs_tcp_setup_socket);
  2345. xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
  2346. break;
  2347. case AF_INET6:
  2348. if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
  2349. xprt_set_bound(xprt);
  2350. INIT_DELAYED_WORK(&transport->connect_worker,
  2351. xs_tcp_setup_socket);
  2352. xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
  2353. break;
  2354. default:
  2355. ret = ERR_PTR(-EAFNOSUPPORT);
  2356. goto out_err;
  2357. }
  2358. if (xprt_bound(xprt))
  2359. dprintk("RPC: set up xprt to %s (port %s) via %s\n",
  2360. xprt->address_strings[RPC_DISPLAY_ADDR],
  2361. xprt->address_strings[RPC_DISPLAY_PORT],
  2362. xprt->address_strings[RPC_DISPLAY_PROTO]);
  2363. else
  2364. dprintk("RPC: set up xprt to %s (autobind) via %s\n",
  2365. xprt->address_strings[RPC_DISPLAY_ADDR],
  2366. xprt->address_strings[RPC_DISPLAY_PROTO]);
  2367. if (try_module_get(THIS_MODULE))
  2368. return xprt;
  2369. ret = ERR_PTR(-EINVAL);
  2370. out_err:
  2371. xprt_free(xprt);
  2372. return ret;
  2373. }
  2374. /**
  2375. * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
  2376. * @args: rpc transport creation arguments
  2377. *
  2378. */
  2379. static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
  2380. {
  2381. struct sockaddr *addr = args->dstaddr;
  2382. struct rpc_xprt *xprt;
  2383. struct sock_xprt *transport;
  2384. struct svc_sock *bc_sock;
  2385. struct rpc_xprt *ret;
  2386. if (args->bc_xprt->xpt_bc_xprt) {
  2387. /*
  2388. * This server connection already has a backchannel
  2389. * export; we can't create a new one, as we wouldn't be
  2390. * able to match replies based on xid any more. So,
  2391. * reuse the already-existing one:
  2392. */
  2393. return args->bc_xprt->xpt_bc_xprt;
  2394. }
  2395. xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries);
  2396. if (IS_ERR(xprt))
  2397. return xprt;
  2398. transport = container_of(xprt, struct sock_xprt, xprt);
  2399. xprt->prot = IPPROTO_TCP;
  2400. xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
  2401. xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
  2402. xprt->timeout = &xs_tcp_default_timeout;
  2403. /* backchannel */
  2404. xprt_set_bound(xprt);
  2405. xprt->bind_timeout = 0;
  2406. xprt->reestablish_timeout = 0;
  2407. xprt->idle_timeout = 0;
  2408. xprt->ops = &bc_tcp_ops;
  2409. switch (addr->sa_family) {
  2410. case AF_INET:
  2411. xs_format_peer_addresses(xprt, "tcp",
  2412. RPCBIND_NETID_TCP);
  2413. break;
  2414. case AF_INET6:
  2415. xs_format_peer_addresses(xprt, "tcp",
  2416. RPCBIND_NETID_TCP6);
  2417. break;
  2418. default:
  2419. ret = ERR_PTR(-EAFNOSUPPORT);
  2420. goto out_err;
  2421. }
  2422. dprintk("RPC: set up xprt to %s (port %s) via %s\n",
  2423. xprt->address_strings[RPC_DISPLAY_ADDR],
  2424. xprt->address_strings[RPC_DISPLAY_PORT],
  2425. xprt->address_strings[RPC_DISPLAY_PROTO]);
  2426. /*
  2427. * Once we've associated a backchannel xprt with a connection,
  2428. * we want to keep it around as long as long as the connection
  2429. * lasts, in case we need to start using it for a backchannel
  2430. * again; this reference won't be dropped until bc_xprt is
  2431. * destroyed.
  2432. */
  2433. xprt_get(xprt);
  2434. args->bc_xprt->xpt_bc_xprt = xprt;
  2435. xprt->bc_xprt = args->bc_xprt;
  2436. bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
  2437. transport->sock = bc_sock->sk_sock;
  2438. transport->inet = bc_sock->sk_sk;
  2439. /*
  2440. * Since we don't want connections for the backchannel, we set
  2441. * the xprt status to connected
  2442. */
  2443. xprt_set_connected(xprt);
  2444. if (try_module_get(THIS_MODULE))
  2445. return xprt;
  2446. xprt_put(xprt);
  2447. ret = ERR_PTR(-EINVAL);
  2448. out_err:
  2449. xprt_free(xprt);
  2450. return ret;
  2451. }
  2452. static struct xprt_class xs_local_transport = {
  2453. .list = LIST_HEAD_INIT(xs_local_transport.list),
  2454. .name = "named UNIX socket",
  2455. .owner = THIS_MODULE,
  2456. .ident = XPRT_TRANSPORT_LOCAL,
  2457. .setup = xs_setup_local,
  2458. };
  2459. static struct xprt_class xs_udp_transport = {
  2460. .list = LIST_HEAD_INIT(xs_udp_transport.list),
  2461. .name = "udp",
  2462. .owner = THIS_MODULE,
  2463. .ident = XPRT_TRANSPORT_UDP,
  2464. .setup = xs_setup_udp,
  2465. };
  2466. static struct xprt_class xs_tcp_transport = {
  2467. .list = LIST_HEAD_INIT(xs_tcp_transport.list),
  2468. .name = "tcp",
  2469. .owner = THIS_MODULE,
  2470. .ident = XPRT_TRANSPORT_TCP,
  2471. .setup = xs_setup_tcp,
  2472. };
  2473. static struct xprt_class xs_bc_tcp_transport = {
  2474. .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list),
  2475. .name = "tcp NFSv4.1 backchannel",
  2476. .owner = THIS_MODULE,
  2477. .ident = XPRT_TRANSPORT_BC_TCP,
  2478. .setup = xs_setup_bc_tcp,
  2479. };
  2480. /**
  2481. * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
  2482. *
  2483. */
  2484. int init_socket_xprt(void)
  2485. {
  2486. #ifdef RPC_DEBUG
  2487. if (!sunrpc_table_header)
  2488. sunrpc_table_header = register_sysctl_table(sunrpc_table);
  2489. #endif
  2490. xprt_register_transport(&xs_local_transport);
  2491. xprt_register_transport(&xs_udp_transport);
  2492. xprt_register_transport(&xs_tcp_transport);
  2493. xprt_register_transport(&xs_bc_tcp_transport);
  2494. return 0;
  2495. }
  2496. /**
  2497. * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
  2498. *
  2499. */
  2500. void cleanup_socket_xprt(void)
  2501. {
  2502. #ifdef RPC_DEBUG
  2503. if (sunrpc_table_header) {
  2504. unregister_sysctl_table(sunrpc_table_header);
  2505. sunrpc_table_header = NULL;
  2506. }
  2507. #endif
  2508. xprt_unregister_transport(&xs_local_transport);
  2509. xprt_unregister_transport(&xs_udp_transport);
  2510. xprt_unregister_transport(&xs_tcp_transport);
  2511. xprt_unregister_transport(&xs_bc_tcp_transport);
  2512. }
  2513. static int param_set_uint_minmax(const char *val,
  2514. const struct kernel_param *kp,
  2515. unsigned int min, unsigned int max)
  2516. {
  2517. unsigned long num;
  2518. int ret;
  2519. if (!val)
  2520. return -EINVAL;
  2521. ret = strict_strtoul(val, 0, &num);
  2522. if (ret == -EINVAL || num < min || num > max)
  2523. return -EINVAL;
  2524. *((unsigned int *)kp->arg) = num;
  2525. return 0;
  2526. }
  2527. static int param_set_portnr(const char *val, const struct kernel_param *kp)
  2528. {
  2529. return param_set_uint_minmax(val, kp,
  2530. RPC_MIN_RESVPORT,
  2531. RPC_MAX_RESVPORT);
  2532. }
  2533. static struct kernel_param_ops param_ops_portnr = {
  2534. .set = param_set_portnr,
  2535. .get = param_get_uint,
  2536. };
  2537. #define param_check_portnr(name, p) \
  2538. __param_check(name, p, unsigned int);
  2539. module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
  2540. module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
  2541. static int param_set_slot_table_size(const char *val,
  2542. const struct kernel_param *kp)
  2543. {
  2544. return param_set_uint_minmax(val, kp,
  2545. RPC_MIN_SLOT_TABLE,
  2546. RPC_MAX_SLOT_TABLE);
  2547. }
  2548. static struct kernel_param_ops param_ops_slot_table_size = {
  2549. .set = param_set_slot_table_size,
  2550. .get = param_get_uint,
  2551. };
  2552. #define param_check_slot_table_size(name, p) \
  2553. __param_check(name, p, unsigned int);
  2554. module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
  2555. slot_table_size, 0644);
  2556. module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
  2557. slot_table_size, 0644);